Skip to content

Commit 7c86a42

Browse files
committed
improve allOf merging for composite schemas
1 parent 2128208 commit 7c86a42

File tree

6 files changed

+320
-12
lines changed

6 files changed

+320
-12
lines changed

src/main/java/com/qdesrame/openapi/diff/compare/SchemaDiff.java

Lines changed: 175 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
package com.qdesrame.openapi.diff.compare;
22

3-
import static java.util.Optional.ofNullable;
4-
53
import com.qdesrame.openapi.diff.compare.schemadiffresult.ArraySchemaDiffResult;
64
import com.qdesrame.openapi.diff.compare.schemadiffresult.ComposedSchemaDiffResult;
75
import com.qdesrame.openapi.diff.compare.schemadiffresult.SchemaDiffResult;
86
import com.qdesrame.openapi.diff.model.ChangedSchema;
97
import com.qdesrame.openapi.diff.model.DiffContext;
108
import com.qdesrame.openapi.diff.utils.RefPointer;
119
import com.qdesrame.openapi.diff.utils.RefType;
10+
11+
import java.util.ArrayList;
12+
import java.util.HashSet;
13+
import java.util.LinkedHashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
import java.util.Objects;
17+
import java.util.Optional;
18+
1219
import io.swagger.v3.oas.models.Components;
20+
import io.swagger.v3.oas.models.ExternalDocumentation;
1321
import io.swagger.v3.oas.models.media.ArraySchema;
1422
import io.swagger.v3.oas.models.media.ComposedSchema;
23+
import io.swagger.v3.oas.models.media.Discriminator;
1524
import io.swagger.v3.oas.models.media.Schema;
16-
import java.util.*;
25+
import io.swagger.v3.oas.models.media.XML;
26+
27+
import static java.util.Optional.ofNullable;
1728

