From 725df58893bf17af24c934a5c984b66f11672b39 Mon Sep 17 00:00:00 2001 From: Kevin Chun Date: Wed, 24 Jul 2024 14:51:10 -0700 Subject: [PATCH] bugfix: use anyOf instead of allOf when creating references in openapi v3 spec (#10986) --- .../openapi/v3/OpenAPIV3Generator.java | 12 ++++++------ .../openapi/v3/OpenAPIV3GeneratorTest.java | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/metadata-service/openapi-servlet/src/main/java/io/datahubproject/openapi/v3/OpenAPIV3Generator.java b/metadata-service/openapi-servlet/src/main/java/io/datahubproject/openapi/v3/OpenAPIV3Generator.java index f423be82d6e8dc..f26ad6821c5833 100644 --- a/metadata-service/openapi-servlet/src/main/java/io/datahubproject/openapi/v3/OpenAPIV3Generator.java +++ b/metadata-service/openapi-servlet/src/main/java/io/datahubproject/openapi/v3/OpenAPIV3Generator.java @@ -547,12 +547,12 @@ private static void addAspectSchemas(final Components components, final AspectSp String $ref = schema.get$ref(); boolean isNameRequired = requiredNames.contains(name); if ($ref != null && !isNameRequired) { - // A non-required $ref property must be wrapped in a { allOf: [ $ref ] } + // A non-required $ref property must be wrapped in a { anyOf: [ $ref ] } // object to allow the // property to be marked as nullable schema.setType(TYPE_OBJECT); schema.set$ref(null); - schema.setAllOf(List.of(new Schema().$ref($ref))); + schema.setAnyOf(List.of(new Schema().$ref($ref))); } schema.setNullable(!isNameRequired); }); @@ -578,7 +578,7 @@ private static Schema buildAspectRefResponseSchema(final String aspectName) { "systemMetadata", new Schema<>() .type(TYPE_OBJECT) - .allOf(List.of(new Schema().$ref(PATH_DEFINITIONS + "SystemMetadata"))) + .anyOf(List.of(new Schema().$ref(PATH_DEFINITIONS + "SystemMetadata"))) .description("System metadata for the aspect.") .nullable(true)); return result; @@ -595,7 +595,7 @@ private static Schema buildAspectRefRequestSchema(final String aspectName) { "systemMetadata", new Schema<>() .type(TYPE_OBJECT) - .allOf(List.of(new Schema().$ref(PATH_DEFINITIONS + "SystemMetadata"))) + .anyOf(List.of(new Schema().$ref(PATH_DEFINITIONS + "SystemMetadata"))) .description("System metadata for the aspect.") .nullable(true)); @@ -681,7 +681,7 @@ private static Schema buildEntityBatchGetRequestSchema( } private static Schema buildAspectRef(final String aspect, final boolean withSystemMetadata) { - // A non-required $ref property must be wrapped in a { allOf: [ $ref ] } + // A non-required $ref property must be wrapped in a { anyOf: [ $ref ] } // object to allow the // property to be marked as nullable final Schema result = new Schema<>(); @@ -697,7 +697,7 @@ private static Schema buildAspectRef(final String aspect, final boolean withSyst internalRef = String.format(FORMAT_PATH_DEFINITIONS, toUpperFirst(aspect), ASPECT_REQUEST_SUFFIX); } - result.setAllOf(List.of(new Schema().$ref(internalRef))); + result.setAnyOf(List.of(new Schema().$ref(internalRef))); return result; } diff --git a/metadata-service/openapi-servlet/src/test/java/io/datahubproject/openapi/v3/OpenAPIV3GeneratorTest.java b/metadata-service/openapi-servlet/src/test/java/io/datahubproject/openapi/v3/OpenAPIV3GeneratorTest.java index b0fbbce05a0f8e..e1568017156d9b 100644 --- a/metadata-service/openapi-servlet/src/test/java/io/datahubproject/openapi/v3/OpenAPIV3GeneratorTest.java +++ b/metadata-service/openapi-servlet/src/test/java/io/datahubproject/openapi/v3/OpenAPIV3GeneratorTest.java @@ -61,12 +61,12 @@ public void testOpenApiSpecBuilder() throws Exception { assertFalse(requiredNames.contains("name")); assertTrue(name.getNullable()); - // Assert non-required $ref properties are replaced by nullable { allOf: [ $ref ] } objects + // Assert non-required $ref properties are replaced by nullable { anyOf: [ $ref ] } objects Schema created = properties.get("created"); assertFalse(requiredNames.contains("created")); assertEquals("object", created.getType()); assertNull(created.get$ref()); - assertEquals(List.of(new Schema().$ref("#/components/schemas/TimeStamp")), created.getAllOf()); + assertEquals(List.of(new Schema().$ref("#/components/schemas/TimeStamp")), created.getAnyOf()); assertTrue(created.getNullable()); // Assert systemMetadata property on response schema is optional. @@ -81,7 +81,7 @@ public void testOpenApiSpecBuilder() throws Exception { assertNull(systemMetadata.get$ref()); assertEquals( List.of(new Schema().$ref("#/components/schemas/SystemMetadata")), - systemMetadata.getAllOf()); + systemMetadata.getAnyOf()); assertTrue(systemMetadata.getNullable()); // Assert enum property is string.