Skip to content
Open
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 @@ -13,7 +13,8 @@
import io.swagger.v3.oas.models.responses.ApiResponse;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -42,7 +43,7 @@ public class MarkdownRender implements Render {

protected RefPointer<Schema<?>> refPointer = new RefPointer<>(RefType.SCHEMAS);
protected ChangedOpenApi diff;
protected Set<Schema<?>> handledSchemas = new HashSet<>();
protected Set<Schema<?>> handledSchemas = Collections.newSetFromMap(new IdentityHashMap<>());
/**
* A parameter which indicates whether or not metadata (summary and metadata) changes should be
* logged in the changelog file.
Expand Down Expand Up @@ -381,17 +382,20 @@ 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));
if (schema instanceof ComposedSchema) {
sb.append(schema(deepness, (ComposedSchema) schema, context));
} else if (schema instanceof ArraySchema) {
sb.append(items(deepness, resolve(((ArraySchema) schema).getItems()), context));
if (!handledSchemas.add(schema)) return "";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The path-scoped guard stops true cycles, but it no longer memoizes shared subtrees. A component reachable through many paths is re-rendered in full each time, so output and runtime can grow exponentially with the depth of shared component nesting on large specs. Consider bounding the expansion (for example a depth or total-output limit) or memoizing rendered fragments per schema when the content does not depend on the path.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At core/src/main/java/org/openapitools/openapidiff/core/output/MarkdownRender.java, line 385:

<comment>The path-scoped guard stops true cycles, but it no longer memoizes shared subtrees. A component reachable through many paths is re-rendered in full each time, so output and runtime can grow exponentially with the depth of shared component nesting on large specs. Consider bounding the expansion (for example a depth or total-output limit) or memoizing rendered fragments per schema when the content does not depend on the path.</comment>

<file context>
@@ -382,7 +382,7 @@ protected String schema(int deepness, ComposedSchema schema, DiffContext context
 
   protected String schema(int deepness, Schema schema, DiffContext context) {
-    if (!visitingSchemas.add(schema)) return "";
+    if (!handledSchemas.add(schema)) return "";
     try {
       StringBuilder sb = new StringBuilder();
</file context>

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The factual core is right: before this PR, handledSchemas (added in 72517cc for #331, the recursive-definitions stack-overflow fix) was a global, value-based HashSet with no removal, so already-visited subtrees were skipped everywhere. But that memo is exactly what #523 reported: a component reached from a second path, or any value-identical schema, was silently dropped from the report.

Measured on this branch: temporarily restoring the old global-memo semantics makes both regression tests added for #523 fail — each repeated enum/property value renders once (expected: 2 but was: 1), because the second reference site returns an empty string. So memoizing again would re-break #523.

True cycles still terminate under the current code (identity-based, path-scoped, removed on exit), so there is no unbounded recursion on recursive specs; the growth case needs a shared component DAG nested many levels deep. Keeping #523's completeness while bounding that expansion requires a visible output change — a back-link for shared subtrees or a depth/output limit — which is a rendering-semantics decision I would not fold into a bug-fix PR. Happy to follow up with either if the maintainer prefers bounded output.

try {
StringBuilder sb = new StringBuilder();
sb.append(listItem(deepness, "Enum", schema.getEnum()));
sb.append(properties(deepness, "Property", schema.getProperties(), true, context));
if (schema instanceof ComposedSchema) {
sb.append(schema(deepness, (ComposedSchema) schema, context));
} else if (schema instanceof ArraySchema) {
sb.append(items(deepness, resolve(((ArraySchema) schema).getItems()), context));
}
return sb.toString();
} finally {
handledSchemas.remove(schema);
}
return sb.toString();
}

protected String items(int deepness, ChangedSchema schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,51 @@ public void issue870_renderDoesNotFailWhenSchemaIsNullButExampleChanged() {
render.render(diff, outputStreamWriter);
assertThat(outputStream.toString()).isNotBlank();
}

@Test
public void issue523_enumValuesAreReportedForRepeatedIdenticalSchemas() {
MarkdownRender render = new MarkdownRender();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
ChangedOpenApi diff =
OpenApiCompare.fromLocations("issue-523-enum-1.yaml", "issue-523-enum-2.yaml");
render.render(diff, outputStreamWriter);
String markdown = outputStream.toString();
assertThat(countOccurrences(markdown, "`none`")).isEqualTo(2);
assertThat(countOccurrences(markdown, "`enabled`")).isEqualTo(2);
assertThat(countOccurrences(markdown, "`push`")).isEqualTo(2);
assertThat(countOccurrences(markdown, "`disabled`")).isEqualTo(2);
}

@Test
public void issue523_propertiesAreReportedForRepeatedIdenticalSchemas() {
MarkdownRender render = new MarkdownRender();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
ChangedOpenApi diff =
OpenApiCompare.fromLocations("issue-523-props-1.yaml", "issue-523-props-2.yaml");
render.render(diff, outputStreamWriter);
String markdown = outputStream.toString();
assertThat(countOccurrences(markdown, "`partiallyPopulated`")).isEqualTo(2);
assertThat(countOccurrences(markdown, "`additionalAttributes`")).isEqualTo(2);
assertThat(countOccurrences(markdown, "`type`")).isEqualTo(2);
}

@Test
public void handledSchemasFieldRemainsAvailableToSubclasses() {
MarkdownRender render = new MarkdownRender();
assertThat(render.handledSchemas).isEmpty();
render.handledSchemas.clear();
assertThat(render.handledSchemas).isEmpty();
}

private static int countOccurrences(String haystack, String needle) {
int count = 0;
int index = 0;
while ((index = haystack.indexOf(needle, index)) != -1) {
count++;
index += needle.length();
}
return count;
}
}
70 changes: 70 additions & 0 deletions core/src/test/resources/issue-523-enum-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
openapi: 3.0.1
info:
title: issue 523 enum
version: "1.0"
paths:
/check/api/Id/Exp/{ExpId}:
get:
responses:
default:
content:
application/json:
schema:
$ref: "#/components/schemas/ExpRO"
components:
schemas:
ExpRO:
type: object
properties:
data:
$ref: "#/components/schemas/ExpDataRO"
ExpDataRO:
type: object
properties:
id:
type: string
type:
type: string
enum:
- tpeExpectations
attributes:
$ref: "#/components/schemas/ExpAttributesRO"
ExpAttributesRO:
type: object
properties:
Terminations:
type: array
items:
$ref: "#/components/schemas/TerminationRO"
TerminationRO:
type: object
properties:
nniNetwork:
$ref: "#/components/schemas/NetworkRO"
ponConnection:
$ref: "#/components/schemas/PonConnectionRO"
NetworkRO:
type: object
properties:
tagAction:
type: string
enum:
- none
- pop
- push
- stamp
floodingLink:
type: string
enum:
- enabled
- disabled
PonConnectionRO:
type: object
properties:
tagAction:
type: string
enum:
- none
- pop
- push
- stamp
80 changes: 80 additions & 0 deletions core/src/test/resources/issue-523-enum-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
openapi: 3.0.1
info:
title: issue 523 enum
version: "1.0"
paths:
/check/api/Id/Exp/{ExpId}:
get:
responses:
default:
content:
application/json:
schema:
$ref: "#/components/schemas/ExpRO"
components:
schemas:
ExpRO:
type: object
properties:
data:
$ref: "#/components/schemas/ExpDataRO"
ExpDataRO:
type: object
properties:
id:
type: string
type:
type: string
enum:
- tpeExpectations
attributes:
$ref: "#/components/schemas/ExpAttributesRO"
ExpAttributesRO:
type: object
properties:
Terminations:
type: array
items:
$ref: "#/components/schemas/TerminationRO"
TerminationRO:
type: object
properties:
NetworkConnectionPackage:
$ref: "#/components/schemas/NetworkConnectionRO"
ponConnectionPackage:
$ref: "#/components/schemas/PonConnectionRO"
datalinkPackage:
$ref: "#/components/schemas/DatalinkRO"
NetworkConnectionRO:
type: object
properties:
tagAction:
type: string
enum:
- none
- pop
- push
- stamp
floodingLink:
type: string
enum:
- enabled
- disabled
PonConnectionRO:
type: object
properties:
tagAction:
type: string
enum:
- none
- pop
- push
- stamp
DatalinkRO:
type: object
properties:
adminState:
type: string
enum:
- enabled
- disabled
28 changes: 28 additions & 0 deletions core/src/test/resources/issue-523-props-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
openapi: 3.0.1
info:
title: issue 523 properties
version: "1.0"
paths:
/check/API:
get:
responses:
default:
content:
application/json:
schema:
$ref: "#/components/schemas/ExpAPIRO"
components:
schemas:
ExpAPIRO:
type: object
properties:
data:
$ref: "#/components/schemas/ExpAPIDataRO"
ExpAPIDataRO:
type: object
properties:
relationships:
$ref: "#/components/schemas/ExpAPIRelationshipsRO"
ExpAPIRelationshipsRO:
type: object
properties: {}
69 changes: 69 additions & 0 deletions core/src/test/resources/issue-523-props-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
openapi: 3.0.1
info:
title: issue 523 properties
version: "1.0"
paths:
/check/API:
get:
responses:
default:
content:
application/json:
schema:
$ref: "#/components/schemas/ExpAPIRO"
components:
schemas:
ExpAPIRO:
type: object
properties:
data:
$ref: "#/components/schemas/ExpAPIDataRO"
ExpAPIDataRO:
type: object
properties:
relationships:
$ref: "#/components/schemas/ExpAPIRelationshipsRO"
ExpAPIRelationshipsRO:
type: object
properties:
linkedTpes:
$ref: "#/components/schemas/OneToManyRelationshipRO"
shelfNetworkConstruct:
$ref: "#/components/schemas/OneToOneRelationshipRO"
OneToManyRelationshipRO:
type: object
properties:
data:
type: array
items:
$ref: "#/components/schemas/RelationshipDataRO"
meta:
$ref: "#/components/schemas/RelationshipMetaDataRO"
RelationshipDataRO:
type: object
properties:
type:
type: string
id:
type: string
attributes:
$ref: "#/components/schemas/RelationshipAttributesRO"
RelationshipAttributesRO:
type: object
properties:
additionalAttributes:
$ref: "#/components/schemas/AdditionalAttributesRO"
AdditionalAttributesRO:
type: object
RelationshipMetaDataRO:
type: object
properties:
partiallyPopulated:
type: boolean
OneToOneRelationshipRO:
type: object
properties:
data:
$ref: "#/components/schemas/RelationshipDataRO"
meta:
$ref: "#/components/schemas/RelationshipMetaDataRO"