From df460bca1eac60520bb1963150337dc77fbcee91 Mon Sep 17 00:00:00 2001 From: Kevin Chun Date: Fri, 19 Jul 2024 13:00:23 -0700 Subject: [PATCH] add a structured type to batchGet in OpenAPI V3 spec --- .../resources/MetadataChangeProposal.avsc | 8 ++--- .../openapi/v3/OpenAPIV3Generator.java | 31 +++++++++++-------- .../openapi/v3/OpenAPIV3GeneratorTest.java | 13 ++++++++ 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/metadata-integration/java/datahub-event/src/main/resources/MetadataChangeProposal.avsc b/metadata-integration/java/datahub-event/src/main/resources/MetadataChangeProposal.avsc index 126d5cf6bcee38..6f2cc121e60a15 100644 --- a/metadata-integration/java/datahub-event/src/main/resources/MetadataChangeProposal.avsc +++ b/metadata-integration/java/datahub-event/src/main/resources/MetadataChangeProposal.avsc @@ -1,14 +1,14 @@ { "type" : "record", "name" : "MetadataChangeProposal", - "namespace" : "com.linkedin.pegasus2avro.mxe", + "namespace" : "com.linkedin.mxe", "doc" : "Kafka event for proposing a metadata change for an entity. A corresponding MetadataChangeLog is emitted when the change is accepted and committed, otherwise a FailedMetadataChangeProposal will be emitted instead.", "fields" : [ { "name" : "auditHeader", "type" : [ "null", { "type" : "record", "name" : "KafkaAuditHeader", - "namespace" : "com.linkedin.events", + "namespace" : "com.linkedin.avro2pegasus.events", "doc" : "This header records information about the context of an event as it is emitted into kafka and is intended to be used by the kafka audit application. For more information see go/kafkaauditheader", "fields" : [ { "name" : "time", @@ -74,7 +74,7 @@ "doc" : "Urn of the entity being written", "default" : null, "java" : { - "class" : "com.linkedin.pegasus2avro.common.urn.Urn" + "class" : "com.linkedin.common.urn.Urn" } }, { "name" : "entityKeyAspect", @@ -99,7 +99,7 @@ "type" : { "type" : "enum", "name" : "ChangeType", - "namespace" : "com.linkedin.pegasus2avro.events.metadata", + "namespace" : "com.linkedin.events.metadata", "doc" : "Descriptor for a change action", "symbols" : [ "UPSERT", "CREATE", "UPDATE", "DELETE", "PATCH", "RESTATE", "CREATE_ENTITY" ], "symbolDocs" : { 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 3a93eb304b8f80..f423be82d6e8dc 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 @@ -82,6 +82,20 @@ public static OpenAPI generateOpenApiSpec(EntityRegistry entityRegistry) { "SystemMetadata", new Schema().type(TYPE_OBJECT).additionalProperties(true)); components.addSchemas("SortOrder", new Schema()._enum(List.of("ASCENDING", "DESCENDING"))); components.addSchemas("AspectPatch", buildAspectPatchSchema()); + components.addSchemas( + "BatchGetRequestBody", + new Schema<>() + .type(TYPE_OBJECT) + .description("Request body for batch get aspects.") + .properties( + Map.of( + "headers", + new Schema<>() + .type(TYPE_OBJECT) + .additionalProperties(new Schema<>().type(TYPE_STRING)) + .description("System headers for the operation.") + .nullable(true))) + .nullable(true)); entityRegistry .getAspectSpecs() .values() @@ -645,28 +659,19 @@ private static Schema buildEntityScrollSchema(final EntitySpec entity) { private static Schema buildEntityBatchGetRequestSchema( final EntitySpec entity, Set aspectNames) { - final Schema stringTypeSchema = new Schema<>(); - stringTypeSchema.setType(TYPE_STRING); - final Map headers = - Map.of( - "headers", - new Schema<>() - .type(TYPE_OBJECT) - .additionalProperties(stringTypeSchema) - .description("System headers for the operation.") - .nullable(true)); - final Map properties = entity.getAspectSpecMap().entrySet().stream() .filter(a -> aspectNames.contains(a.getValue().getName())) .collect( Collectors.toMap( - Map.Entry::getKey, a -> new Schema().type(TYPE_OBJECT).properties(headers))); + Map.Entry::getKey, + a -> new Schema().$ref("#/components/schemas/BatchGetRequestBody"))); properties.put( PROPERTY_URN, new Schema<>().type(TYPE_STRING).description("Unique id for " + entity.getName())); - properties.put(entity.getKeyAspectName(), new Schema().type(TYPE_OBJECT).properties(headers)); + properties.put( + entity.getKeyAspectName(), new Schema().$ref("#/components/schemas/BatchGetRequestBody")); return new Schema<>() .type(TYPE_OBJECT) 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 10b75fd7faed37..b0fbbce05a0f8e 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 @@ -88,5 +88,18 @@ public void testOpenApiSpecBuilder() throws Exception { Schema fabricType = openAPI.getComponents().getSchemas().get("FabricType"); assertEquals("string", fabricType.getType()); assertFalse(fabricType.getEnum().isEmpty()); + + Map batchProperties = + openAPI + .getComponents() + .getSchemas() + .get("BatchGetContainerEntityRequest_v3") + .getProperties(); + batchProperties.entrySet().stream() + .filter(entry -> !entry.getKey().equals("urn")) + .forEach( + entry -> + assertEquals( + "#/components/schemas/BatchGetRequestBody", entry.getValue().get$ref())); } }