Skip to content

Commit

Permalink
add a structured type to batchGet in OpenAPI V3 spec
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin1chun committed Jul 19, 2024
1 parent 44cdb04 commit df460bc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -645,28 +659,19 @@ private static Schema buildEntityScrollSchema(final EntitySpec entity) {
private static Schema buildEntityBatchGetRequestSchema(
final EntitySpec entity, Set<String> aspectNames) {

final Schema stringTypeSchema = new Schema<>();
stringTypeSchema.setType(TYPE_STRING);
final Map<String, Schema> headers =
Map.of(
"headers",
new Schema<>()
.type(TYPE_OBJECT)
.additionalProperties(stringTypeSchema)
.description("System headers for the operation.")
.nullable(true));

final Map<String, Schema> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Schema> 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()));
}
}

0 comments on commit df460bc

Please sign in to comment.