-
Notifications
You must be signed in to change notification settings - Fork 0
Add schema description endpoint #281
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 25 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
543008d
jsonschema for types
Tristan-WorkGH 2174d87
Add endpoint
Tristan-WorkGH 150d190
Merge main
45b9281
Add schema description endpoint
b92aa91
Add failIfNoClassesMatch
56f7fa9
bis
16aad65
Add junit-bom back
232179c
Use mockbean instead of spybean
25afeac
checkstyle
e58b368
rollback mockbean
5966ef9
checkstyle
1995670
add check at SchemaService construction
c7de41f
Fix branches tests
d67aff8
Update path instead of move files
badc323
Undo
de97b43
Remove pom comments
feda1d9
Merge remote-tracking branch 'origin/main' into add-dto-schema
0c0b6ce
Add null guard
9c1204e
checkstyle
c460eb9
test
9bd7dcd
Licence and unused variable
cf0d286
Tweak comment
7543d5a
Switch to jackson jsonSchema
a0ae938
Merge main
535ae70
Clean tests
8901e03
Merge remote-tracking branch 'origin/main' into add-dto-schema
c9637f1
Add jackson json schema version to pom properties
d4d1209
various fixes
3bfd4f5
Remove useless config
2525cbe
Reorganize deps position
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
19 changes: 19 additions & 0 deletions
19
src/main/java/org/gridsuite/network/map/NetworkMapApi.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,19 @@ | ||
/** | ||
* Copyright (c) 2025, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package org.gridsuite.network.map; | ||
|
||
/** | ||
* @author Hugo Marcellin <hugo.marcelin at rte-france.com> | ||
*/ | ||
|
||
public final class NetworkMapApi { | ||
|
||
private NetworkMapApi() { | ||
} | ||
|
||
public static final String API_VERSION = "v1"; | ||
} |
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
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
45 changes: 45 additions & 0 deletions
45
src/main/java/org/gridsuite/network/map/SchemaController.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,45 @@ | ||
/** | ||
* Copyright (c) 2025, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package org.gridsuite.network.map; | ||
|
||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.module.jsonSchema.JsonSchema; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.AllArgsConstructor; | ||
import org.gridsuite.network.map.dto.ElementInfos.InfoType; | ||
import org.gridsuite.network.map.dto.ElementType; | ||
import org.gridsuite.network.map.services.SchemaService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
@RestController | ||
@RequestMapping(value = "/" + NetworkMapApi.API_VERSION + "/schemas") | ||
@Tag(name = "Network map server - Schemas") | ||
@AllArgsConstructor | ||
public class SchemaController { | ||
public static final String APPLICATION_JSON_SCHEMA_VALUE = "application/schema+json"; | ||
private final SchemaService schemaService; | ||
|
||
@GetMapping(value = "/{elementType}/{infoType}", produces = APPLICATION_JSON_SCHEMA_VALUE) | ||
@Operation(summary = "Get network elements") | ||
TheMaskedTurtle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
@ApiResponse(responseCode = "200", description = "Elements description") | ||
TheMaskedTurtle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
public JsonSchema getElementSchema(@Parameter(description = "Element type") @PathVariable(name = "elementType") ElementType elementType, | ||
@Parameter(description = "Info type") @PathVariable(name = "infoType") InfoType infoType) { | ||
TheMaskedTurtle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
try { | ||
return schemaService.getSchema(elementType, infoType); | ||
} catch (final UnsupportedOperationException | JsonMappingException ex) { | ||
throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "The view " + infoType + " for " + elementType + " type is not available yet."); | ||
} | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
src/main/java/org/gridsuite/network/map/services/SchemaService.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,76 @@ | ||
/** | ||
* Copyright (c) 2025, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package org.gridsuite.network.map.services; | ||
|
||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.module.jsonSchema.JsonSchema; | ||
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator; | ||
import lombok.AllArgsConstructor; | ||
import lombok.NonNull; | ||
import org.gridsuite.network.map.dto.ElementInfos.InfoType; | ||
import org.gridsuite.network.map.dto.ElementType; | ||
import org.gridsuite.network.map.dto.definition.battery.BatteryTabInfos; | ||
import org.gridsuite.network.map.dto.definition.branch.BranchTabInfos; | ||
import org.gridsuite.network.map.dto.definition.branch.line.LineTabInfos; | ||
import org.gridsuite.network.map.dto.definition.branch.twowindingstransformer.TwoWindingsTransformerTabInfos; | ||
import org.gridsuite.network.map.dto.definition.bus.BusTabInfos; | ||
import org.gridsuite.network.map.dto.definition.busbarsection.BusBarSectionTabInfos; | ||
import org.gridsuite.network.map.dto.definition.danglingline.DanglingLineTabInfos; | ||
import org.gridsuite.network.map.dto.definition.generator.GeneratorTabInfos; | ||
import org.gridsuite.network.map.dto.definition.hvdc.HvdcTabInfos; | ||
import org.gridsuite.network.map.dto.definition.lccconverterstation.LccConverterStationTabInfos; | ||
import org.gridsuite.network.map.dto.definition.load.LoadTabInfos; | ||
import org.gridsuite.network.map.dto.definition.shuntcompensator.ShuntCompensatorTabInfos; | ||
import org.gridsuite.network.map.dto.definition.staticvarcompensator.StaticVarCompensatorTabInfos; | ||
import org.gridsuite.network.map.dto.definition.substation.SubstationTabInfos; | ||
import org.gridsuite.network.map.dto.definition.threewindingstransformer.ThreeWindingsTransformerTabInfos; | ||
import org.gridsuite.network.map.dto.definition.tieline.TieLineTabInfos; | ||
import org.gridsuite.network.map.dto.definition.voltagelevel.VoltageLevelTabInfos; | ||
import org.gridsuite.network.map.dto.definition.vscconverterstation.VscConverterStationTabInfos; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@AllArgsConstructor | ||
public class SchemaService { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
/** | ||
* @apiNote use class instance to be more secure with enum and classes rename/moving/etc with IDE | ||
*/ | ||
private static Class<?> getTabInfosClass(final ElementType elementType) { | ||
return switch (elementType) { | ||
case BATTERY -> BatteryTabInfos.class; | ||
case BUS -> BusTabInfos.class; | ||
case BUSBAR_SECTION -> BusBarSectionTabInfos.class; | ||
case DANGLING_LINE -> DanglingLineTabInfos.class; | ||
case GENERATOR -> GeneratorTabInfos.class; | ||
case HVDC_LINE, HVDC_LINE_LCC, HVDC_LINE_VSC -> HvdcTabInfos.class; | ||
case LCC_CONVERTER_STATION -> LccConverterStationTabInfos.class; | ||
case LINE -> LineTabInfos.class; | ||
case LOAD -> LoadTabInfos.class; | ||
case SHUNT_COMPENSATOR -> ShuntCompensatorTabInfos.class; | ||
case STATIC_VAR_COMPENSATOR -> StaticVarCompensatorTabInfos.class; | ||
case SUBSTATION -> SubstationTabInfos.class; | ||
case THREE_WINDINGS_TRANSFORMER -> ThreeWindingsTransformerTabInfos.class; | ||
case TIE_LINE -> TieLineTabInfos.class; | ||
case TWO_WINDINGS_TRANSFORMER -> TwoWindingsTransformerTabInfos.class; | ||
case VOLTAGE_LEVEL -> VoltageLevelTabInfos.class; | ||
case VSC_CONVERTER_STATION -> VscConverterStationTabInfos.class; | ||
case BRANCH -> BranchTabInfos.class; | ||
}; | ||
} | ||
|
||
public JsonSchema getSchema(@NonNull final ElementType elementType, @NonNull final InfoType infoType) throws JsonMappingException { | ||
if (infoType != InfoType.TAB) { | ||
throw new UnsupportedOperationException("This info type is not currently supported."); | ||
} | ||
JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(objectMapper); | ||
return schemaGen.generateSchema(getTabInfosClass(elementType)); | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
src/test/java/org/gridsuite/network/map/SchemaControllerTest.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,70 @@ | ||
/** | ||
* Copyright (c) 2025, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package org.gridsuite.network.map; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ObjectWriter; | ||
import com.fasterxml.jackson.module.jsonSchema.JsonSchema; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.gridsuite.network.map.dto.ElementInfos.InfoType; | ||
import org.gridsuite.network.map.dto.ElementType; | ||
import org.gridsuite.network.map.services.SchemaService; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.SpyBean; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.log; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@WebMvcTest(SchemaController.class) | ||
class SchemaControllerTest { | ||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@SpyBean | ||
private SchemaService schemaService; | ||
|
||
private static Stream<Arguments> schemaRequestValues() { | ||
final List<Pair<ElementType, InfoType>> cases = new ArrayList<>(); | ||
for (ElementType elementType : ElementType.values()) { | ||
for (InfoType infoType : InfoType.values()) { | ||
cases.add(Pair.of(elementType, infoType)); | ||
} | ||
} | ||
return cases.stream().map(e1 -> Arguments.of(e1.getKey(), e1.getValue())); | ||
} | ||
|
||
@ParameterizedTest(name = "{0} (view {1})") | ||
@MethodSource("schemaRequestValues") | ||
void schemaRequest(@NonNull final ElementType eType, @NonNull final InfoType iType) throws Exception { | ||
ResultActions result = this.mockMvc.perform(get("/v1/schemas/{eType}/{iType}", eType, iType)).andDo(log()); | ||
if (iType.equals(InfoType.TAB)) { | ||
JsonSchema schema = schemaService.getSchema(eType, iType); | ||
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); | ||
String json = ow.writeValueAsString(schema); | ||
result.andExpectAll( | ||
status().isOk(), | ||
content().contentType(SchemaController.APPLICATION_JSON_SCHEMA_VALUE), | ||
content().json(json) | ||
); | ||
} else { | ||
result.andExpect(status().isNotImplemented()); | ||
} | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.