Skip to content

Fix NPE with unnamed schemas in {one,any,all}-of #220

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 1 commit into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.swagger.v3.oas.models.media.Schema;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -63,7 +64,8 @@ public <T extends Schema<X>, X> Optional<ChangedSchema> diff(

MapKeyDiff<String, Schema> mappingDiff =
MapKeyDiff.diff(
getSchema(leftComponents, leftMapping), getSchema(rightComponents, rightMapping));
getSchema(leftComponents, leftMapping, leftComposedSchema),
getSchema(rightComponents, rightMapping, rightComposedSchema));
Map<String, ChangedSchema> changedMapping = new LinkedHashMap<>();
for (String key : mappingDiff.getSharedKey()) {
Schema leftSchema = new Schema();
Expand All @@ -88,10 +90,14 @@ public <T extends Schema<X>, X> Optional<ChangedSchema> diff(
}
}

private Map<String, Schema> getSchema(Components components, Map<String, String> mapping) {
private Map<String, Schema> getSchema(Components components, Map<String, String> mapping, ComposedSchema composedSchema) {
Map<String, Schema> result = new LinkedHashMap<>();
mapping.forEach(
(key, value) -> result.put(key, refPointer.resolveRef(components, new Schema(), value)));

result.putAll(getUnnamedSchemas(composedSchema.getAllOf(), "all-of"));
result.putAll(getUnnamedSchemas(composedSchema.getOneOf(), "one-of"));
result.putAll(getUnnamedSchemas(composedSchema.getAnyOf(), "any-of"));
Comment on lines +98 to +100
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quick and dirty workaround - naming the unnamed schemas in order of their appearance in the list.

return result;
}

Expand All @@ -100,7 +106,7 @@ private Map<String, String> getMapping(ComposedSchema composedSchema) {
for (Schema schema : composedSchema.getOneOf()) {
String ref = schema.get$ref();
if (ref == null) {
throw new IllegalArgumentException("invalid oneOf schema");
continue;
}
String schemaName = refPointer.getRefName(ref);
if (schemaName == null) {
Expand All @@ -119,4 +125,25 @@ private Map<String, String> getMapping(ComposedSchema composedSchema) {
return reverseMapping.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
}

private Map<String, Schema> getUnnamedSchemas(List<Schema> schemas, String name) {
Map<String, Schema> result = new LinkedHashMap<>();

if (schemas == null) {
return result;
}
Comment on lines +132 to +134
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list is sometimes null (in the diff case) which throws nullptr


for (int i = 0; i < schemas.size(); i++) {
Schema schema = schemas.get(i);

// If the ref is named, then we ignore it since getMapping will handle it.
if (schema.get$ref() != null) {
continue;
}

result.put(String.format("%s-%s", name, i), schema);
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class OneOfDiffTest {
private final String OPENAPI_DOC7 = "oneOf_discriminator-changed_2.yaml";
private final String OPENAPI_DOC8 = "oneOf_discriminator-missing_1.yaml";
private final String OPENAPI_DOC9 = "oneOf_discriminator-missing_2.yaml";
private final String OPENAPI_DOC10 = "unnamed_oneof_schema_1.yaml";

@Test
public void testDiffSame() {
Expand All @@ -39,6 +40,10 @@ public void testComposedSchema() {
assertOpenApiBackwardIncompatible(OPENAPI_DOC4, OPENAPI_DOC5);
}

@Test
public void testComposedSchemaDiff() {
assertOpenApiAreEquals(OPENAPI_DOC10, OPENAPI_DOC10);
}
@Test
public void testOneOfDiscrimitatorChanged() {
// The oneOf 'discriminator' changed: 'realtype' -> 'othertype':
Expand Down
22 changes: 22 additions & 0 deletions core/src/test/resources/unnamed_oneof_schema_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
openapi: 3.0.0
info:
title: Test Service
version: 1.0.0
paths:
/test:
get:
responses:
"200":
description: Success
content:
application/json:
schema:
oneOf:
- type: object
properties:
name:
type: string
- type: object
properties:
name:
type: number