From db44f7a1e155f44e358765ed674d24120eda8d93 Mon Sep 17 00:00:00 2001 From: Inf1mum Date: Sun, 15 Nov 2026 21:33:30 +0300 Subject: [PATCH] Add regression tests for phantom media type changes with byte format examples swagger-parser deserializes the example of a media type whose schema is type string / format byte as a byte array. The examples diff introduced in 2.1.0-beta.12 (#671) compared such examples with Objects.equals, which is identity for arrays, so every media type with a byte example was reported as changed even when both specs were identical. Those changed media types carry no ChangedSchema, which is what made the Markdown and HTML renderers throw a NullPointerException when generating reports for this input. The comparison and the renderers have already been fixed on master (#836, #869, #871), but nothing covered the exact scenario from the report. The new fixtures are taken from that report: six response media types with byte format examples and shared component responses. The tests pin that identical specs produce no changes, that an example-only change is a backward compatible metadata change with no schema change attached, and that all renderers produce a report without failing. Signed-off-by: Inf1mum --- .../openapidiff/core/ContentDiffTest.java | 46 +++++ .../core/output/AsciidocRenderTest.java | 13 ++ .../core/output/ConsoleRenderTest.java | 15 ++ .../core/output/HtmlRenderTest.java | 13 ++ .../core/output/MarkdownRenderTest.java | 13 ++ .../issue-805-byte-example-media-types-1.yaml | 159 ++++++++++++++++++ .../issue-805-byte-example-media-types-2.yaml | 159 ++++++++++++++++++ 7 files changed, 418 insertions(+) create mode 100644 core/src/test/resources/issue-805-byte-example-media-types-1.yaml create mode 100644 core/src/test/resources/issue-805-byte-example-media-types-2.yaml diff --git a/core/src/test/java/org/openapitools/openapidiff/core/ContentDiffTest.java b/core/src/test/java/org/openapitools/openapidiff/core/ContentDiffTest.java index e3cf03534..5d65dd3d1 100644 --- a/core/src/test/java/org/openapitools/openapidiff/core/ContentDiffTest.java +++ b/core/src/test/java/org/openapitools/openapidiff/core/ContentDiffTest.java @@ -2,8 +2,13 @@ import static org.assertj.core.api.Assertions.assertThat; +import java.util.List; +import java.util.Map; import org.junit.jupiter.api.Test; +import org.openapitools.openapidiff.core.model.ChangedMediaType; import org.openapitools.openapidiff.core.model.ChangedOpenApi; +import org.openapitools.openapidiff.core.model.ChangedOperation; +import org.openapitools.openapidiff.core.model.ChangedResponse; public class ContentDiffTest { @@ -59,4 +64,45 @@ public void testRemovedRequestContentTypeDiff() { "content_type_request_add_2.yaml", "content_type_request_add_1.yaml"); assertThat(changedOpenApi.isCompatible()).isFalse(); } + + @Test + public void issue805_identicalSpecsWithByteFormatExamplesHaveNoMediaTypeChanges() { + ChangedOpenApi changedOpenApi = + OpenApiCompare.fromLocations( + "issue-805-byte-example-media-types-1.yaml", + "issue-805-byte-example-media-types-1.yaml"); + assertThat(changedOpenApi.isUnchanged()).isTrue(); + assertThat(changedOpenApi.isDifferent()).isFalse(); + assertThat(changedOpenApi.isIncompatible()).isFalse(); + assertThat(changedOpenApi.isCompatible()).isTrue(); + } + + @Test + public void issue805_byteFormatExampleChangeIsReportedAsBackwardCompatible() { + ChangedOpenApi changedOpenApi = + OpenApiCompare.fromLocations( + "issue-805-byte-example-media-types-1.yaml", + "issue-805-byte-example-media-types-2.yaml"); + assertThat(changedOpenApi.isDifferent()).isTrue(); + assertThat(changedOpenApi.isIncompatible()).isFalse(); + assertThat(changedOpenApi.isCompatible()).isTrue(); + + List changedOperations = changedOpenApi.getChangedOperations(); + assertThat(changedOperations).hasSize(1); + ChangedResponse changedResponse = + changedOperations.get(0).getApiResponses().getChanged().get("200"); + assertThat(changedResponse).isNotNull(); + + // every change comes from the byte format example alone, so no schema change is attached + Map changedMediaTypes = changedResponse.getContent().getChanged(); + assertThat(changedMediaTypes) + .containsOnlyKeys( + "text/csv", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "application/pdf", + "application/x-bibtex", + "application/marc", + "text/xml"); + changedMediaTypes.values().forEach(mediaType -> assertThat(mediaType.getSchema()).isNull()); + } } diff --git a/core/src/test/java/org/openapitools/openapidiff/core/output/AsciidocRenderTest.java b/core/src/test/java/org/openapitools/openapidiff/core/output/AsciidocRenderTest.java index a9e7937b1..3dc3160ab 100644 --- a/core/src/test/java/org/openapitools/openapidiff/core/output/AsciidocRenderTest.java +++ b/core/src/test/java/org/openapitools/openapidiff/core/output/AsciidocRenderTest.java @@ -105,4 +105,17 @@ public void issue870_renderDoesNotFailWhenSchemaIsNullButExampleChanged() { render.render(diff, outputStreamWriter); assertThat(outputStream.toString()).isNotBlank(); } + + @Test + public void issue805_renderDoesNotFailWhenByteExampleMediaTypesChangeWithNoSchemaChanges() { + AsciidocRender render = new AsciidocRender(); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); + ChangedOpenApi diff = + OpenApiCompare.fromLocations( + "issue-805-byte-example-media-types-1.yaml", + "issue-805-byte-example-media-types-2.yaml"); + render.render(diff, outputStreamWriter); + assertThat(outputStream.toString()).isNotBlank().contains("Changed text/csv"); + } } diff --git a/core/src/test/java/org/openapitools/openapidiff/core/output/ConsoleRenderTest.java b/core/src/test/java/org/openapitools/openapidiff/core/output/ConsoleRenderTest.java index a193e4e51..421afeb14 100644 --- a/core/src/test/java/org/openapitools/openapidiff/core/output/ConsoleRenderTest.java +++ b/core/src/test/java/org/openapitools/openapidiff/core/output/ConsoleRenderTest.java @@ -88,4 +88,19 @@ public void issue870_renderDoesNotFailWhenSchemaIsNullButExampleChanged() { render.render(diff, outputStreamWriter); assertThat(outputStream.toString()).isNotBlank(); } + + @Test + public void issue805_renderDoesNotFailWhenByteExampleMediaTypesChangeWithNoSchemaChanges() { + ConsoleRender render = new ConsoleRender(); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); + ChangedOpenApi diff = + OpenApiCompare.fromLocations( + "issue-805-byte-example-media-types-1.yaml", + "issue-805-byte-example-media-types-2.yaml"); + render.render(diff, outputStreamWriter); + assertThat(outputStream.toString()) + .isNotBlank() + .containsSubsequence("Media types:", "- Changed text/csv", "Backward compatible"); + } } diff --git a/core/src/test/java/org/openapitools/openapidiff/core/output/HtmlRenderTest.java b/core/src/test/java/org/openapitools/openapidiff/core/output/HtmlRenderTest.java index 2d770e00c..1907eed2f 100644 --- a/core/src/test/java/org/openapitools/openapidiff/core/output/HtmlRenderTest.java +++ b/core/src/test/java/org/openapitools/openapidiff/core/output/HtmlRenderTest.java @@ -43,4 +43,17 @@ public void issue865_renderWithShowAllChangesDoesNotFailWhenSchemaIsNullButExamp render.render(diff, outputStreamWriter); assertThat(outputStream.toString()).isNotBlank(); } + + @Test + public void issue805_renderDoesNotFailWhenByteExampleMediaTypesChangeWithNoSchemaChanges() { + HtmlRender render = new HtmlRender(); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); + ChangedOpenApi diff = + OpenApiCompare.fromLocations( + "issue-805-byte-example-media-types-1.yaml", + "issue-805-byte-example-media-types-2.yaml"); + render.render(diff, outputStreamWriter); + assertThat(outputStream.toString()).isNotBlank().contains("text/csv"); + } } diff --git a/core/src/test/java/org/openapitools/openapidiff/core/output/MarkdownRenderTest.java b/core/src/test/java/org/openapitools/openapidiff/core/output/MarkdownRenderTest.java index 597882105..ad587d993 100644 --- a/core/src/test/java/org/openapitools/openapidiff/core/output/MarkdownRenderTest.java +++ b/core/src/test/java/org/openapitools/openapidiff/core/output/MarkdownRenderTest.java @@ -52,4 +52,17 @@ public void issue870_renderDoesNotFailWhenSchemaIsNullButExampleChanged() { render.render(diff, outputStreamWriter); assertThat(outputStream.toString()).isNotBlank(); } + + @Test + public void issue805_renderDoesNotFailWhenByteExampleMediaTypesChangeWithNoSchemaChanges() { + MarkdownRender render = new MarkdownRender(); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); + ChangedOpenApi diff = + OpenApiCompare.fromLocations( + "issue-805-byte-example-media-types-1.yaml", + "issue-805-byte-example-media-types-2.yaml"); + render.render(diff, outputStreamWriter); + assertThat(outputStream.toString()).isNotBlank().contains("Changed content type : `text/csv`"); + } } diff --git a/core/src/test/resources/issue-805-byte-example-media-types-1.yaml b/core/src/test/resources/issue-805-byte-example-media-types-1.yaml new file mode 100644 index 000000000..a8db6983a --- /dev/null +++ b/core/src/test/resources/issue-805-byte-example-media-types-1.yaml @@ -0,0 +1,159 @@ +openapi: '3.0.3' +info: + license: + name: '' + termsOfService: '' + title: "my test service" + version: 'Latest' +tags: + - name: mytestTag + description: endpoints +paths: + /v4/mytest/formats/{downloadFormat}/records/{recordId}: + get: + tags: + - mytestTag + summary: Converts record to provided format + operationId: getV4DownloadsFormatsRecords + deprecated: false + security: + - Authorization: [ ] + parameters: + - name: downloadFormat + in: path + description: Export format + required: true + schema: + type: string + example: 'csv' + - name: recordId + in: path + description: Unique record identifier + required: true + example: 'ax74x2wyin' + schema: + type: string + responses: + '200': + description: OK + content: + text/csv: + schema: + type: string + format: byte + example: 'blah' + application/vnd.openxmlformats-officedocument.wordprocessingml.document: + schema: + type: string + format: byte + example: 'blah' + application/pdf: + schema: + type: string + format: byte + example: 'blah' + application/x-bibtex: + schema: + type: string + format: byte + example: 'blah' + application/marc: + schema: + type: string + format: byte + example: 'blah' + text/xml: + schema: + type: string + format: byte + example: 'blah' + '400': + $ref: '#/components/responses/BadRequest' + '401': + $ref: '#/components/responses/Unauthorized' + '403': + $ref: '#/components/responses/Forbidden' + '404': + $ref: '#/components/responses/NotFound' +components: + responses: + BadRequest: + description: 'BadRequest' + content: + application/json: + example: + name: BAD_REQUEST + details: + - field: '#/login/username' + issue: BAD_REQUEST + location: body + debugId: '123456789' + message: Request contains errors + informationLink: 'http://developer.foo.com/apidoc/blah#BAD_REQUEST' + schema: + $ref: '#/components/schemas/ErrorResponse' + NotFound: + description: 'NotFound' + content: + application/json: + example: + name: NOT_FOUND + details: + - field: '#/login/username' + issue: USERNAME_IS_NOT_FOUND + location: body + debugId: '123456789' + message: Item is not found + informationLink: 'http://developer.foo.com/apidoc/blah#USERNAME_IS_NOT_FOUND' + schema: + $ref: '#/components/schemas/ErrorResponse' + Unauthorized: + description: 'Unauthorized' + content: + application/json: + example: + name: UNAUTHORIZED + details: + - field: '#/login/username' + issue: NOT_AUTHORIZED + location: body + debugId: '123456789' + message: User is not authorized + informationLink: 'http://developer.foo.com/apidoc/blah#NOT_AUTHORIZED' + schema: + $ref: '#/components/schemas/ErrorResponse' + Forbidden: + description: 'Forbidden' + content: + application/json: + example: + name: FORBIDDEN + details: + - field: '#/login/username' + issue: FORBIDDEN + location: body + debugId: '123456789' + message: Access denied + informationLink: 'http://developer.foo.com/apidoc/blah#FORBIDDEN' + schema: + $ref: '#/components/schemas/ErrorResponse' + schemas: + ErrorResponse: + type: object + description: Common Error response + x-examples: + example-1: + name: VALIDATION_ERROR + details: + - field: '#/login/username' + issue: REQUIRED_FIELD_MISSING + location: body + debugId: '123456789' + message: Invalid data provided + informationLink: 'http://developer.foo.com/apidoc/blah#VALIDATION_ERROR' + securitySchemes: + Authorization: + description: Authorization token + type: http + scheme: bearer + bearerFormat: JWT diff --git a/core/src/test/resources/issue-805-byte-example-media-types-2.yaml b/core/src/test/resources/issue-805-byte-example-media-types-2.yaml new file mode 100644 index 000000000..209cfdc1c --- /dev/null +++ b/core/src/test/resources/issue-805-byte-example-media-types-2.yaml @@ -0,0 +1,159 @@ +openapi: '3.0.3' +info: + license: + name: '' + termsOfService: '' + title: "my test service" + version: 'Latest' +tags: + - name: mytestTag + description: endpoints +paths: + /v4/mytest/formats/{downloadFormat}/records/{recordId}: + get: + tags: + - mytestTag + summary: Converts record to provided format + operationId: getV4DownloadsFormatsRecords + deprecated: false + security: + - Authorization: [ ] + parameters: + - name: downloadFormat + in: path + description: Export format + required: true + schema: + type: string + example: 'csv' + - name: recordId + in: path + description: Unique record identifier + required: true + example: 'ax74x2wyin' + schema: + type: string + responses: + '200': + description: OK + content: + text/csv: + schema: + type: string + format: byte + example: 'blob' + application/vnd.openxmlformats-officedocument.wordprocessingml.document: + schema: + type: string + format: byte + example: 'blob' + application/pdf: + schema: + type: string + format: byte + example: 'blob' + application/x-bibtex: + schema: + type: string + format: byte + example: 'blob' + application/marc: + schema: + type: string + format: byte + example: 'blob' + text/xml: + schema: + type: string + format: byte + example: 'blob' + '400': + $ref: '#/components/responses/BadRequest' + '401': + $ref: '#/components/responses/Unauthorized' + '403': + $ref: '#/components/responses/Forbidden' + '404': + $ref: '#/components/responses/NotFound' +components: + responses: + BadRequest: + description: 'BadRequest' + content: + application/json: + example: + name: BAD_REQUEST + details: + - field: '#/login/username' + issue: BAD_REQUEST + location: body + debugId: '123456789' + message: Request contains errors + informationLink: 'http://developer.foo.com/apidoc/blah#BAD_REQUEST' + schema: + $ref: '#/components/schemas/ErrorResponse' + NotFound: + description: 'NotFound' + content: + application/json: + example: + name: NOT_FOUND + details: + - field: '#/login/username' + issue: USERNAME_IS_NOT_FOUND + location: body + debugId: '123456789' + message: Item is not found + informationLink: 'http://developer.foo.com/apidoc/blah#USERNAME_IS_NOT_FOUND' + schema: + $ref: '#/components/schemas/ErrorResponse' + Unauthorized: + description: 'Unauthorized' + content: + application/json: + example: + name: UNAUTHORIZED + details: + - field: '#/login/username' + issue: NOT_AUTHORIZED + location: body + debugId: '123456789' + message: User is not authorized + informationLink: 'http://developer.foo.com/apidoc/blah#NOT_AUTHORIZED' + schema: + $ref: '#/components/schemas/ErrorResponse' + Forbidden: + description: 'Forbidden' + content: + application/json: + example: + name: FORBIDDEN + details: + - field: '#/login/username' + issue: FORBIDDEN + location: body + debugId: '123456789' + message: Access denied + informationLink: 'http://developer.foo.com/apidoc/blah#FORBIDDEN' + schema: + $ref: '#/components/schemas/ErrorResponse' + schemas: + ErrorResponse: + type: object + description: Common Error response + x-examples: + example-1: + name: VALIDATION_ERROR + details: + - field: '#/login/username' + issue: REQUIRED_FIELD_MISSING + location: body + debugId: '123456789' + message: Invalid data provided + informationLink: 'http://developer.foo.com/apidoc/blah#VALIDATION_ERROR' + securitySchemes: + Authorization: + description: Authorization token + type: http + scheme: bearer + bearerFormat: JWT