Skip to content

Commit 6098e97

Browse files
fix(openapi-schema): fix openapi schema generator (#12590)
1 parent 15ac294 commit 6098e97

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

metadata-service/openapi-servlet/src/main/java/io/datahubproject/openapi/v3/OpenAPIV3Generator.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ public static OpenAPI generateOpenApiSpec(
114114

115115
// --> Aspect components
116116
components.addSchemas(
117-
ASPECTS + ASPECT_RESPONSE_SUFFIX, buildAspectsRefResponseSchema(entityRegistry));
117+
ASPECTS + ASPECT_RESPONSE_SUFFIX,
118+
buildAspectsRefResponseSchema(entityRegistry, definitionNames));
118119
entityRegistry
119120
.getAspectSpecs()
120121
.values()
@@ -724,7 +725,8 @@ private static void addAspectSchemas(final Components components, final AspectSp
724725
* @param entityRegistry entity registry
725726
* @return schema
726727
*/
727-
private static Schema buildAspectsRefResponseSchema(final EntityRegistry entityRegistry) {
728+
private static Schema buildAspectsRefResponseSchema(
729+
final EntityRegistry entityRegistry, final Set<String> definitionNames) {
728730
final Schema result =
729731
new Schema<>()
730732
.type(TYPE_OBJECT)
@@ -734,7 +736,11 @@ private static Schema buildAspectsRefResponseSchema(final EntityRegistry entityR
734736
// Create a list of reference schemas for each aspect
735737
List<Schema> aspectRefs =
736738
entityRegistry.getAspectSpecs().values().stream()
737-
.map(aspect -> new Schema<>().$ref(PATH_DEFINITIONS + toUpperFirst(aspect.getName())))
739+
.filter(a -> definitionNames.contains(a.getName()))
740+
.map(
741+
aspect ->
742+
new Schema<>()
743+
.$ref(PATH_DEFINITIONS + toUpperFirst(aspect.getPegasusSchema().getName())))
738744
.distinct()
739745
.collect(Collectors.toList());
740746

metadata-service/openapi-servlet/src/test/java/io/datahubproject/openapi/v3/OpenAPIV3GeneratorTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,16 @@ public void testOpenApiSpecBuilder() throws Exception {
130130
Character.isUpperCase(refName.charAt(0)),
131131
"schema reference should start with capital letter: " + name);
132132
});
133+
134+
// Check for pegasus name example
135+
assertTrue(
136+
valueProperty.getOneOf().stream()
137+
.anyMatch(
138+
schema ->
139+
schema.get$ref().equals("#/components/schemas/StructuredPropertyDefinition")));
140+
assertTrue(
141+
valueProperty.getOneOf().stream()
142+
.noneMatch(
143+
schema -> schema.get$ref().equals("#/components/schemas/PropertyDefinition")));
133144
}
134145
}

0 commit comments

Comments
 (0)