Skip to content

Commit da18a85

Browse files
authored
Fix: Renderers fail when processing range HTTP status code such as '2XX' (#397)
Both Markdown and Console renderer fail when trying to process a range HTTP status code, e. g. '2XX'. So any diff that results in an output including such a status code fails to render for markdown and console. The error occurs when trying to parse the String HTTP status code into an Integer right here (both in Markdown and Console renderer): ```java if (!code.equals("default")) { // Integer.parseInt(code) fails when passing e. g. '2XX' status = HttpStatus.getReasonPhrase(Integer.parseInt(code)); } ``` JSON and HTML renderer are not affected, since they do not try to display the 'reason phrase'. Everything else, the actual diff process etc. is also not affected. # Expected behavior Expected ranges of HTTP status codes, like '2XX' covering all codes between 200-299, to be treated like any other HTTP status code, since they have been part of the OpenAPI specification since version [3.0.0](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#patterned-fields-1).
1 parent f96f35d commit da18a85

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

core/src/main/java/org/openapitools/openapidiff/core/output/ConsoleRender.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private String ul_response(ChangedApiResponse changedApiResponse) {
118118
private String itemResponse(String title, String code) {
119119
StringBuilder sb = new StringBuilder();
120120
String status = "";
121-
if (!code.equals("default")) {
121+
if (!code.equals("default") && !code.matches("[1-5]XX")) {
122122
status = HttpStatus.getReasonPhrase(Integer.parseInt(code));
123123
}
124124
sb.append(StringUtils.repeat(' ', 4))

core/src/main/java/org/openapitools/openapidiff/core/output/MarkdownRender.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ protected String itemResponse(String code, ChangedResponse response) {
158158
protected String itemResponse(String title, String code, String description) {
159159
StringBuilder sb = new StringBuilder();
160160
String status = "";
161-
if (!code.equals("default")) {
161+
if (!code.equals("default") && !code.matches("[1-5]XX")) {
162162
status = HttpStatus.getReasonPhrase(Integer.parseInt(code));
163163
}
164164
sb.append(format("%s : **%s %s**\n", title, code, status));

core/src/test/java/org/openapitools/openapidiff/core/ConsoleRenderTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@ public void renderDoesNotFailWhenPropertyHasBeenRemoved() {
1414
OpenApiCompare.fromLocations("missing_property_1.yaml", "missing_property_2.yaml");
1515
assertThat(render.render(diff)).isNotBlank();
1616
}
17+
18+
@Test
19+
public void renderDoesNotFailWhenHTTPStatusCodeIsRange() {
20+
ConsoleRender render = new ConsoleRender();
21+
ChangedOpenApi diff =
22+
OpenApiCompare.fromLocations("range_statuscode_1.yaml", "range_statuscode_2.yaml");
23+
assertThat(render.render(diff)).isNotBlank();
24+
}
1725
}

core/src/test/java/org/openapitools/openapidiff/core/MarkdownRenderTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@ public void renderDoesNotCauseStackOverflowWithRecursiveDefinitions() {
2121
ChangedOpenApi diff = OpenApiCompare.fromLocations("recursive_old.yaml", "recursive_new.yaml");
2222
assertThat(render.render(diff)).isNotBlank();
2323
}
24+
25+
@Test
26+
public void renderDoesNotFailWhenHTTPStatusCodeIsRange() {
27+
MarkdownRender render = new MarkdownRender();
28+
ChangedOpenApi diff =
29+
OpenApiCompare.fromLocations("range_statuscode_1.yaml", "range_statuscode_2.yaml");
30+
assertThat(render.render(diff)).isNotBlank();
31+
}
2432
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Projects API
4+
version: 1.0.0
5+
paths:
6+
/pet/:
7+
get:
8+
responses:
9+
"405":
10+
description: "Invalid input"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Projects API
4+
version: 1.0.0
5+
paths:
6+
/pet/:
7+
get:
8+
responses:
9+
"4XX":
10+
description: "Invalid input"

0 commit comments

Comments
 (0)