fix(openapi-spec): fix openapi spec oneOf schema (#12561)

This commit is contained in:
david-leifker 2025-02-05 18:17:53 -06:00 committed by GitHub
parent 0ed3d7f4a4
commit 65376ee2d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 7 deletions

View File

@ -731,13 +731,16 @@ public class OpenAPIV3Generator {
.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<>()

View File

@ -105,5 +105,30 @@ public class OpenAPIV3GeneratorTest {
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);
});
}
}