Skip to content

Commit

Permalink
fix(openapi-spec): fix openapi spec oneOf schema (#12561)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-leifker authored Feb 6, 2025
1 parent 0ed3d7f commit 65376ee
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -731,13 +731,16 @@ private static Schema buildAspectsRefResponseSchema(final EntityRegistry entityR
.description(ASPECT_DESCRIPTION)
.required(List.of(PROPERTY_VALUE));

entityRegistry
.getAspectSpecs()
.values()
.forEach(
aspect ->
result.addProperty(
PROPERTY_VALUE, new Schema<>().$ref(PATH_DEFINITIONS + aspect.getName())));
// Create a list of reference schemas for each aspect
List<Schema> aspectRefs =
entityRegistry.getAspectSpecs().values().stream()
.map(aspect -> new Schema<>().$ref(PATH_DEFINITIONS + toUpperFirst(aspect.getName())))
.distinct()
.collect(Collectors.toList());

// Add the value property with oneOf constraint
result.addProperty(PROPERTY_VALUE, new Schema<>().oneOf(aspectRefs));

result.addProperty(
NAME_SYSTEM_METADATA,
new Schema<>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,30 @@ public void testOpenApiSpecBuilder() throws Exception {
entry ->
assertEquals(
"#/components/schemas/BatchGetRequestBody", entry.getValue().get$ref()));

// Assert aspect response schemas have value property with oneOf references
Schema<?> aspectResponseSchema =
openAPI.getComponents().getSchemas().get("AspectsAspectResponse_v3");
Schema<?> valueProperty = aspectResponseSchema.getProperties().get("value");
assertTrue(
valueProperty.getOneOf() != null && !valueProperty.getOneOf().isEmpty(),
"value property should use oneOf");

// Check each reference has proper format and capitalization
valueProperty
.getOneOf()
.forEach(
schema -> {
String ref = schema.get$ref();
assertTrue(
ref != null && ref.startsWith("#/components/schemas/"),
"reference should start with '#/components/schemas/': " + ref);

// Extract the last part after the last slash and check first character
String refName = ref.substring(ref.lastIndexOf('/') + 1);
assertTrue(
Character.isUpperCase(refName.charAt(0)),
"schema reference should start with capital letter: " + name);
});
}
}

0 comments on commit 65376ee

Please sign in to comment.