Skip to content

Commit 2a8648c

Browse files
authored
Fix NPE with unnamed schemas in {one,any,all}-of (#220)
Fixes #176
1 parent 0b3cf26 commit 2a8648c

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

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

+30-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.swagger.v3.oas.models.media.Schema;
77
import java.util.HashSet;
88
import java.util.LinkedHashMap;
9+
import java.util.List;
910
import java.util.Map;
1011
import java.util.Optional;
1112
import java.util.stream.Collectors;
@@ -63,7 +64,8 @@ public <T extends Schema<X>, X> Optional<ChangedSchema> diff(
6364

6465
MapKeyDiff<String, Schema> mappingDiff =
6566
MapKeyDiff.diff(
66-
getSchema(leftComponents, leftMapping), getSchema(rightComponents, rightMapping));
67+
getSchema(leftComponents, leftMapping, leftComposedSchema),
68+
getSchema(rightComponents, rightMapping, rightComposedSchema));
6769
Map<String, ChangedSchema> changedMapping = new LinkedHashMap<>();
6870
for (String key : mappingDiff.getSharedKey()) {
6971
Schema leftSchema = new Schema();
@@ -88,10 +90,14 @@ public <T extends Schema<X>, X> Optional<ChangedSchema> diff(
8890
}
8991
}
9092

91-
private Map<String, Schema> getSchema(Components components, Map<String, String> mapping) {
93+
private Map<String, Schema> getSchema(Components components, Map<String, String> mapping, ComposedSchema composedSchema) {
9294
Map<String, Schema> result = new LinkedHashMap<>();
9395
mapping.forEach(
9496
(key, value) -> result.put(key, refPointer.resolveRef(components, new Schema(), value)));
97+
98+
result.putAll(getUnnamedSchemas(composedSchema.getAllOf(), "all-of"));
99+
result.putAll(getUnnamedSchemas(composedSchema.getOneOf(), "one-of"));
100+
result.putAll(getUnnamedSchemas(composedSchema.getAnyOf(), "any-of"));
95101
return result;
96102
}
97103

@@ -100,7 +106,7 @@ private Map<String, String> getMapping(ComposedSchema composedSchema) {
100106
for (Schema schema : composedSchema.getOneOf()) {
101107
String ref = schema.get$ref();
102108
if (ref == null) {
103-
throw new IllegalArgumentException("invalid oneOf schema");
109+
continue;
104110
}
105111
String schemaName = refPointer.getRefName(ref);
106112
if (schemaName == null) {
@@ -119,4 +125,25 @@ private Map<String, String> getMapping(ComposedSchema composedSchema) {
119125
return reverseMapping.entrySet().stream()
120126
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
121127
}
128+
129+
private Map<String, Schema> getUnnamedSchemas(List<Schema> schemas, String name) {
130+
Map<String, Schema> result = new LinkedHashMap<>();
131+
132+
if (schemas == null) {
133+
return result;
134+
}
135+
136+
for (int i = 0; i < schemas.size(); i++) {
137+
Schema schema = schemas.get(i);
138+
139+
// If the ref is named, then we ignore it since getMapping will handle it.
140+
if (schema.get$ref() != null) {
141+
continue;
142+
}
143+
144+
result.put(String.format("%s-%s", name, i), schema);
145+
}
146+
147+
return result;
148+
}
122149
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class OneOfDiffTest {
1818
private final String OPENAPI_DOC7 = "oneOf_discriminator-changed_2.yaml";
1919
private final String OPENAPI_DOC8 = "oneOf_discriminator-missing_1.yaml";
2020
private final String OPENAPI_DOC9 = "oneOf_discriminator-missing_2.yaml";
21+
private final String OPENAPI_DOC10 = "unnamed_oneof_schema_1.yaml";
2122

2223
@Test
2324
public void testDiffSame() {
@@ -39,6 +40,10 @@ public void testComposedSchema() {
3940
assertOpenApiBackwardIncompatible(OPENAPI_DOC4, OPENAPI_DOC5);
4041
}
4142

43+
@Test
44+
public void testComposedSchemaDiff() {
45+
assertOpenApiAreEquals(OPENAPI_DOC10, OPENAPI_DOC10);
46+
}
4247
@Test
4348
public void testOneOfDiscrimitatorChanged() {
4449
// The oneOf 'discriminator' changed: 'realtype' -> 'othertype':
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Test Service
4+
version: 1.0.0
5+
paths:
6+
/test:
7+
get:
8+
responses:
9+
"200":
10+
description: Success
11+
content:
12+
application/json:
13+
schema:
14+
oneOf:
15+
- type: object
16+
properties:
17+
name:
18+
type: string
19+
- type: object
20+
properties:
21+
name:
22+
type: number

0 commit comments

Comments
 (0)