From e5e3ffb90e13b782fc9d7958beb23fd4461b9606 Mon Sep 17 00:00:00 2001 From: Aliaksandr Pinchuk Date: Thu, 24 Apr 2025 21:34:52 +0200 Subject: [PATCH 1/2] test: add AnyOf diff test cases for schema validation --- .../openapidiff/core/SchemaDiffTest.java | 28 +++++++++++++++ .../resources/schemaDiff/anyOf-diff-1.yaml | 35 +++++++++++++++++++ .../resources/schemaDiff/anyOf-diff-2.yaml | 33 +++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 core/src/test/resources/schemaDiff/anyOf-diff-1.yaml create mode 100644 core/src/test/resources/schemaDiff/anyOf-diff-2.yaml diff --git a/core/src/test/java/org/openapitools/openapidiff/core/SchemaDiffTest.java b/core/src/test/java/org/openapitools/openapidiff/core/SchemaDiffTest.java index d918fa99..268027a0 100644 --- a/core/src/test/java/org/openapitools/openapidiff/core/SchemaDiffTest.java +++ b/core/src/test/java/org/openapitools/openapidiff/core/SchemaDiffTest.java @@ -352,4 +352,32 @@ public void changePatternHandling() { assertThat(props.get("patternAdded").getPattern().getNewPattern()) .isEqualTo("^\\d{3}-\\d{2}-\\d{4}$?"); } + + @Test // issue #212 + public void testAnyOfDiff() { + ChangedOpenApi changedOpenApi = + OpenApiCompare.fromLocations( + "schemaDiff/anyOf-diff-1.yaml", "schemaDiff/anyOf-diff-2.yaml"); + ChangedSchema changedSchema = + getRequestBodyChangedSchema(changedOpenApi, POST, "/anyof/test", "application/json"); + + assertThat(changedSchema).isNotNull(); + // The diff compares the *merged* schema resulting from anyOf, not the anyOf structure itself. + // See details in #772 + assertThat(changedSchema.isChanged()).isEqualTo(DiffResult.COMPATIBLE); + + // fieldA only changed required status, not its schema + assertThat(changedSchema.getChangedProperties()).isEmpty(); + + // fieldB: Removed from the merged schema properties + assertThat(changedSchema.getMissingProperties()).containsKey("fieldB"); + + // fieldC: Added to the merged schema properties + assertThat(changedSchema.getIncreasedProperties()).containsKey("fieldC"); + + // Check the overall required list changes for the merged schema + assertThat(changedSchema.getRequired().isChanged()).isEqualTo(DiffResult.COMPATIBLE); + assertThat(changedSchema.getRequired().getMissing()).containsExactly("fieldA"); + assertThat(changedSchema.getRequired().getIncreased()).isEmpty(); + } } diff --git a/core/src/test/resources/schemaDiff/anyOf-diff-1.yaml b/core/src/test/resources/schemaDiff/anyOf-diff-1.yaml new file mode 100644 index 00000000..4420283e --- /dev/null +++ b/core/src/test/resources/schemaDiff/anyOf-diff-1.yaml @@ -0,0 +1,35 @@ +openapi: 3.0.0 +info: + title: AnyOf Diff Test - Version 1 + version: 1.0.0 +paths: + /anyof/test: + post: + summary: Test endpoint for anyOf diff + requestBody: + required: true + content: + application/json: + schema: + anyOf: + - $ref: '#/components/schemas/TypeA' + - $ref: '#/components/schemas/TypeB' + responses: + '200': + description: OK +components: + schemas: + TypeA: + type: object + properties: + fieldA: + type: string + commonField: + type: integer + required: + - fieldA + TypeB: + type: object + properties: + fieldB: + type: boolean diff --git a/core/src/test/resources/schemaDiff/anyOf-diff-2.yaml b/core/src/test/resources/schemaDiff/anyOf-diff-2.yaml new file mode 100644 index 00000000..4991cf45 --- /dev/null +++ b/core/src/test/resources/schemaDiff/anyOf-diff-2.yaml @@ -0,0 +1,33 @@ +openapi: 3.0.0 +info: + title: AnyOf Diff Test - Version 2 + version: 1.0.0 +paths: + /anyof/test: + post: + summary: Test endpoint for anyOf diff + requestBody: + required: true + content: + application/json: + schema: + anyOf: + - $ref: '#/components/schemas/TypeA' + - $ref: '#/components/schemas/TypeC' + responses: + '200': + description: OK +components: + schemas: + TypeA: + type: object + properties: + fieldA: + type: string + commonField: + type: integer + TypeC: + type: object + properties: + fieldC: + type: number From ab56913a9eedbbc990b3797635d11ff05fcdc433 Mon Sep 17 00:00:00 2001 From: Aliaksandr Pinchuk Date: Thu, 24 Apr 2025 21:40:14 +0200 Subject: [PATCH 2/2] test: add allOf diff test cases for schema validation --- .../openapidiff/core/SchemaDiffTest.java | 28 +++++++++++++++ .../resources/schemaDiff/allOf-diff-1.yaml | 35 +++++++++++++++++++ .../resources/schemaDiff/allOf-diff-2.yaml | 34 ++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 core/src/test/resources/schemaDiff/allOf-diff-1.yaml create mode 100644 core/src/test/resources/schemaDiff/allOf-diff-2.yaml diff --git a/core/src/test/java/org/openapitools/openapidiff/core/SchemaDiffTest.java b/core/src/test/java/org/openapitools/openapidiff/core/SchemaDiffTest.java index 268027a0..fc37659f 100644 --- a/core/src/test/java/org/openapitools/openapidiff/core/SchemaDiffTest.java +++ b/core/src/test/java/org/openapitools/openapidiff/core/SchemaDiffTest.java @@ -380,4 +380,32 @@ public void testAnyOfDiff() { assertThat(changedSchema.getRequired().getMissing()).containsExactly("fieldA"); assertThat(changedSchema.getRequired().getIncreased()).isEmpty(); } + + @Test // issue #212 - adapted for allOf + public void testAllOfDiff() { + ChangedOpenApi changedOpenApi = + OpenApiCompare.fromLocations( + "schemaDiff/allOf-diff-1.yaml", "schemaDiff/allOf-diff-2.yaml"); + ChangedSchema changedSchema = + getRequestBodyChangedSchema(changedOpenApi, POST, "/allof/test", "application/json"); + + assertThat(changedSchema).isNotNull(); + // The diff compares the *merged* schema resulting from allOf, not the allOf structure itself. + // See details in #772 + assertThat(changedSchema.isChanged()).isEqualTo(DiffResult.COMPATIBLE); + + // fieldA only changed required status, commonField is unchanged + assertThat(changedSchema.getChangedProperties()).isEmpty(); + + // fieldB: Removed from the merged schema properties + assertThat(changedSchema.getMissingProperties()).containsKey("fieldB"); + + // fieldC: Added to the merged schema properties + assertThat(changedSchema.getIncreasedProperties()).containsKey("fieldC"); + + // Check the overall required list changes for the merged schema + assertThat(changedSchema.getRequired().isChanged()).isEqualTo(DiffResult.COMPATIBLE); + assertThat(changedSchema.getRequired().getMissing()).containsExactly("fieldA"); + assertThat(changedSchema.getRequired().getIncreased()).isEmpty(); + } } diff --git a/core/src/test/resources/schemaDiff/allOf-diff-1.yaml b/core/src/test/resources/schemaDiff/allOf-diff-1.yaml new file mode 100644 index 00000000..9351c832 --- /dev/null +++ b/core/src/test/resources/schemaDiff/allOf-diff-1.yaml @@ -0,0 +1,35 @@ +openapi: 3.0.0 +info: + title: AllOf Diff Test - Version 1 + version: 1.0.0 +paths: + /allof/test: + post: + summary: Test endpoint for allOf diff + requestBody: + required: true + content: + application/json: + schema: + allOf: + - $ref: '#/components/schemas/TypeA' + - $ref: '#/components/schemas/TypeB' + responses: + '200': + description: OK +components: + schemas: + TypeA: + type: object + required: + - fieldA + properties: + fieldA: + type: string + commonField: + type: integer + TypeB: + type: object + properties: + fieldB: + type: boolean diff --git a/core/src/test/resources/schemaDiff/allOf-diff-2.yaml b/core/src/test/resources/schemaDiff/allOf-diff-2.yaml new file mode 100644 index 00000000..d3f808e7 --- /dev/null +++ b/core/src/test/resources/schemaDiff/allOf-diff-2.yaml @@ -0,0 +1,34 @@ +openapi: 3.0.0 +info: + title: AllOf Diff Test - Version 2 + version: 1.0.0 +paths: + /allof/test: + post: + summary: Test endpoint for allOf diff + requestBody: + required: true + content: + application/json: + schema: + allOf: + - $ref: '#/components/schemas/TypeA' + - $ref: '#/components/schemas/TypeC' + responses: + '200': + description: OK +components: + schemas: + TypeA: + type: object + # fieldA is no longer required here + properties: + fieldA: + type: string + commonField: + type: integer + TypeC: + type: object + properties: + fieldC: + type: number