Fix StackOverflowError on composed schema cycles without refs

IN
9h ago
+350-1

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, the cycle is descended with no reference left to check, so the existing reference-based recursion guards never fire. computeDeferredDiff now tracks the identity of the (left, right) schema pair on the current path via RecursiveSchemaSet and stops 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.

Tests

  • Added CyclicComposedSchemaTest covering an Entry/Child cycle reached through an inline allOf response schema and an eight-level acyclic allOf chain.