Skip to content

Fix stackoverflow in recursive definitions #331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.responses.ApiResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.openapidiff.core.model.*;
import org.openapitools.openapidiff.core.utils.RefPointer;
Expand All @@ -35,6 +37,7 @@ public class MarkdownRender implements Render {

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

public String render(ChangedOpenApi diff) {
this.diff = diff;
this.handledSchemas.clear();
return listEndpoints("What's New", diff.getNewEndpoints())
+ listEndpoints("What's Deleted", diff.getMissingEndpoints())
+ listEndpoints("What's Deprecated", diff.getDeprecatedEndpoints())
Expand Down Expand Up @@ -335,6 +339,8 @@ protected String schema(int deepness, ComposedSchema schema, DiffContext context
}

protected String schema(int deepness, Schema schema, DiffContext context) {
if (handledSchemas.contains(schema)) return "";
handledSchemas.add(schema);
StringBuilder sb = new StringBuilder();
sb.append(listItem(deepness, "Enum", schema.getEnum()));
sb.append(properties(deepness, "Property", schema.getProperties(), true, context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ public void renderDoesNotFailWhenPropertyHasBeenRemoved() {
OpenApiCompare.fromLocations("missing_property_1.yaml", "missing_property_2.yaml");
assertThat(render.render(diff)).isNotBlank();
}

@Test
public void renderDoesNotCauseStackOverflowWithRecursiveDefinitions() {
MarkdownRender render = new MarkdownRender();
ChangedOpenApi diff = OpenApiCompare.fromLocations("recursive_old.yaml", "recursive_new.yaml");
assertThat(render.render(diff)).isNotBlank();
}
}
29 changes: 29 additions & 0 deletions core/src/test/resources/recursive_new.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
openapi: 3.0.1
info:
title: recursive test
version: "1.0"
servers:
- url: "http://localhost:8000/"
paths:
/ping:
get:
operationId: ping
responses:
"200":
description: OK
content:
text/plain:
schema:
$ref: "#/components/schemas/A"
components:
schemas:
A:
type: object
properties:
propname2:
$ref: "#/components/schemas/B"
B:
type: object
properties:
propname2:
$ref: "#/components/schemas/A"
29 changes: 29 additions & 0 deletions core/src/test/resources/recursive_old.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
openapi: 3.0.1
info:
title: recursive test
version: "1.0"
servers:
- url: "http://localhost:8000/"
paths:
/ping:
get:
operationId: ping
responses:
"200":
description: OK
content:
text/plain:
schema:
$ref: "#/components/schemas/A"
components:
schemas:
A:
type: object
properties:
propname:
$ref: "#/components/schemas/B"
B:
type: object
properties:
propname:
$ref: "#/components/schemas/A"