From c004a715595f73bf919168f0e03aa2544b17b149 Mon Sep 17 00:00:00 2001 From: Kevin Chun Date: Wed, 12 Jun 2024 14:10:48 -0700 Subject: [PATCH] Aspect refs inside entity schema are nullable (#10695) LGTM! --- .../openapi/v3/OpenAPIV3Generator.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 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 4966e618a16435..86b03ccc467d93 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 @@ -539,14 +539,23 @@ private static Schema buildEntityScrollSchema(final EntitySpec entity) { } private static Schema buildAspectRef(final String aspect, final boolean withSystemMetadata) { + // A non-required $ref property must be wrapped in a { allOf: [ $ref ] } + // object to allow the + // property to be marked as nullable final Schema result = new Schema<>(); + + result.setType(TYPE_OBJECT); + result.set$ref(null); + result.setNullable(true); + final String internalRef; if (withSystemMetadata) { - result.set$ref( - String.format(FORMAT_PATH_DEFINITIONS, toUpperFirst(aspect), ASPECT_RESPONSE_SUFFIX)); + internalRef = + String.format(FORMAT_PATH_DEFINITIONS, toUpperFirst(aspect), ASPECT_RESPONSE_SUFFIX); } else { - result.set$ref( - String.format(FORMAT_PATH_DEFINITIONS, toUpperFirst(aspect), ASPECT_REQUEST_SUFFIX)); + internalRef = + String.format(FORMAT_PATH_DEFINITIONS, toUpperFirst(aspect), ASPECT_REQUEST_SUFFIX); } + result.setAllOf(List.of(new Schema().$ref(internalRef))); return result; }