-
Notifications
You must be signed in to change notification settings - Fork 24
CDA-118: Add input examples for Level endpoints #1815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7e541e1
Add example files for level endpoints. Add example loading methods to…
zack-rma d3b7bed
Moved OpenAPI endpoint examples classes and helper methods to new ope…
zack-rma 3d59f98
Merge remote-tracking branch 'hec/develop' into bugfix/level-examples
zack-rma 59dbc47
Cleaned up ApiServlet imports
zack-rma 000e9e1
Reverted change to incorrect example, updated intended example
zack-rma 398e27f
Scoping fixes per feedback
zack-rma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
cwms-data-api/src/main/java/cwms/cda/openapi/ExampleUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /* | ||
| * | ||
| * MIT License | ||
| * | ||
| * Copyright (c) 2026 Hydrologic Engineering Center | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all | ||
| * copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
| * DEALINGS IN THE | ||
| * SOFTWARE. | ||
| */ | ||
|
|
||
| package cwms.cda.openapi; | ||
|
|
||
| import cwms.cda.ApiServlet; | ||
| import cwms.cda.data.dto.CwmsDTOBase; | ||
| import cwms.cda.data.dto.locationlevel.ConstantLocationLevel; | ||
| import cwms.cda.data.dto.locationlevel.LocationLevel; | ||
| import cwms.cda.data.dto.locationlevel.SeasonalLocationLevel; | ||
| import cwms.cda.data.dto.locationlevel.TimeSeriesLocationLevel; | ||
| import cwms.cda.data.dto.locationlevel.VirtualLocationLevel; | ||
| import cwms.cda.formatters.Formats; | ||
| import io.javalin.plugin.openapi.OpenApiOptions; | ||
| import io.swagger.v3.oas.models.examples.Example; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import org.apache.commons.io.IOUtils; | ||
|
|
||
| public final class ExampleUtils { | ||
| private ExampleUtils() { | ||
| throw new IllegalStateException("Utility class"); | ||
| } | ||
|
|
||
| /** | ||
| * Method to add specific input examples to API endpoints as defaults. | ||
| * Can be overridden with controller annotations. | ||
| * Primarily for use with endpoints that accept multiple classes as input and cannot be represented via annotations. | ||
| * Current annotation limitations are due to legacy Javalin library. | ||
| * | ||
| * @param ops the OpenApiOptions object to add the examples to. | ||
| */ | ||
| public static void addEndpointExamples(OpenApiOptions ops) { | ||
| String swaggerPath = "/swagger-docs"; | ||
| for (EndpointExamples endpoint : EndpointExamples.values()) { | ||
| endpoint.getExamples().forEach(config -> | ||
| ops.path(swaggerPath) | ||
| .addExample(config.targetClass, config.displayName, | ||
| buildExample(config.exampleClass, config.resourcePath)) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Builds an example object for the given class and resource path. | ||
| * @param exampleClass the class of the example object | ||
| * @param path the path to the example resource | ||
| * @return Example object | ||
| */ | ||
| private static Example buildExample(Class<? extends CwmsDTOBase> exampleClass, String path) { | ||
| cwms.cda.formatters.ContentType contentType = Formats.parseHeader(Formats.JSON, exampleClass); | ||
| Example example = new Example(); | ||
| try (InputStream stream = ApiServlet.class.getClassLoader().getResourceAsStream(path)) { | ||
| if (stream == null) { | ||
| throw new IllegalArgumentException("Unable to find example file: " + path); | ||
| } | ||
| String ex = IOUtils.toString(stream, StandardCharsets.UTF_8); | ||
| ex = Formats.format(contentType, Formats.parseContent(contentType, ex, exampleClass)); | ||
| example.value(ex); | ||
| } catch (IOException ex) { | ||
| throw new IllegalStateException("Unable to load example file: " + path, ex); | ||
| } | ||
| return example; | ||
| } | ||
|
|
||
| // Enum to define example configurations by endpoint | ||
| // Add examples here for other endpoints | ||
| private enum EndpointExamples { | ||
| LEVELS(Arrays.asList( | ||
| new ExampleConfig(LocationLevel.class, "Constant Location Level", | ||
| ConstantLocationLevel.class, "cwms/cda/data/levels/levels_constant_create.json"), | ||
| new ExampleConfig(LocationLevel.class, "Seasonal Location Level", | ||
| SeasonalLocationLevel.class, "cwms/cda/data/levels/levels_seasonal_create.json"), | ||
| new ExampleConfig(LocationLevel.class, "Timeseries Location Level", | ||
| TimeSeriesLocationLevel.class, "cwms/cda/data/levels/levels_timeseries_create.json"), | ||
| new ExampleConfig(LocationLevel.class, "Virtual Location Level", | ||
| VirtualLocationLevel.class, "cwms/cda/data/levels/levels_virtual_create.json") | ||
| )); | ||
| // Add more endpoints as needed | ||
|
|
||
| private final List<ExampleConfig> examples; | ||
|
|
||
| EndpointExamples(List<ExampleConfig> examples) { | ||
| this.examples = examples; | ||
| } | ||
|
|
||
| private List<ExampleConfig> getExamples() { | ||
| return examples; | ||
| } | ||
| } | ||
|
|
||
| private static class ExampleConfig { | ||
| private final Class<? extends CwmsDTOBase> targetClass; | ||
| private final String displayName; | ||
| private final Class<? extends CwmsDTOBase> exampleClass; | ||
| private final String resourcePath; | ||
|
|
||
| private ExampleConfig(Class<? extends CwmsDTOBase> targetClass, String displayName, | ||
| Class<? extends CwmsDTOBase> exampleClass, String resourcePath) { | ||
| this.targetClass = targetClass; | ||
| this.displayName = displayName; | ||
| this.exampleClass = exampleClass; | ||
| this.resourcePath = resourcePath; | ||
| } | ||
| } | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
cwms-data-api/src/main/resources/cwms/cda/data/levels/levels_constant_create.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "constant-value": 10, | ||
| "level-units-id": "ft", | ||
| "level-date": "2008-12-03T10:15:30+01:00[UTC]", | ||
| "level-comment": "Lowest Point", | ||
| "interpolate-string": "F", | ||
| "location-level-id": "Sacramento.Elev.Inst.0.Bottom of Inlet", | ||
| "office-id": "LRL" | ||
| } |
21 changes: 21 additions & 0 deletions
21
cwms-data-api/src/main/resources/cwms/cda/data/levels/levels_seasonal_create.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "seasonal-values": [ | ||
| { | ||
| "value": 10, | ||
| "offset-minutes": 0, | ||
| "offset-months": 0 | ||
| }, { | ||
| "value": 15, | ||
| "offset-minutes": 0, | ||
| "offset-months": 1 | ||
| } | ||
| ], | ||
| "level-units-id": "ft", | ||
| "level-date": "2008-12-03T10:15:30+01:00[UTC]", | ||
| "level-comment": "Lowest Point", | ||
| "interval-origin": "2008-12-03T10:15:30+01:00[UTC]", | ||
| "interval-months": 1, | ||
| "interpolate-string": "F", | ||
| "location-level-id": "Sacramento.Elev.Inst.0.Bottom of Inlet", | ||
| "office-id": "LRL" | ||
| } |
9 changes: 9 additions & 0 deletions
9
cwms-data-api/src/main/resources/cwms/cda/data/levels/levels_timeseries_create.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "seasonal-time-series-id": "Sacramento.Stage.Inst.5Minutes.0.Bottom of Inlet", | ||
| "level-units-id": "ft", | ||
| "level-date": "2008-12-03T10:15:30+01:00[UTC]", | ||
| "level-comment": "Bottom of Inlet, lowest level", | ||
| "interpolate-string": "F", | ||
| "location-level-id": "Sacramento.Elev.Inst.0.Bottom of Inlet", | ||
| "office-id": "LRL" | ||
| } |
28 changes: 28 additions & 0 deletions
28
cwms-data-api/src/main/resources/cwms/cda/data/levels/levels_virtual_create.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
| "office-id": "SPK", | ||
| "location-level-id": "Sacramento.Stage.Ave.1Day.Top of Normal", | ||
| "specified-level-id": "Top of Normal", | ||
| "parameter-id": "Stage", | ||
| "attribute-value": 12.5, | ||
| "attribute-units-id": "ft", | ||
| "attribute-parameter-id": "Stage", | ||
| "attribute-parameter-type-id": "Inst", | ||
| "level-units-id": "ft", | ||
| "level-date": "2023-06-01T00:00:00-07:00", | ||
| "expiration-date": "2025-06-01T00:00:00-07:00", | ||
| "constituents": [ | ||
| { | ||
| "type": "LOCATION_LEVEL", | ||
| "abbr": "L1", | ||
| "name": "Sonoma.Stage.Ave.1Day.Top of Normal", | ||
| "attribute-id": "Stage", | ||
| "attribute-value": 16.5 | ||
| }, | ||
| { | ||
| "type": "RATING", | ||
| "abbr": "R1", | ||
| "name": "Sacramento.Stage;Flow.COE.Production" | ||
| } | ||
| ], | ||
| "constituent-connections": "L1=R1I1" | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is certainly fine for now... but let's all agree this will grow insanely and longer term we probably want something like annotations on the DTOs that can be looked up and contain links to relevant data.