Skip to content

Commit 726740a

Browse files
committed
Merge branch 'dang-gyg-optional-discriminator-29' into master
Refs #158
2 parents a6b076e + 4f10108 commit 726740a

File tree

4 files changed

+111
-10
lines changed

4 files changed

+111
-10
lines changed

core/src/main/java/com/qdesrame/openapi/diff/core/compare/schemadiffresult/ComposedSchemaDiffResult.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ public <T extends Schema<X>, X> Optional<ChangedSchema> diff(
4242

4343
Discriminator leftDis = leftComposedSchema.getDiscriminator();
4444
Discriminator rightDis = rightComposedSchema.getDiscriminator();
45-
if (leftDis == null
46-
|| rightDis == null
47-
|| leftDis.getPropertyName() == null
48-
|| rightDis.getPropertyName() == null) {
49-
throw new IllegalArgumentException(
50-
"discriminator or property not found for oneOf schema");
51-
} else if (!leftDis.getPropertyName().equals(rightDis.getPropertyName())
52-
|| (CollectionUtils.isEmpty(leftComposedSchema.getOneOf())
53-
|| CollectionUtils.isEmpty(rightComposedSchema.getOneOf()))) {
45+
if ((leftDis == null && rightDis != null)
46+
|| (leftDis != null && rightDis == null)
47+
|| (leftDis != null
48+
&& rightDis != null
49+
&& ((leftDis.getPropertyName() == null && rightDis.getPropertyName() != null)
50+
|| (leftDis.getPropertyName() != null && rightDis.getPropertyName() == null)
51+
|| (leftDis.getPropertyName() != null
52+
&& rightDis.getPropertyName() != null
53+
&& !leftDis.getPropertyName().equals(rightDis.getPropertyName()))))) {
5454
changedSchema.setOldSchema(left);
5555
changedSchema.setNewSchema(right);
5656
changedSchema.setDiscriminatorPropertyChanged(true);
@@ -109,7 +109,8 @@ private Map<String, String> getMapping(ComposedSchema composedSchema) {
109109
reverseMapping.put(ref, schemaName);
110110
}
111111

112-
if (composedSchema.getDiscriminator().getMapping() != null) {
112+
if (composedSchema.getDiscriminator() != null
113+
&& composedSchema.getDiscriminator().getMapping() != null) {
113114
for (String ref : composedSchema.getDiscriminator().getMapping().keySet()) {
114115
reverseMapping.put(composedSchema.getDiscriminator().getMapping().get(ref), ref);
115116
}

core/src/test/java/com/qdesrame/openapi/test/OneOfDiffTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class OneOfDiffTest {
1616
private final String OPENAPI_DOC5 = "composed_schema_2.yaml";
1717
private final String OPENAPI_DOC6 = "oneOf_discriminator-changed_1.yaml";
1818
private final String OPENAPI_DOC7 = "oneOf_discriminator-changed_2.yaml";
19+
private final String OPENAPI_DOC8 = "oneOf_discriminator-missing_1.yaml";
20+
private final String OPENAPI_DOC9 = "oneOf_discriminator-missing_2.yaml";
1921

2022
@Test
2123
public void testDiffSame() {
@@ -42,4 +44,14 @@ public void testOneOfDiscrimitatorChanged() {
4244
// The oneOf 'discriminator' changed: 'realtype' -> 'othertype':
4345
assertOpenApiBackwardIncompatible(OPENAPI_DOC6, OPENAPI_DOC7);
4446
}
47+
48+
@Test
49+
public void testOneOfDiscrimitatorMissingSameOrder() {
50+
assertOpenApiAreEquals(OPENAPI_DOC8, OPENAPI_DOC8);
51+
}
52+
53+
@Test
54+
public void testOneOfDiscrimitatorMissingDifferentOrder() {
55+
assertOpenApiAreEquals(OPENAPI_DOC8, OPENAPI_DOC9);
56+
}
4557
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
openapi: 3.0.1
2+
info:
3+
title: oneOf test for issue 29
4+
version: '1.0'
5+
servers:
6+
- url: 'http://localhost:8000/'
7+
paths:
8+
/state:
9+
post:
10+
operationId: update
11+
requestBody:
12+
content:
13+
application/json:
14+
schema:
15+
oneOf:
16+
- $ref: '#/components/schemas/A'
17+
- $ref: '#/components/schemas/B'
18+
required: true
19+
responses:
20+
'201':
21+
description: OK
22+
components:
23+
schemas:
24+
A:
25+
type: object
26+
properties:
27+
realtype:
28+
type: string
29+
othertype:
30+
type: string
31+
message:
32+
type: string
33+
B:
34+
type: object
35+
properties:
36+
realtype:
37+
type: string
38+
othertype:
39+
type: string
40+
description:
41+
type: string
42+
code:
43+
type: integer
44+
format: int32
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
openapi: 3.0.1
2+
info:
3+
title: oneOf test for issue 29
4+
version: '1.0'
5+
servers:
6+
- url: 'http://localhost:8000/'
7+
paths:
8+
/state:
9+
post:
10+
operationId: update
11+
requestBody:
12+
content:
13+
application/json:
14+
schema:
15+
oneOf:
16+
- $ref: '#/components/schemas/B'
17+
- $ref: '#/components/schemas/A'
18+
required: true
19+
responses:
20+
'201':
21+
description: OK
22+
components:
23+
schemas:
24+
A:
25+
type: object
26+
properties:
27+
realtype:
28+
type: string
29+
othertype:
30+
type: string
31+
message:
32+
type: string
33+
B:
34+
type: object
35+
properties:
36+
realtype:
37+
type: string
38+
othertype:
39+
type: string
40+
description:
41+
type: string
42+
code:
43+
type: integer
44+
format: int32

0 commit comments

Comments
 (0)