From 11d2fa24bc0acf32862fb5f15b3381df9efdcd74 Mon Sep 17 00:00:00 2001 From: Timo Behrendt Date: Sun, 24 Jul 2022 20:09:28 +0200 Subject: [PATCH 1/3] added support for range HTTP status codes --- .../core/output/ConsoleRender.java | 2 +- .../core/output/MarkdownRender.java | 2 +- .../openapidiff/core/ConsoleRenderTest.java | 8 +++++ .../openapidiff/core/MarkdownRenderTest.java | 8 +++++ .../test/resources/range_statuscode_1.yaml | 35 +++++++++++++++++++ .../test/resources/range_statuscode_2.yaml | 35 +++++++++++++++++++ 6 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 core/src/test/resources/range_statuscode_1.yaml create mode 100644 core/src/test/resources/range_statuscode_2.yaml diff --git a/core/src/main/java/org/openapitools/openapidiff/core/output/ConsoleRender.java b/core/src/main/java/org/openapitools/openapidiff/core/output/ConsoleRender.java index 204f814ab..72597ad91 100644 --- a/core/src/main/java/org/openapitools/openapidiff/core/output/ConsoleRender.java +++ b/core/src/main/java/org/openapitools/openapidiff/core/output/ConsoleRender.java @@ -118,7 +118,7 @@ private String ul_response(ChangedApiResponse changedApiResponse) { private String itemResponse(String title, String code) { StringBuilder sb = new StringBuilder(); String status = ""; - if (!code.equals("default")) { + if (!code.equals("default") && !code.matches("[1-5]XX")) { status = HttpStatus.getReasonPhrase(Integer.parseInt(code)); } sb.append(StringUtils.repeat(' ', 4)) diff --git a/core/src/main/java/org/openapitools/openapidiff/core/output/MarkdownRender.java b/core/src/main/java/org/openapitools/openapidiff/core/output/MarkdownRender.java index ad74bd610..a0d2c5448 100644 --- a/core/src/main/java/org/openapitools/openapidiff/core/output/MarkdownRender.java +++ b/core/src/main/java/org/openapitools/openapidiff/core/output/MarkdownRender.java @@ -158,7 +158,7 @@ protected String itemResponse(String code, ChangedResponse response) { protected String itemResponse(String title, String code, String description) { StringBuilder sb = new StringBuilder(); String status = ""; - if (!code.equals("default")) { + if (!code.equals("default") && !code.matches("[1-5]XX")) { status = HttpStatus.getReasonPhrase(Integer.parseInt(code)); } sb.append(format("%s : **%s %s**\n", title, code, status)); diff --git a/core/src/test/java/org/openapitools/openapidiff/core/ConsoleRenderTest.java b/core/src/test/java/org/openapitools/openapidiff/core/ConsoleRenderTest.java index 329fea054..455631700 100644 --- a/core/src/test/java/org/openapitools/openapidiff/core/ConsoleRenderTest.java +++ b/core/src/test/java/org/openapitools/openapidiff/core/ConsoleRenderTest.java @@ -14,4 +14,12 @@ public void renderDoesNotFailWhenPropertyHasBeenRemoved() { OpenApiCompare.fromLocations("missing_property_1.yaml", "missing_property_2.yaml"); assertThat(render.render(diff)).isNotBlank(); } + + @Test + public void renderDoesNotFailWhenHTTPStatusCodeIsRange() { + ConsoleRender render = new ConsoleRender(); + ChangedOpenApi diff = + OpenApiCompare.fromLocations("range_statuscode_1.yaml", "range_statuscode_2.yaml"); + assertThat(render.render(diff)).isNotBlank(); + } } diff --git a/core/src/test/java/org/openapitools/openapidiff/core/MarkdownRenderTest.java b/core/src/test/java/org/openapitools/openapidiff/core/MarkdownRenderTest.java index 154fef127..36e007a8c 100644 --- a/core/src/test/java/org/openapitools/openapidiff/core/MarkdownRenderTest.java +++ b/core/src/test/java/org/openapitools/openapidiff/core/MarkdownRenderTest.java @@ -21,4 +21,12 @@ public void renderDoesNotCauseStackOverflowWithRecursiveDefinitions() { ChangedOpenApi diff = OpenApiCompare.fromLocations("recursive_old.yaml", "recursive_new.yaml"); assertThat(render.render(diff)).isNotBlank(); } + + @Test + public void renderDoesNotFailWhenHTTPStatusCodeIsRange() { + MarkdownRender render = new MarkdownRender(); + ChangedOpenApi diff = + OpenApiCompare.fromLocations("range_statuscode_1.yaml", "range_statuscode_2.yaml"); + assertThat(render.render(diff)).isNotBlank(); + } } diff --git a/core/src/test/resources/range_statuscode_1.yaml b/core/src/test/resources/range_statuscode_1.yaml new file mode 100644 index 000000000..28128a820 --- /dev/null +++ b/core/src/test/resources/range_statuscode_1.yaml @@ -0,0 +1,35 @@ +openapi: 3.0.0 +servers: + - url: 'http://petstore.swagger.io/v2' +info: + description: >- + This is a sample server Petstore server. You can find out more about + Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, + #swagger](http://swagger.io/irc/). For this sample, you can use the api key + `special-key` to test the authorization filters. + version: 1.0.0 + title: Swagger Petstore + termsOfService: 'http://swagger.io/terms/' + contact: + email: apiteam@swagger.io + license: + name: Apache 2.0 + url: 'http://www.apache.org/licenses/LICENSE-2.0.html' +paths: + /pet/{petId}: + get: + tags: + - pet + summary: gets a pet by id + description: '' + operationId: updatePetWithForm + parameters: + - name: petId + in: path + description: ID of pet that needs to be updated + required: true + schema: + type: integer + responses: + '405': + description: Invalid input \ No newline at end of file diff --git a/core/src/test/resources/range_statuscode_2.yaml b/core/src/test/resources/range_statuscode_2.yaml new file mode 100644 index 000000000..7b819cd63 --- /dev/null +++ b/core/src/test/resources/range_statuscode_2.yaml @@ -0,0 +1,35 @@ +openapi: 3.0.0 +servers: + - url: 'http://petstore.swagger.io/v2' +info: + description: >- + This is a sample server Petstore server. You can find out more about + Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, + #swagger](http://swagger.io/irc/). For this sample, you can use the api key + `special-key` to test the authorization filters. + version: 1.0.0 + title: Swagger Petstore + termsOfService: 'http://swagger.io/terms/' + contact: + email: apiteam@swagger.io + license: + name: Apache 2.0 + url: 'http://www.apache.org/licenses/LICENSE-2.0.html' +paths: + /pet/{petId}: + get: + tags: + - pet + summary: gets a pet by id + description: '' + operationId: updatePetWithForm + parameters: + - name: petId + in: path + description: ID of pet that needs to be updated + required: true + schema: + type: integer + responses: + '4XX': + description: Invalid input \ No newline at end of file From 3296e11b0ae7601d59c87d9fc8cfbb450f49433c Mon Sep 17 00:00:00 2001 From: Timo Behrendt Date: Sun, 24 Jul 2022 20:35:59 +0200 Subject: [PATCH 2/3] deflated test cases --- .../test/resources/range_statuscode_1.yaml | 25 +++---------------- .../test/resources/range_statuscode_2.yaml | 25 +++---------------- 2 files changed, 6 insertions(+), 44 deletions(-) diff --git a/core/src/test/resources/range_statuscode_1.yaml b/core/src/test/resources/range_statuscode_1.yaml index 28128a820..1f4adbfb1 100644 --- a/core/src/test/resources/range_statuscode_1.yaml +++ b/core/src/test/resources/range_statuscode_1.yaml @@ -1,35 +1,16 @@ openapi: 3.0.0 -servers: - - url: 'http://petstore.swagger.io/v2' info: - description: >- - This is a sample server Petstore server. You can find out more about - Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, - #swagger](http://swagger.io/irc/). For this sample, you can use the api key - `special-key` to test the authorization filters. + title: Projects API version: 1.0.0 - title: Swagger Petstore - termsOfService: 'http://swagger.io/terms/' - contact: - email: apiteam@swagger.io - license: - name: Apache 2.0 - url: 'http://www.apache.org/licenses/LICENSE-2.0.html' paths: /pet/{petId}: get: - tags: - - pet - summary: gets a pet by id - description: '' - operationId: updatePetWithForm parameters: - name: petId in: path - description: ID of pet that needs to be updated required: true schema: type: integer responses: - '405': - description: Invalid input \ No newline at end of file + "405": + description: "Invalid input" diff --git a/core/src/test/resources/range_statuscode_2.yaml b/core/src/test/resources/range_statuscode_2.yaml index 7b819cd63..e4f6de824 100644 --- a/core/src/test/resources/range_statuscode_2.yaml +++ b/core/src/test/resources/range_statuscode_2.yaml @@ -1,35 +1,16 @@ openapi: 3.0.0 -servers: - - url: 'http://petstore.swagger.io/v2' info: - description: >- - This is a sample server Petstore server. You can find out more about - Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, - #swagger](http://swagger.io/irc/). For this sample, you can use the api key - `special-key` to test the authorization filters. + title: Projects API version: 1.0.0 - title: Swagger Petstore - termsOfService: 'http://swagger.io/terms/' - contact: - email: apiteam@swagger.io - license: - name: Apache 2.0 - url: 'http://www.apache.org/licenses/LICENSE-2.0.html' paths: /pet/{petId}: get: - tags: - - pet - summary: gets a pet by id - description: '' - operationId: updatePetWithForm parameters: - name: petId in: path - description: ID of pet that needs to be updated required: true schema: type: integer responses: - '4XX': - description: Invalid input \ No newline at end of file + "4XX": + description: "Invalid input" From f0fa3fa5b4201cba83a8120143ce55449e1d9539 Mon Sep 17 00:00:00 2001 From: Timo Behrendt Date: Sun, 24 Jul 2022 20:45:44 +0200 Subject: [PATCH 3/3] deflated test cases even more --- core/src/test/resources/range_statuscode_1.yaml | 8 +------- core/src/test/resources/range_statuscode_2.yaml | 8 +------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/core/src/test/resources/range_statuscode_1.yaml b/core/src/test/resources/range_statuscode_1.yaml index 1f4adbfb1..4653a70fc 100644 --- a/core/src/test/resources/range_statuscode_1.yaml +++ b/core/src/test/resources/range_statuscode_1.yaml @@ -3,14 +3,8 @@ info: title: Projects API version: 1.0.0 paths: - /pet/{petId}: + /pet/: get: - parameters: - - name: petId - in: path - required: true - schema: - type: integer responses: "405": description: "Invalid input" diff --git a/core/src/test/resources/range_statuscode_2.yaml b/core/src/test/resources/range_statuscode_2.yaml index e4f6de824..d0ecbeb75 100644 --- a/core/src/test/resources/range_statuscode_2.yaml +++ b/core/src/test/resources/range_statuscode_2.yaml @@ -3,14 +3,8 @@ info: title: Projects API version: 1.0.0 paths: - /pet/{petId}: + /pet/: get: - parameters: - - name: petId - in: path - required: true - schema: - type: integer responses: "4XX": description: "Invalid input"