Skip to content

Commit

Permalink
bugfix: use anyOf instead of allOf when creating references in openap…
Browse files Browse the repository at this point in the history
…i v3 spec (#10986)
  • Loading branch information
kevin1chun authored Jul 24, 2024
1 parent 959d351 commit 725df58
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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;
Expand All @@ -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));

Expand Down Expand Up @@ -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<>();
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit 725df58

Please sign in to comment.