diff --git a/src/main/java/com/qdesrame/openapi/diff/compare/schemadiffresult/ComposedSchemaDiffResult.java b/src/main/java/com/qdesrame/openapi/diff/compare/schemadiffresult/ComposedSchemaDiffResult.java index 5121072ca..6d264bcc6 100644 --- a/src/main/java/com/qdesrame/openapi/diff/compare/schemadiffresult/ComposedSchemaDiffResult.java +++ b/src/main/java/com/qdesrame/openapi/diff/compare/schemadiffresult/ComposedSchemaDiffResult.java @@ -42,15 +42,15 @@ public , X> Optional diff( Discriminator leftDis = leftComposedSchema.getDiscriminator(); Discriminator rightDis = rightComposedSchema.getDiscriminator(); - if (leftDis == null - || rightDis == null - || leftDis.getPropertyName() == null - || rightDis.getPropertyName() == null) { - throw new IllegalArgumentException( - "discriminator or property not found for oneOf schema"); - } else if (!leftDis.getPropertyName().equals(rightDis.getPropertyName()) - || (CollectionUtils.isEmpty(leftComposedSchema.getOneOf()) - || CollectionUtils.isEmpty(rightComposedSchema.getOneOf()))) { + if ((leftDis == null && rightDis != null) + || (leftDis != null && rightDis == null) + || (leftDis != null && rightDis != null && ( + (leftDis.getPropertyName() == null && rightDis.getPropertyName() != null) + || (leftDis.getPropertyName() != null && rightDis.getPropertyName() == null) + || (leftDis.getPropertyName() != null && rightDis.getPropertyName() != null && + !leftDis.getPropertyName().equals(rightDis.getPropertyName())) + )) + ) { changedSchema.setOldSchema(left); changedSchema.setNewSchema(right); changedSchema.setDiscriminatorPropertyChanged(true); @@ -109,7 +109,7 @@ private Map getMapping(ComposedSchema composedSchema) { reverseMapping.put(ref, schemaName); } - if (composedSchema.getDiscriminator().getMapping() != null) { + if (composedSchema.getDiscriminator() != null && composedSchema.getDiscriminator().getMapping() != null) { for (String ref : composedSchema.getDiscriminator().getMapping().keySet()) { reverseMapping.put(composedSchema.getDiscriminator().getMapping().get(ref), ref); } diff --git a/src/test/java/com/qdesrame/openapi/test/OneOfDiffTest.java b/src/test/java/com/qdesrame/openapi/test/OneOfDiffTest.java index 5f6779454..16a4eb5c4 100644 --- a/src/test/java/com/qdesrame/openapi/test/OneOfDiffTest.java +++ b/src/test/java/com/qdesrame/openapi/test/OneOfDiffTest.java @@ -16,6 +16,8 @@ public class OneOfDiffTest { private final String OPENAPI_DOC5 = "composed_schema_2.yaml"; private final String OPENAPI_DOC6 = "oneOf_discriminator-changed_1.yaml"; 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"; @Test public void testDiffSame() { @@ -42,4 +44,14 @@ public void testOneOfDiscrimitatorChanged() { // The oneOf 'discriminator' changed: 'realtype' -> 'othertype': assertOpenApiBackwardIncompatible(OPENAPI_DOC6, OPENAPI_DOC7); } + + @Test + public void testOneOfDiscrimitatorMissingSameOrder() { + assertOpenApiAreEquals(OPENAPI_DOC8, OPENAPI_DOC8); + } + + @Test + public void testOneOfDiscrimitatorMissingDifferentOrder() { + assertOpenApiAreEquals(OPENAPI_DOC8, OPENAPI_DOC9); + } } diff --git a/src/test/resources/oneOf_discriminator-missing_1.yaml b/src/test/resources/oneOf_discriminator-missing_1.yaml new file mode 100644 index 000000000..f541d12ee --- /dev/null +++ b/src/test/resources/oneOf_discriminator-missing_1.yaml @@ -0,0 +1,44 @@ +openapi: 3.0.1 +info: + title: oneOf test for issue 29 + version: '1.0' +servers: + - url: 'http://localhost:8000/' +paths: + /state: + post: + operationId: update + requestBody: + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/A' + - $ref: '#/components/schemas/B' + required: true + responses: + '201': + description: OK +components: + schemas: + A: + type: object + properties: + realtype: + type: string + othertype: + type: string + message: + type: string + B: + type: object + properties: + realtype: + type: string + othertype: + type: string + description: + type: string + code: + type: integer + format: int32 \ No newline at end of file diff --git a/src/test/resources/oneOf_discriminator-missing_2.yaml b/src/test/resources/oneOf_discriminator-missing_2.yaml new file mode 100644 index 000000000..ea456bb79 --- /dev/null +++ b/src/test/resources/oneOf_discriminator-missing_2.yaml @@ -0,0 +1,44 @@ +openapi: 3.0.1 +info: + title: oneOf test for issue 29 + version: '1.0' +servers: + - url: 'http://localhost:8000/' +paths: + /state: + post: + operationId: update + requestBody: + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/B' + - $ref: '#/components/schemas/A' + required: true + responses: + '201': + description: OK +components: + schemas: + A: + type: object + properties: + realtype: + type: string + othertype: + type: string + message: + type: string + B: + type: object + properties: + realtype: + type: string + othertype: + type: string + description: + type: string + code: + type: integer + format: int32 \ No newline at end of file