Fix StackOverflowError on schema cycles that carry no $ref

AN
2mo ago
+213-1

Comparing a spec against itself overflows the stack when schemas form a reference cycle that no $ref survives.

resolveComposedSchema inlines an allOf target's properties into the wrapping schema and then clears the allOf. Two wrappers in a cycle therefore end up holding each other's properties with neither a $ref nor an allOf left on either one, and computeDeferredDiff only consults its recursion guard when both sides carry a bare $ref, so nothing stops the descent.

Guarding on the identity of the schema pair being compared covers that case, and also the simpler ones where a cycle passes through an allOf-wrapped reference on one or both sides. The guard is scoped to the current path, so sibling occurrences of the same schema are still diffed.

Changes:

  • track the (left, right) schema pair in RecursiveSchemaSet, entering before computeDiffForReal and leaving after it
  • cut the recursion when a pair is re-entered
  • add regression tests and fixtures for a wrapped cycle, a changed schema inside a wrapped cycle, and a mixed wrapped/bare cycle

./mvnw clean verify passes (core 273 tests, maven 13 tests, 0 failures). Also checked against a ~540KB generated spec that reproduced the original overflow: it now compares clean, and a type change made deep inside the cycle is still reported.

Claude assisted with this change.