Skip to content

Commit 60e6a9f

Browse files
authored
NullPointerException when oneOf changes to anyOf in response schema (#892)
Fixes #381
1 parent ef096a7 commit 60e6a9f

File tree

4 files changed

+86
-9
lines changed

4 files changed

+86
-9
lines changed

core/src/main/java/org/openapitools/openapidiff/core/compare/schemadiffresult/ComposedSchemaDiffResult.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,18 @@ private Map<String, Schema> getSchema(
117117

118118
private Map<String, String> getMapping(ComposedSchema composedSchema) {
119119
Map<String, String> reverseMapping = new LinkedHashMap<>();
120-
for (Schema<?> schema : composedSchema.getOneOf()) {
121-
String ref = schema.get$ref();
122-
if (ref == null) {
123-
continue;
124-
}
125-
String schemaName = refPointer.getRefName(ref);
126-
if (schemaName == null) {
127-
throw new IllegalArgumentException("invalid schema: " + ref);
120+
if (composedSchema.getOneOf() != null) {
121+
for (Schema<?> schema : composedSchema.getOneOf()) {
122+
String ref = schema.get$ref();
123+
if (ref == null) {
124+
continue;
125+
}
126+
String schemaName = refPointer.getRefName(ref);
127+
if (schemaName == null) {
128+
throw new IllegalArgumentException("invalid schema: " + ref);
129+
}
130+
reverseMapping.put(ref, schemaName);
128131
}
129-
reverseMapping.put(ref, schemaName);
130132
}
131133

132134
if (composedSchema.getDiscriminator() != null

core/src/test/java/org/openapitools/openapidiff/core/OneOfDiffTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class OneOfDiffTest {
1818
private final String OPENAPI_DOC8 = "oneOf_discriminator-missing_1.yaml";
1919
private final String OPENAPI_DOC9 = "oneOf_discriminator-missing_2.yaml";
2020
private final String OPENAPI_DOC10 = "unnamed_oneof_schema_1.yaml";
21+
private final String OPENAPI_DOC11 = "oneOf_to_anyOf_1.yaml";
22+
private final String OPENAPI_DOC12 = "oneOf_to_anyOf_2.yaml";
2123

2224
@Test
2325
public void testDiffSame() {
@@ -59,4 +61,9 @@ public void testOneOfDiscrimitatorMissingSameOrder() {
5961
public void testOneOfDiscrimitatorMissingDifferentOrder() {
6062
assertOpenApiAreEquals(OPENAPI_DOC8, OPENAPI_DOC9);
6163
}
64+
65+
@Test // issue #381
66+
public void testOneOfToAnyOfDoesNotThrow() {
67+
assertOpenApiChangedEndpoints(OPENAPI_DOC11, OPENAPI_DOC12);
68+
}
6269
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
openapi: 3.0.1
2+
info:
3+
title: oneOf to anyOf repro (old)
4+
version: 1.0.0
5+
paths:
6+
/example:
7+
get:
8+
responses:
9+
"200":
10+
description: ok
11+
content:
12+
application/json:
13+
schema:
14+
$ref: "#/components/schemas/Result"
15+
components:
16+
schemas:
17+
Result:
18+
oneOf:
19+
- $ref: "#/components/schemas/TypeA"
20+
- $ref: "#/components/schemas/TypeB"
21+
TypeA:
22+
type: object
23+
required:
24+
- a
25+
properties:
26+
a:
27+
type: string
28+
TypeB:
29+
type: object
30+
required:
31+
- b
32+
properties:
33+
b:
34+
type: string
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
openapi: 3.0.1
2+
info:
3+
title: oneOf to anyOf repro (new)
4+
version: 1.0.0
5+
paths:
6+
/example:
7+
get:
8+
responses:
9+
"200":
10+
description: ok
11+
content:
12+
application/json:
13+
schema:
14+
$ref: "#/components/schemas/Result"
15+
components:
16+
schemas:
17+
Result:
18+
anyOf:
19+
- $ref: "#/components/schemas/TypeA"
20+
- $ref: "#/components/schemas/TypeB"
21+
TypeA:
22+
type: object
23+
required:
24+
- a
25+
properties:
26+
a:
27+
type: string
28+
TypeB:
29+
type: object
30+
required:
31+
- b
32+
properties:
33+
b:
34+
type: string

0 commit comments

Comments
 (0)