Skip to content

Commit 7762956

Browse files
CopilotDrSatyr
andcommitted
Fix NullPointerException in HtmlRender when schema is null
Co-authored-by: DrSatyr <8143518+DrSatyr@users.noreply.github.com>
1 parent cfb5e5b commit 7762956

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,15 @@ private LiTag li_missingRequest(String name, MediaType request) {
342342
}
343343

344344
private LiTag li_changedRequest(String name, ChangedMediaType request) {
345-
LiTag li =
346-
li().with(div_changedSchema(request.getSchema()))
347-
.withText(String.format("Changed body: '%s'", name));
348-
if (request.isIncompatible() && !showAllChanges) {
349-
incompatibilities(li, request.getSchema());
350-
} else if (showAllChanges) {
351-
allChanges(li, request.getSchema());
345+
LiTag li = li().withText(String.format("Changed body: '%s'", name));
346+
ChangedSchema schema = request.getSchema();
347+
if (schema != null) {
348+
li.with(div_changedSchema(schema));
349+
if (request.isIncompatible() && !showAllChanges) {
350+
incompatibilities(li, schema);
351+
} else if (showAllChanges) {
352+
allChanges(li, schema);
353+
}
352354
}
353355
return li;
354356
}

core/src/test/java/org/openapitools/openapidiff/core/output/HtmlRenderTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,26 @@ public void renderDoesNotFailWhenPropertyHasBeenRemoved() {
1919
render.render(diff, outputStreamWriter);
2020
assertThat(outputStream.toString()).isNotBlank();
2121
}
22+
23+
@Test
24+
public void renderDoesNotFailWhenSchemaIsNullButExampleChanged() {
25+
HtmlRender render = new HtmlRender();
26+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
27+
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
28+
ChangedOpenApi diff =
29+
OpenApiCompare.fromLocations("null_schema_issue_1.yaml", "null_schema_issue_2.yaml");
30+
render.render(diff, outputStreamWriter);
31+
assertThat(outputStream.toString()).isNotBlank();
32+
}
33+
34+
@Test
35+
public void renderWithShowAllChangesDoesNotFailWhenSchemaIsNullButExampleChanged() {
36+
HtmlRender render = new HtmlRender(true);
37+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
38+
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
39+
ChangedOpenApi diff =
40+
OpenApiCompare.fromLocations("null_schema_issue_1.yaml", "null_schema_issue_2.yaml");
41+
render.render(diff, outputStreamWriter);
42+
assertThat(outputStream.toString()).isNotBlank();
43+
}
2244
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
openapi: 3.0.2
2+
info:
3+
description: Test for null schema issue
4+
title: Null Schema Test
5+
version: 1.0.0
6+
paths:
7+
/test:
8+
post:
9+
requestBody:
10+
content:
11+
application/json:
12+
example: original example
13+
schema:
14+
type: string
15+
responses:
16+
'200':
17+
description: Success
18+
content:
19+
application/json:
20+
schema:
21+
type: string
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
openapi: 3.0.2
2+
info:
3+
description: Test for null schema issue
4+
title: Null Schema Test
5+
version: 1.0.0
6+
paths:
7+
/test:
8+
post:
9+
requestBody:
10+
content:
11+
application/json:
12+
example: updated example
13+
schema:
14+
type: string
15+
responses:
16+
'200':
17+
description: Success
18+
content:
19+
application/json:
20+
schema:
21+
type: string

0 commit comments

Comments
 (0)