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 df3f6445a855a0..4966e618a16435 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 @@ -437,27 +437,31 @@ private static void addAspectSchemas(final Components components, final AspectSp final String newDefinition = definition.replaceAll("definitions", "components/schemas"); Schema s = Json.mapper().readValue(newDefinition, Schema.class); - Set requiredNames = - Optional.ofNullable(s.getRequired()) - .map(names -> Set.copyOf(names)) - .orElse(new HashSet()); - Map properties = - Optional.ofNullable(s.getProperties()).orElse(new HashMap<>()); - properties.forEach( - (name, schema) -> { - 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 ] } - // 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.setNullable(!isNameRequired); - }); - + // Set enums to "string". + if (s.getEnum() != null && !s.getEnum().isEmpty()) { + s.setType("string"); + } else { + Set requiredNames = + Optional.ofNullable(s.getRequired()) + .map(names -> Set.copyOf(names)) + .orElse(new HashSet()); + Map properties = + Optional.ofNullable(s.getProperties()).orElse(new HashMap<>()); + properties.forEach( + (name, schema) -> { + 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 ] } + // 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.setNullable(!isNameRequired); + }); + } components.addSchemas(n, s); } catch (Exception e) { throw new RuntimeException(e); 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 0ce62f5cb10f64..10b75fd7faed37 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 @@ -83,5 +83,10 @@ public void testOpenApiSpecBuilder() throws Exception { List.of(new Schema().$ref("#/components/schemas/SystemMetadata")), systemMetadata.getAllOf()); assertTrue(systemMetadata.getNullable()); + + // Assert enum property is string. + Schema fabricType = openAPI.getComponents().getSchemas().get("FabricType"); + assertEquals("string", fabricType.getType()); + assertFalse(fabricType.getEnum().isEmpty()); } }