Skip to content

Commit 036440f

Browse files
authored
Fix NPE when response changed from no schema to any schema (#1)
1 parent 0818dd5 commit 036440f

File tree

5 files changed

+268
-2
lines changed

5 files changed

+268
-2
lines changed

src/main/java/com/qdesrame/openapi/diff/output/ConsoleRender.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ protected Schema resolve(Schema schema) {
234234

235235
protected String type(Schema schema) {
236236
String result = "object";
237-
if (schema instanceof ArraySchema) {
237+
if (schema == null) {
238+
result = "no schema";
239+
} else if (schema instanceof ArraySchema) {
238240
result = "array";
239241
} else if (schema.getType() != null) {
240242
result = schema.getType();

src/main/java/com/qdesrame/openapi/diff/output/HtmlRender.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ protected Schema resolve(Schema schema) {
288288

289289
protected String type(Schema schema) {
290290
String result = "object";
291-
if (schema instanceof ArraySchema) {
291+
if (schema == null) {
292+
result = "no schema";
293+
} else if (schema instanceof ArraySchema) {
292294
result = "array";
293295
} else if (schema.getType() != null) {
294296
result = schema.getType();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.qdesrame.openapi.test;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import com.qdesrame.openapi.diff.OpenApiCompare;
6+
import com.qdesrame.openapi.diff.model.ChangedOpenApi;
7+
import com.qdesrame.openapi.diff.model.ChangedResponse;
8+
import com.qdesrame.openapi.diff.output.ConsoleRender;
9+
import com.qdesrame.openapi.diff.output.HtmlRender;
10+
import com.qdesrame.openapi.diff.output.MarkdownRender;
11+
import io.swagger.v3.oas.models.media.Content;
12+
import java.util.Map;
13+
import org.junit.jupiter.api.Test;
14+
15+
public class ResponseAddedContentSchemaTest {
16+
17+
private final String OPENAPI_DOC1 = "response_schema_added_1.yaml";
18+
private final String OPENAPI_DOC2 = "response_schema_added_2.yaml";
19+
20+
@Test
21+
public void testDiffDifferent() {
22+
ChangedOpenApi changedOpenApi = OpenApiCompare.fromLocations(OPENAPI_DOC1, OPENAPI_DOC2);
23+
24+
assertThat(changedOpenApi.getNewEndpoints()).isEmpty();
25+
assertThat(changedOpenApi.getMissingEndpoints()).isEmpty();
26+
assertThat(changedOpenApi.getChangedOperations()).isNotEmpty();
27+
28+
Map<String, ChangedResponse> changedResponses =
29+
changedOpenApi.getChangedOperations().get(0).getApiResponses().getChanged();
30+
assertThat(changedResponses).containsKey("200");
31+
32+
ChangedResponse changedResponse = changedResponses.get("200");
33+
Content oldContent = changedResponse.getOldApiResponse().getContent();
34+
Content newContent = changedResponse.getNewApiResponse().getContent();
35+
assertThat(oldContent.get("application/json").getSchema()).isNull();
36+
assertThat(newContent.get("application/json").getSchema()).isNotNull();
37+
}
38+
39+
@Test
40+
public void testDiffCanBeRendered() {
41+
ChangedOpenApi changedOpenApi = OpenApiCompare.fromLocations(OPENAPI_DOC1, OPENAPI_DOC2);
42+
43+
assertThat(new ConsoleRender().render(changedOpenApi)).isNotBlank();
44+
assertThat(new HtmlRender().render(changedOpenApi)).isNotBlank();
45+
assertThat(new MarkdownRender().render(changedOpenApi)).isNotBlank();
46+
}
47+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://petstore.swagger.io/v1
9+
paths:
10+
/pets:
11+
get:
12+
summary: List all pets
13+
operationId: listPets
14+
tags:
15+
- pets
16+
parameters:
17+
- name: limit
18+
in: query
19+
description: How many items to return at one time (max 100)
20+
required: false
21+
schema:
22+
type: integer
23+
format: int32
24+
responses:
25+
'200':
26+
description: A paged array of pets
27+
headers:
28+
x-next:
29+
description: A link to the next page of responses
30+
schema:
31+
type: string
32+
content:
33+
application/json: {}
34+
default:
35+
description: unexpected error
36+
content:
37+
application/json:
38+
schema:
39+
$ref: "#/components/schemas/Error"
40+
post:
41+
summary: Create a pet
42+
operationId: createPets
43+
tags:
44+
- pets
45+
responses:
46+
'201':
47+
description: Null response
48+
default:
49+
description: unexpected error
50+
content:
51+
application/json:
52+
schema:
53+
$ref: "#/components/schemas/Error"
54+
/pets/{petId}:
55+
get:
56+
summary: Info for a specific pet
57+
operationId: showPetById
58+
tags:
59+
- pets
60+
parameters:
61+
- name: petId
62+
in: path
63+
required: true
64+
description: The id of the pet to retrieve
65+
schema:
66+
type: string
67+
responses:
68+
'200':
69+
description: Expected response to a valid request
70+
content:
71+
application/json:
72+
$ref: "#/components/schemas/Pets"
73+
default:
74+
description: unexpected error
75+
content:
76+
application/json:
77+
schema:
78+
$ref: "#/components/schemas/Error"
79+
components:
80+
schemas:
81+
Pet:
82+
required:
83+
- id
84+
- name
85+
properties:
86+
id:
87+
type: integer
88+
format: int64
89+
name:
90+
type: string
91+
tag:
92+
type: string
93+
Pets:
94+
type: array
95+
items:
96+
$ref: "#/components/schemas/Pet"
97+
Error:
98+
required:
99+
- code
100+
- message
101+
properties:
102+
code:
103+
type: integer
104+
format: int32
105+
message:
106+
type: string
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://petstore.swagger.io/v1
9+
paths:
10+
/pets:
11+
get:
12+
summary: List all pets
13+
operationId: listPets
14+
tags:
15+
- pets
16+
parameters:
17+
- name: limit
18+
in: query
19+
description: How many items to return at one time (max 100)
20+
required: false
21+
schema:
22+
type: integer
23+
format: int32
24+
responses:
25+
'200':
26+
description: A paged array of pets
27+
headers:
28+
x-next:
29+
description: A link to the next page of responses
30+
schema:
31+
type: string
32+
content:
33+
application/json:
34+
schema:
35+
$ref: "#/components/schemas/Pets"
36+
default:
37+
description: unexpected error
38+
content:
39+
application/json:
40+
schema:
41+
$ref: "#/components/schemas/Error"
42+
post:
43+
summary: Create a pet
44+
operationId: createPets
45+
tags:
46+
- pets
47+
responses:
48+
'201':
49+
description: Null response
50+
default:
51+
description: unexpected error
52+
content:
53+
application/json:
54+
schema:
55+
$ref: "#/components/schemas/Error"
56+
/pets/{petId}:
57+
get:
58+
summary: Info for a specific pet
59+
operationId: showPetById
60+
tags:
61+
- pets
62+
parameters:
63+
- name: petId
64+
in: path
65+
required: true
66+
description: The id of the pet to retrieve
67+
schema:
68+
type: string
69+
responses:
70+
'200':
71+
description: Expected response to a valid request
72+
content:
73+
application/json:
74+
schema:
75+
$ref: "#/components/schemas/Pets"
76+
default:
77+
description: unexpected error
78+
content:
79+
application/json:
80+
schema:
81+
$ref: "#/components/schemas/Error"
82+
components:
83+
schemas:
84+
Pet:
85+
required:
86+
- id
87+
- name
88+
properties:
89+
id:
90+
type: integer
91+
format: int64
92+
name:
93+
type: string
94+
tag:
95+
type: string
96+
Pets:
97+
type: array
98+
items:
99+
$ref: "#/components/schemas/Pet"
100+
Error:
101+
required:
102+
- code
103+
- message
104+
properties:
105+
code:
106+
type: integer
107+
format: int32
108+
message:
109+
type: string

0 commit comments

Comments
 (0)