Skip to content

improve allOf merging for composite schemas #85

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 2 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
improve allOf merging for composite schemas
  • Loading branch information
damianw committed May 31, 2019
commit 7c86a42805e3a0094b3f6ec7e06c93b74df6d9ea
182 changes: 175 additions & 7 deletions src/main/java/com/qdesrame/openapi/diff/compare/SchemaDiff.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package com.qdesrame.openapi.diff.compare;

import static java.util.Optional.ofNullable;

import com.qdesrame.openapi.diff.compare.schemadiffresult.ArraySchemaDiffResult;
import com.qdesrame.openapi.diff.compare.schemadiffresult.ComposedSchemaDiffResult;
import com.qdesrame.openapi.diff.compare.schemadiffresult.SchemaDiffResult;
import com.qdesrame.openapi.diff.model.ChangedSchema;
import com.qdesrame.openapi.diff.model.DiffContext;
import com.qdesrame.openapi.diff.utils.RefPointer;
import com.qdesrame.openapi.diff.utils.RefType;

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 io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Discriminator;
import io.swagger.v3.oas.models.media.Schema;
import java.util.*;
import io.swagger.v3.oas.models.media.XML;

import static java.util.Optional.ofNullable;

public class SchemaDiff extends ReferenceDiffCache<Schema, ChangedSchema> {

Expand Down Expand Up @@ -87,10 +98,9 @@ protected static Schema resolveComposedSchema(Components components, Schema sche
protected static Schema addSchema(Schema<?> schema, Schema<?> fromSchema) {
if (fromSchema.getProperties() != null) {
if (schema.getProperties() == null) {
schema.setProperties(fromSchema.getProperties());
} else {
schema.getProperties().putAll(fromSchema.getProperties());
schema.setProperties(new LinkedHashMap<>());
}
schema.getProperties().putAll(fromSchema.getProperties());
}

if (fromSchema.getRequired() != null) {
Expand All @@ -100,7 +110,165 @@ protected static Schema addSchema(Schema<?> schema, Schema<?> fromSchema) {
schema.getRequired().addAll(fromSchema.getRequired());
}
}
// TODO copy other things from fromSchema

if (fromSchema.getReadOnly() != null) {
schema.setReadOnly(fromSchema.getReadOnly());
}
if (fromSchema.getWriteOnly() != null) {
schema.setWriteOnly(fromSchema.getWriteOnly());
}
if (fromSchema.getDeprecated() != null) {
schema.setDeprecated(fromSchema.getDeprecated());
}
if (fromSchema.getExclusiveMaximum() != null) {
schema.setExclusiveMaximum(fromSchema.getExclusiveMaximum());
}
if (fromSchema.getExclusiveMinimum() != null) {
schema.setExclusiveMinimum(fromSchema.getExclusiveMinimum());
}
if (fromSchema.getNullable() != null) {
schema.setNullable(fromSchema.getNullable());
}
if (fromSchema.getUniqueItems() != null) {
schema.setUniqueItems(fromSchema.getUniqueItems());
}
if (fromSchema.getDescription() != null) {
schema.setDescription(fromSchema.getDescription());
}
if (fromSchema.getFormat() != null) {
schema.setFormat(fromSchema.getFormat());
}
if (fromSchema.getType() != null) {
schema.setType(fromSchema.getType());
}
if (fromSchema.getEnum() != null) {
if (schema.getEnum() == null) {
schema.setEnum(new ArrayList<>());
}
//noinspection unchecked
schema.getEnum().addAll((List) fromSchema.getEnum());
}
if (fromSchema.getExtensions() != null) {
if (schema.getExtensions() == null) {
schema.setExtensions(new LinkedHashMap<>());
}
schema.getExtensions().putAll(fromSchema.getExtensions());
}
if (fromSchema.getDiscriminator() != null) {
if (schema.getDiscriminator() == null) {
schema.setDiscriminator(new Discriminator());
}
final Discriminator discriminator = schema.getDiscriminator();
final Discriminator fromDiscriminator = fromSchema.getDiscriminator();
if (fromDiscriminator.getPropertyName() != null) {
discriminator.setPropertyName(fromDiscriminator.getPropertyName());
}
if (fromDiscriminator.getMapping() != null) {
if (discriminator.getMapping() == null) {
discriminator.setMapping(new LinkedHashMap<>());
}
discriminator.getMapping().putAll(fromDiscriminator.getMapping());
}
}
if (fromSchema.getTitle() != null) {
schema.setTitle(fromSchema.getTitle());
}
if (fromSchema.getName() != null) {
schema.setName(fromSchema.getName());
}
if (fromSchema.getAdditionalProperties() != null) {
schema.setAdditionalProperties(fromSchema.getAdditionalProperties());
}
if (fromSchema.getDefault() != null) {
schema.setDefault(fromSchema.getDefault());
}
if (fromSchema.getExample() != null) {
schema.setExample(fromSchema.getExample());
}
if (fromSchema.getExternalDocs() != null) {
if (schema.getExternalDocs() == null) {
schema.setExternalDocs(new ExternalDocumentation());
}
final ExternalDocumentation externalDocs = schema.getExternalDocs();
final ExternalDocumentation fromExternalDocs = fromSchema.getExternalDocs();
if (fromExternalDocs.getDescription() != null) {
externalDocs.setDescription(fromExternalDocs.getDescription());
}
if (fromExternalDocs.getExtensions() != null) {
if (externalDocs.getExtensions() == null) {
externalDocs.setExtensions(new LinkedHashMap<>());
}
externalDocs.getExtensions().putAll(fromExternalDocs.getExtensions());
}
if (fromExternalDocs.getUrl() != null) {
externalDocs.setUrl(fromExternalDocs.getUrl());
}
}
if (fromSchema.getMaximum() != null) {
schema.setMaximum(fromSchema.getMaximum());
}
if (fromSchema.getMinimum() != null) {
schema.setMinimum(fromSchema.getMinimum());
}
if (fromSchema.getMaxItems() != null) {
schema.setMaxItems(fromSchema.getMaxItems());
}
if (fromSchema.getMinItems() != null) {
schema.setMinItems(fromSchema.getMinItems());
}
if (fromSchema.getMaxProperties() != null) {
schema.setMaxProperties(fromSchema.getMaxProperties());
}
if (fromSchema.getMinProperties() != null) {
schema.setMinProperties(fromSchema.getMinProperties());
}
if (fromSchema.getMaxLength() != null) {
schema.setMaxLength(fromSchema.getMaxLength());
}
if (fromSchema.getMinLength() != null) {
schema.setMinLength(fromSchema.getMinLength());
}
if (fromSchema.getMultipleOf() != null) {
schema.setMultipleOf(fromSchema.getMultipleOf());
}
if (fromSchema.getNot() != null) {
if (schema.getNot() == null) {
schema.setNot(addSchema(new Schema(), fromSchema.getNot()));
} else {
addSchema(schema.getNot(), fromSchema.getNot());
}
}
if (fromSchema.getPattern() != null) {
schema.setPattern(fromSchema.getPattern());
}
if (fromSchema.getXml() != null) {
if (schema.getXml() == null) {
schema.setXml(new XML());
}
final XML xml = schema.getXml();
final XML fromXml = fromSchema.getXml();
if (fromXml.getAttribute() != null) {
xml.setAttribute(fromXml.getAttribute());
}
if (fromXml.getName() != null) {
xml.setName(fromXml.getName());
}
if (fromXml.getNamespace() != null) {
xml.setNamespace(fromXml.getNamespace());
}
if (fromXml.getExtensions() != null) {
if (xml.getExtensions() == null) {
xml.setExtensions(new LinkedHashMap<>());
}
xml.getExtensions().putAll(fromXml.getExtensions());
}
if (fromXml.getPrefix() != null) {
xml.setPrefix(fromXml.getPrefix());
}
if (fromXml.getWrapped() != null) {
xml.setWrapped(fromXml.getWrapped());
}
}
return schema;
}

Expand Down
12 changes: 9 additions & 3 deletions src/test/java/com/qdesrame/openapi/test/AllOfDiffTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.qdesrame.openapi.test;

import org.junit.jupiter.api.Test;

import static com.qdesrame.openapi.test.TestUtils.assertOpenApiAreEquals;
import static com.qdesrame.openapi.test.TestUtils.assertOpenApiChangedEndpoints;

import org.junit.jupiter.api.Test;

/** Created by adarsh.sharma on 19/12/17. */
public class AllOfDiffTest {

private final String OPENAPI_DOC1 = "allOf_diff_1.yaml";
private final String OPENAPI_DOC2 = "allOf_diff_2.yaml";
private final String OPENAPI_DOC3 = "allOf_diff_3.yaml";
private final String OPENAPI_DOC4 = "allOf_diff_4.yaml";

@Test
public void testDiffSame() {
Expand All @@ -23,7 +24,12 @@ public void testDiffSameWithAllOf() {
}

@Test
public void testDiffDifferent() {
public void testDiffDifferent1() {
assertOpenApiChangedEndpoints(OPENAPI_DOC1, OPENAPI_DOC3);
}

@Test
public void testDiffDifferent2() {
assertOpenApiChangedEndpoints(OPENAPI_DOC1, OPENAPI_DOC4);
}
}
4 changes: 3 additions & 1 deletion src/test/resources/allOf_diff_1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ components:
- pet_type
properties:
pet_type:
type: string
nullable: false
allOf:
- type: string
Cat:
description: Cat class
allOf:
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/allOf_diff_2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ components:
- pet_type
properties:
pet_type:
nullable: false
type: string
Cat:
description: Cat class
Expand Down
4 changes: 3 additions & 1 deletion src/test/resources/allOf_diff_3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ components:
- pet_type
properties:
pet_type:
type: string
nullable: false
allOf:
- type: number
Cat:
description: Cat class
allOf:
Expand Down
Loading