From 65488aae0c23777b664faa0b66f04297a6677c78 Mon Sep 17 00:00:00 2001 From: Jeremie Bresson Date: Thu, 7 Jun 2018 13:27:44 +0200 Subject: [PATCH] Fix NullPointerException with oneOf discriminator change --- .../ComposedSchemaDiffResult.java | 1 + .../qdesrame/openapi/test/OneOfDiffTest.java | 8 +++ .../oneOf_discriminator-changed_1.yaml | 49 +++++++++++++++++++ .../oneOf_discriminator-changed_2.yaml | 49 +++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 src/test/resources/oneOf_discriminator-changed_1.yaml create mode 100644 src/test/resources/oneOf_discriminator-changed_2.yaml 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 ebf61d5a5..fe14d8425 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 @@ -46,6 +46,7 @@ public Optional diff(HashSet refSet, Components leftCompo changedSchema.setOldSchema(left); changedSchema.setNewSchema(right); changedSchema.setDiscriminatorPropertyChanged(true); + changedSchema.setContext(context); return Optional.of(changedSchema); } diff --git a/src/test/java/com/qdesrame/openapi/test/OneOfDiffTest.java b/src/test/java/com/qdesrame/openapi/test/OneOfDiffTest.java index 5c5918ccd..3793d7dd4 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_DOC3 = "oneOf_diff_3.yaml"; private final String OPENAPI_DOC4 = "composed_schema_1.yaml"; 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"; @Test public void testDiffSame() { @@ -37,4 +39,10 @@ public void testComposedSchema() { assertOpenApiBackwardIncompatible(OPENAPI_DOC4, OPENAPI_DOC5); } + @Test + public void testOneOfDiscrimitatorChanged() { + //The oneOf 'discriminator' changed: 'realtype' -> 'othertype': + assertOpenApiBackwardIncompatible(OPENAPI_DOC6, OPENAPI_DOC7); + } + } diff --git a/src/test/resources/oneOf_discriminator-changed_1.yaml b/src/test/resources/oneOf_discriminator-changed_1.yaml new file mode 100644 index 000000000..c0d88f136 --- /dev/null +++ b/src/test/resources/oneOf_discriminator-changed_1.yaml @@ -0,0 +1,49 @@ +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' + discriminator: + propertyName: realtype + mapping: + a-type: '#/components/schemas/A' + b-type: '#/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-changed_2.yaml b/src/test/resources/oneOf_discriminator-changed_2.yaml new file mode 100644 index 000000000..c479c17d6 --- /dev/null +++ b/src/test/resources/oneOf_discriminator-changed_2.yaml @@ -0,0 +1,49 @@ +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' + discriminator: + propertyName: othertype + mapping: + a-type: '#/components/schemas/A' + b-type: '#/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