1829
public class SchemaDiff extends ReferenceDiffCache<Schema, ChangedSchema> {
1930

@@ -87,10 +98,9 @@ protected static Schema resolveComposedSchema(Components components, Schema sche
8798
protected static Schema addSchema(Schema<?> schema, Schema<?> fromSchema) {
8899
if (fromSchema.getProperties() != null) {
89100
if (schema.getProperties() == null) {
90-
schema.setProperties(fromSchema.getProperties());
91-
} else {
92-
schema.getProperties().putAll(fromSchema.getProperties());
101+
schema.setProperties(new LinkedHashMap<>());
93102
}
103+
schema.getProperties().putAll(fromSchema.getProperties());
94104
}
95105

96106
if (fromSchema.getRequired() != null) {
@@ -100,7 +110,165 @@ protected static Schema addSchema(Schema<?> schema, Schema<?> fromSchema) {
100110
schema.getRequired().addAll(fromSchema.getRequired());
101111
}
102112
}
103-
// TODO copy other things from fromSchema
113+
114+
if (fromSchema.getReadOnly() != null) {
115+
schema.setReadOnly(fromSchema.getReadOnly());
116+
}
117+
if (fromSchema.getWriteOnly() != null) {
118+
schema.setWriteOnly(fromSchema.getWriteOnly());
119+
}
120+
if (fromSchema.getDeprecated() != null) {
121+
schema.setDeprecated(fromSchema.getDeprecated());
122+
}
123+
if (fromSchema.getExclusiveMaximum() != null) {
124+
schema.setExclusiveMaximum(fromSchema.getExclusiveMaximum());
125+
}
126+
if (fromSchema.getExclusiveMinimum() != null) {
127+
schema.setExclusiveMinimum(fromSchema.getExclusiveMinimum());
128+
}
129+
if (fromSchema.getNullable() != null) {
130+
schema.setNullable(fromSchema.getNullable());
131+
}
132+
if (fromSchema.getUniqueItems() != null) {
133+
schema.setUniqueItems(fromSchema.getUniqueItems());
134+
}
135+
if (fromSchema.getDescription() != null) {
136+
schema.setDescription(fromSchema.getDescription());
137+
}
138+
if (fromSchema.getFormat() != null) {
139+
schema.setFormat(fromSchema.getFormat());
140+
}
141+
if (fromSchema.getType() != null) {
142+
schema.setType(fromSchema.getType());
143+
}
144+
if (fromSchema.getEnum() != null) {
145+
if (schema.getEnum() == null) {
146+
schema.setEnum(new ArrayList<>());
147+
}
148+
//noinspection unchecked
149+
schema.getEnum().addAll((List) fromSchema.getEnum());
150+
}
151+
if (fromSchema.getExtensions() != null) {
152+
if (schema.getExtensions() == null) {
153+
schema.setExtensions(new LinkedHashMap<>());
154+
}
155+
schema.getExtensions().putAll(fromSchema.getExtensions());
156+
}
157+
if (fromSchema.getDiscriminator() != null) {
158+
if (schema.getDiscriminator() == null) {
159+
schema.setDiscriminator(new Discriminator());
160+
}
161+
final Discriminator discriminator = schema.getDiscriminator();
162+
final Discriminator fromDiscriminator = fromSchema.getDiscriminator();
163+
if (fromDiscriminator.getPropertyName() != null) {
164+
discriminator.setPropertyName(fromDiscriminator.getPropertyName());
165+
}
166+
if (fromDiscriminator.getMapping() != null) {
167+
if (discriminator.getMapping() == null) {
168+
discriminator.setMapping(new LinkedHashMap<>());
169+
}
170+
discriminator.getMapping().putAll(fromDiscriminator.getMapping());
171+
}
172+
}
173+
if (fromSchema.getTitle() != null) {
174+
schema.setTitle(fromSchema.getTitle());
175+
}
176+
if (fromSchema.getName() != null) {
177+
schema.setName(fromSchema.getName());
178+
}
179+
if (fromSchema.getAdditionalProperties() != null) {
180+
schema.setAdditionalProperties(fromSchema.getAdditionalProperties());
181+
}
182+
if (fromSchema.getDefault() != null) {
183+
schema.setDefault(fromSchema.getDefault());
184+
}
185+
if (fromSchema.getExample() != null) {
186+
schema.setExample(fromSchema.getExample());
187+
}
188+
if (fromSchema.getExternalDocs() != null) {
189+
if (schema.getExternalDocs() == null) {
190+
schema.setExternalDocs(new ExternalDocumentation());
191+
}
192+
final ExternalDocumentation externalDocs = schema.getExternalDocs();
193+
final ExternalDocumentation fromExternalDocs = fromSchema.getExternalDocs();
194+
if (fromExternalDocs.getDescription() != null) {
195+
externalDocs.setDescription(fromExternalDocs.getDescription());
196+
}
197+
if (fromExternalDocs.getExtensions() != null) {
198+
if (externalDocs.getExtensions() == null) {
199+
externalDocs.setExtensions(new LinkedHashMap<>());
200+
}
201+
externalDocs.getExtensions().putAll(fromExternalDocs.getExtensions());
202+
}
203+
if (fromExternalDocs.getUrl() != null) {
204+
externalDocs.setUrl(fromExternalDocs.getUrl());
205+
}
206+
}
207+
if (fromSchema.getMaximum() != null) {
208+
schema.setMaximum(fromSchema.getMaximum());
209+
}
210+
if (fromSchema.getMinimum() != null) {
211+
schema.setMinimum(fromSchema.getMinimum());
212+
}
213+
if (fromSchema.getMaxItems() != null) {
214+
schema.setMaxItems(fromSchema.getMaxItems());
215+
}
216+
if (fromSchema.getMinItems() != null) {
217+
schema.setMinItems(fromSchema.getMinItems());
218+
}
219+
if (fromSchema.getMaxProperties() != null) {
220+
schema.setMaxProperties(fromSchema.getMaxProperties());
221+
}
222+
if (fromSchema.getMinProperties() != null) {
223+
schema.setMinProperties(fromSchema.getMinProperties());
224+
}
225+
if (fromSchema.getMaxLength() != null) {
226+
schema.setMaxLength(fromSchema.getMaxLength());
227+
}
228+
if (fromSchema.getMinLength() != null) {
229+
schema.setMinLength(fromSchema.getMinLength());
230+
}
231+
if (fromSchema.getMultipleOf() != null) {
232+
schema.setMultipleOf(fromSchema.getMultipleOf());
233+
}
234+
if (fromSchema.getNot() != null) {
235+
if (schema.getNot() == null) {
236+
schema.setNot(addSchema(new Schema(), fromSchema.getNot()));
237+
} else {
238+
addSchema(schema.getNot(), fromSchema.getNot());
239+
}
240+
}
241+
if (fromSchema.getPattern() != null) {
242+
schema.setPattern(fromSchema.getPattern());
243+
}
244+
if (fromSchema.getXml() != null) {
245+
if (schema.getXml() == null) {
246+
schema.setXml(new XML());
247+
}
248+
final XML xml = schema.getXml();
249+
final XML fromXml = fromSchema.getXml();
250+
if (fromXml.getAttribute() != null) {
251+
xml.setAttribute(fromXml.getAttribute());
252+
}
253+
if (fromXml.getName() != null) {
254+
xml.setName(fromXml.getName());
255+
}
256+
if (fromXml.getNamespace() != null) {
257+
xml.setNamespace(fromXml.getNamespace());
258+
}
259+
if (fromXml.getExtensions() != null) {
260+
if (xml.getExtensions() == null) {
261+
xml.setExtensions(new LinkedHashMap<>());
262+
}
263+
xml.getExtensions().putAll(fromXml.getExtensions());
264+
}
265+
if (fromXml.getPrefix() != null) {
266+
xml.setPrefix(fromXml.getPrefix());
267+
}
268+
if (fromXml.getWrapped() != null) {
269+
xml.setWrapped(fromXml.getWrapped());
270+
}
271+
}
104272
return schema;
105273
}
106274

src/test/java/com/qdesrame/openapi/test/AllOfDiffTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package com.qdesrame.openapi.test;
22

3+
import org.junit.jupiter.api.Test;
4+
35
import static com.qdesrame.openapi.test.TestUtils.assertOpenApiAreEquals;
46
import static com.qdesrame.openapi.test.TestUtils.assertOpenApiChangedEndpoints;
57

6-
import org.junit.jupiter.api.Test;
7-
88
/** Created by adarsh.sharma on 19/12/17. */
99
public class AllOfDiffTest {
1010

1111
private final String OPENAPI_DOC1 = "allOf_diff_1.yaml";
1212
private final String OPENAPI_DOC2 = "allOf_diff_2.yaml";
1313
private final String OPENAPI_DOC3 = "allOf_diff_3.yaml";
14+
private final String OPENAPI_DOC4 = "allOf_diff_4.yaml";
1415

1516
@Test
1617
public void testDiffSame() {
@@ -23,7 +24,12 @@ public void testDiffSameWithAllOf() {
2324
}
2425

2526
@Test
26-
public void testDiffDifferent() {
27+
public void testDiffDifferent1() {
2728
assertOpenApiChangedEndpoints(OPENAPI_DOC1, OPENAPI_DOC3);
2829
}
30+
31+
@Test
32+
public void testDiffDifferent2() {
33+
assertOpenApiChangedEndpoints(OPENAPI_DOC1, OPENAPI_DOC4);
34+
}
2935
}

src/test/resources/allOf_diff_1.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ components:
108108
- pet_type
109109
properties:
110110
pet_type:
111-
type: string
111+
nullable: false
112+
allOf:
113+
- type: string
112114
Cat:
113115
description: Cat class
114116
allOf:

src/test/resources/allOf_diff_2.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ components:
101101
- pet_type
102102
properties:
103103
pet_type:
104+
nullable: false
104105
type: string
105106
Cat:
106107
description: Cat class

src/test/resources/allOf_diff_3.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ components:
105105
- pet_type
106106
properties:
107107
pet_type:
108-
type: string
108+
nullable: false
109+
allOf:
110+
- type: number
109111
Cat:
110112
description: Cat class
111113
allOf:

0 commit comments

Comments
 (0)