Skip to content

Support allOf schemas referring to oneOf schemas #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.XML;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import org.openapitools.openapidiff.core.compare.schemadiffresult.ArraySchemaDiffResult;
import org.openapitools.openapidiff.core.compare.schemadiffresult.ComposedSchemaDiffResult;
import org.openapitools.openapidiff.core.compare.schemadiffresult.SchemaDiffResult;
Expand Down Expand Up @@ -79,17 +81,26 @@ public static SchemaDiffResult getSchemaDiffResult(
}
}

protected static Schema<?> resolveComposedSchema(Components components, Schema<?> schema) {
protected static Schema<?> resolveComposedSchema(
Components components, Schema<?> schema, Set<String> visitedRefs) {
if (schema instanceof ComposedSchema) {
ComposedSchema composedSchema = (ComposedSchema) schema;
List<Schema> allOfSchemaList = composedSchema.getAllOf();
if (allOfSchemaList != null) {
for (Schema<?> allOfSchema : allOfSchemaList) {
allOfSchema = refPointer.resolveRef(components, allOfSchema, allOfSchema.get$ref());
allOfSchema = resolveComposedSchema(components, allOfSchema);
schema = addSchema(schema, allOfSchema);
List<Schema> composedSchemas = new ArrayList<>();
Optional.ofNullable(composedSchema.getAllOf()).ifPresent(composedSchemas::addAll);
Optional.ofNullable(composedSchema.getAnyOf()).ifPresent(composedSchemas::addAll);

if (!composedSchemas.isEmpty()) {
for (Schema<?> composed : composedSchemas) {
if (composed.get$ref() == null || !visitedRefs.contains(composed.get$ref())) {
Set<String> updatedVisitedRefs = new HashSet<>(visitedRefs);
updatedVisitedRefs.add(composed.get$ref());
composed = refPointer.resolveRef(components, composed, composed.get$ref());
composed = resolveComposedSchema(components, composed, updatedVisitedRefs);
schema = addSchema(schema, composed);
}
}
composedSchema.setAllOf(null);
composedSchema.setAnyOf(null);
}
}
return schema;
Expand Down Expand Up @@ -154,6 +165,16 @@ protected static Schema<?> addSchema(Schema<?> schema, Schema<?> fromSchema) {
}
schema.getExtensions().putAll(fromSchema.getExtensions());
}
if (fromSchema instanceof ComposedSchema && schema instanceof ComposedSchema) {
ComposedSchema composedFromSchema = (ComposedSchema) fromSchema;
ComposedSchema composedSchema = (ComposedSchema) schema;
if (composedFromSchema.getOneOf() != null) {
if (composedSchema.getOneOf() == null) {
composedSchema.setOneOf(new ArrayList<>());
}
composedSchema.getOneOf().addAll(composedFromSchema.getOneOf());
}
}
if (fromSchema.getDiscriminator() != null) {
if (schema.getDiscriminator() == null) {
schema.setDiscriminator(new Discriminator());
Expand Down Expand Up @@ -316,8 +337,8 @@ public DeferredChanged<ChangedSchema> computeDiffForReal(
left = refPointer.resolveRef(this.leftComponents, left, getSchemaRef(left));
right = refPointer.resolveRef(this.rightComponents, right, getSchemaRef(right));

left = resolveComposedSchema(leftComponents, left);
right = resolveComposedSchema(rightComponents, right);
left = resolveComposedSchema(leftComponents, left, new HashSet<>());
right = resolveComposedSchema(rightComponents, right, new HashSet<>());

// If type of schemas are different, just set old & new schema, set changedType to true in
// SchemaDiffResult and
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.openapitools.openapidiff.core;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.openapitools.openapidiff.core.model.ChangedOpenApi;

public class AllOfOneOfDiffTest {
@Test
void allOfReferringToOneOfSchemasAreSupported() {
ChangedOpenApi diff = OpenApiCompare.fromLocations("issue-317_1.json", "issue-317_2.json");
assertThat(diff.isCoreChanged().isUnchanged());
}
}
84 changes: 84 additions & 0 deletions core/src/test/resources/issue-317_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"openapi":"3.0.0",
"info":{
"title":"API",
"version":"0.1.0"
},
"paths":{
"/resource":{
"post":{
"responses":{
"200":{
"description":"Created resource",
"content":{
"application/json":{
"schema":{
"type":"string"
}
}
}
}
},
"summary":"Create resource",
"requestBody":{
"content":{
"application/json":{
"schema":{
"$ref":"#/components/schemas/Resource"
}
}
},
"description":"Definition of the resource"
}
}
}
},
"components":{
"schemas":{
"Resource":{
"type":"object",
"properties":{
"assignment":{
"$ref":"#/components/schemas/Foo"
}
}
},
"Foo":{
"oneOf":[
{
"$ref":"#/components/schemas/Foo.Bar"
},
{
"$ref":"#/components/schemas/Foo.Baz"
}
],
"discriminator":{
"propertyName":"type",
"mapping":{
"Bar":"#/components/schemas/Foo.Bar",
"Baz":"#/components/schemas/Foo.Baz"
}
}
},
"Foo.Bar":{
"type":"object",
"properties":{
"type":{
"type":"string"
}
}
},
"Foo.Baz":{
"type":"object",
"properties":{
"type":{
"type":"string"
}
}
}
},
"securitySchemes":{

}
}
}
91 changes: 91 additions & 0 deletions core/src/test/resources/issue-317_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"openapi":"3.0.0",
"info":{
"title":"API",
"version":"0.1.0"
},
"paths":{
"/resource":{
"post":{
"responses":{
"200":{
"description":"Created resource",
"content":{
"application/json":{
"schema":{
"type":"string"
}
}
}
}
},
"summary":"Create resource",
"requestBody":{
"content":{
"application/json":{
"schema":{
"$ref":"#/components/schemas/Resource"
}
}
},
"description":"Definition of the resource"
}
}
}
},
"components":{
"schemas":{
"Resource":{
"type":"object",
"properties":{
"assignment":{
"default":{
"type":"Bar"
},
"allOf":[
{
"$ref":"#/components/schemas/Foo"
}
]
}
}
},
"Foo":{
"oneOf":[
{
"$ref":"#/components/schemas/Foo.Bar"
},
{
"$ref":"#/components/schemas/Foo.Baz"
}
],
"discriminator":{
"propertyName":"type",
"mapping":{
"Bar":"#/components/schemas/Foo.Bar",
"Baz":"#/components/schemas/Foo.Baz"
}
}
},
"Foo.Bar":{
"type":"object",
"properties":{
"type":{
"type":"string"
}
}
},
"Foo.Baz":{
"type":"object",
"properties":{
"type":{
"type":"string"
}
}
}
},
"securitySchemes":{

}
}
}