Skip to content

Commit f928125

Browse files
CopilotDrSatyr
andauthored
Fix NullPointerException in HtmlRender when schema is null (#869)
* Initial plan * Fix NullPointerException in HtmlRender when schema is null Co-authored-by: DrSatyr <8143518+DrSatyr@users.noreply.github.com> * Rename test methods and YAML files to include issue ID #865 Co-authored-by: DrSatyr <8143518+DrSatyr@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: DrSatyr <8143518+DrSatyr@users.noreply.github.com>
1 parent 51b35ed commit f928125

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,28 @@ public void renderDoesNotFailWhenPropertyHasBeenRemoved() {
1919
render.render(diff, outputStreamWriter);
2020
assertThat(outputStream.toString()).isNotBlank();
2121
}
22+
23+
@Test
24+
public void issue865_renderDoesNotFailWhenSchemaIsNullButExampleChanged() {
25+
HtmlRender render = new HtmlRender();
26+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
27+
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
28+
ChangedOpenApi diff =
29+
OpenApiCompare.fromLocations(
30+
"issue-865-null-schema-1.yaml", "issue-865-null-schema-2.yaml");
31+
render.render(diff, outputStreamWriter);
32+
assertThat(outputStream.toString()).isNotBlank();
33+
}
34+
35+
@Test
36+
public void issue865_renderWithShowAllChangesDoesNotFailWhenSchemaIsNullButExampleChanged() {
37+
HtmlRender render = new HtmlRender(true);
38+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
39+
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
40+
ChangedOpenApi diff =
41+
OpenApiCompare.fromLocations(
42+
"issue-865-null-schema-1.yaml", "issue-865-null-schema-2.yaml");
43+
render.render(diff, outputStreamWriter);
44+
assertThat(outputStream.toString()).isNotBlank();
45+
}
2246
}
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)