Skip to content

Commit 72517cc

Browse files
mikkelaMikkel Arentoft
and
Mikkel Arentoft
authored
Fix stack overflow in recursive definitions (#331)
Co-authored-by: Mikkel Arentoft <[email protected]>
1 parent 37004ef commit 72517cc

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
import io.swagger.v3.oas.models.media.Schema;
1212
import io.swagger.v3.oas.models.parameters.Parameter;
1313
import io.swagger.v3.oas.models.responses.ApiResponse;
14+
import java.util.HashSet;
1415
import java.util.List;
1516
import java.util.Map;
17+
import java.util.Set;
1618
import org.apache.commons.lang3.StringUtils;
1719
import org.openapitools.openapidiff.core.model.*;
1820
import org.openapitools.openapidiff.core.utils.RefPointer;
@@ -35,6 +37,7 @@ public class MarkdownRender implements Render {
3537

3638
protected RefPointer<Schema<?>> refPointer = new RefPointer<>(RefType.SCHEMAS);
3739
protected ChangedOpenApi diff;
40+
protected Set<Schema<?>> handledSchemas = new HashSet<>();
3841
/**
3942
* A parameter which indicates whether or not metadata (summary and metadata) changes should be
4043
* logged in the changelog file.
@@ -43,6 +46,7 @@ public class MarkdownRender implements Render {
4346

4447
public String render(ChangedOpenApi diff) {
4548
this.diff = diff;
49+
this.handledSchemas.clear();
4650
return listEndpoints("What's New", diff.getNewEndpoints())
4751
+ listEndpoints("What's Deleted", diff.getMissingEndpoints())
4852
+ listEndpoints("What's Deprecated", diff.getDeprecatedEndpoints())
@@ -335,6 +339,8 @@ protected String schema(int deepness, ComposedSchema schema, DiffContext context
335339
}
336340

337341
protected String schema(int deepness, Schema schema, DiffContext context) {
342+
if (handledSchemas.contains(schema)) return "";
343+
handledSchemas.add(schema);
338344
StringBuilder sb = new StringBuilder();
339345
sb.append(listItem(deepness, "Enum", schema.getEnum()));
340346
sb.append(properties(deepness, "Property", schema.getProperties(), true, context));

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

+7
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@ 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 renderDoesNotCauseStackOverflowWithRecursiveDefinitions() {
20+
MarkdownRender render = new MarkdownRender();
21+
ChangedOpenApi diff = OpenApiCompare.fromLocations("recursive_old.yaml", "recursive_new.yaml");
22+
assertThat(render.render(diff)).isNotBlank();
23+
}
1724
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
openapi: 3.0.1
2+
info:
3+
title: recursive test
4+
version: "1.0"
5+
servers:
6+
- url: "http://localhost:8000/"
7+
paths:
8+
/ping:
9+
get:
10+
operationId: ping
11+
responses:
12+
"200":
13+
description: OK
14+
content:
15+
text/plain:
16+
schema:
17+
$ref: "#/components/schemas/A"
18+
components:
19+
schemas:
20+
A:
21+
type: object
22+
properties:
23+
propname2:
24+
$ref: "#/components/schemas/B"
25+
B:
26+
type: object
27+
properties:
28+
propname2:
29+
$ref: "#/components/schemas/A"
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
openapi: 3.0.1
2+
info:
3+
title: recursive test
4+
version: "1.0"
5+
servers:
6+
- url: "http://localhost:8000/"
7+
paths:
8+
/ping:
9+
get:
10+
operationId: ping
11+
responses:
12+
"200":
13+
description: OK
14+
content:
15+
text/plain:
16+
schema:
17+
$ref: "#/components/schemas/A"
18+
components:
19+
schemas:
20+
A:
21+
type: object
22+
properties:
23+
propname:
24+
$ref: "#/components/schemas/B"
25+
B:
26+
type: object
27+
properties:
28+
propname:
29+
$ref: "#/components/schemas/A"

0 commit comments

Comments
 (0)