Skip to content

Fix StackOverflowError on composed schema cycles without refs - #945

Open
Inf1mum wants to merge 2 commits into
OpenAPITools:masterfrom
Inf1mum:fix/788-deferred-recursion
Open

Inf1mum wants to merge 2 commits into
OpenAPITools:masterfrom
Inf1mum:fix/788-deferred-recursion

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Nov 15, 2026 •

Copy link
Copy Markdown

Fixes #788

Diagnosis

A composed schema cycle that survives without a $ref overflows the stack. SchemaDiff#resolveComposedSchema merges an allOf member's properties into the wrapping schema and then clears the allOf, so when the cycle is entered through an inline composed schema (for example a response schema allOf: [$ref Entry], with Entry and Child referencing each other through allOf-wrapped properties), the recursion is followed with no reference left to check. The existing reference-based guards never fire on such a path: DeferredSchemaCache#getOrAddSchema is only consulted when both sides carry a $ref, and nothing puts those refs into RecursiveSchemaSet, so the visited refs that resolveComposedSchema checks stay empty.

The descent then loops along exactly the cycle from the issue's stack trace:

ComposedSchemaDiffResult.diff
  -> RealizedChanged.flatMap
    -> SchemaDiff.computeDeferredDiff
      -> SchemaDiff.computeDiffForReal
        -> ComposedSchemaDiffResult.diff
          -> ...

I reproduced this end to end with two small specs: the CLI built from master throws java.lang.StackOverflowError on them, while the CLI built from this branch prints a change report and writes the HTML file.

Fix

computeDeferredDiff now guards the branch that falls through the cache on the identity of the (left, right) schema pair. The pair is recorded in RecursiveSchemaSet (the set that already carries the reference keys) before computeDiffForReal and removed after it returns; when a pair is already being compared higher up the current path, the descent stops with an empty result. The guard is path-scoped, so occurrences of the same pair in sibling positions are still diffed and the output for specs without such cycles does not change.

Tests

  • cyclic_composed_1.yaml / cyclic_composed_2.yaml: an Entry <-> Child cycle reached through an inline allOf response schema. Comparing the spec against itself reports no changes, and a property type change inside the cycle (child.name, string -> integer) is still reported as backward incompatible. Both comparisons threw StackOverflowError before the change.
  • cyclic_composed_deep_1.yaml / cyclic_composed_deep_2.yaml: an eight-level acyclic allOf chain, proving deep paths are still followed end to end and a change at the bottom is still detected.

./mvnw test passes for all modules (core 274 tests, 0 failures).

Related: #928 proposes a similar identity-based guard for cycles that carry no $ref.

Signed-off-by: Inf1mum vadimlazuk5@gmail.com


Summary by cubic

Fixes StackOverflowError on composed schema cycles that don't carry a $ref, resolving #788. When resolveComposedSchema merges an allOf member into the wrapping schema, a cycle is descended with no reference left to check, so the reference-based recursion guards never fire. computeDeferredDiff now stops when the same (left, right) schema pair is re-entered on the current path, tracked by identity in RecursiveSchemaSet. The guard is path-scoped, so sibling occurrences of the same pair are still diffed and output for specs without such cycles is unchanged.

Tests

  • Added a cyclic Entry/Child pair reached through an inline allOf response schema, an eight-level acyclic allOf chain, and a fixture whose two sibling properties revisit the same merged cyclic pair to confirm changes are still reported in both branches.

Written for commit 1ff8ed3. Summary will update on new commits.

View guided diff

Schema pairs compared without a $ref on both sides skip the deferred
cache guard in computeDeferredDiff. When resolveComposedSchema merges an
allOf member into the wrapping schema, a cycle between composed schemas
can lead back into the same pair with no reference left to check, and
the descent recursed until the stack overflowed, looping through
ComposedSchemaDiffResult.diff -> RealizedChanged.flatMap ->
SchemaDiff.computeDeferredDiff as in the report.

Track the identity of the schema pair being compared on the current path
in RecursiveSchemaSet and stop when a pair is re-entered. The guard is
path-scoped, so sibling occurrences of the same pair are still diffed
and output for specs without such cycles is unchanged.

Fixes OpenAPITools#788

Signed-off-by: Inf1mum <vadimlazuk5@gmail.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 7 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Add a fixture pair with two sibling properties whose allOf branches
revisit the same merged Entry/Child schema pair, and assert the
change inside the pair is reported for both branches, so a globally
retained pair cache would fail the suite.

Signed-off-by: Vadim Lazuk <vadimlazuk5@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exception in thread "main" java.lang.StackOverflowError

1 participant