Skip to content

Fix circular and deep external $ref resolution causing hangs#1903

Open
lahma wants to merge 1 commit intoRicoSuter:masterfrom
lahma:fix/lazy-ref-resolution
Open

Fix circular and deep external $ref resolution causing hangs#1903
lahma wants to merge 1 commit intoRicoSuter:masterfrom
lahma:fix/lazy-ref-resolution

Conversation

@lahma
Copy link
Copy Markdown
Collaborator

@lahma lahma commented Mar 8, 2026

Summary

  • In-progress document tracking: Added _inProgressDocuments HashSet to JsonReferenceResolver that detects when a document is already being resolved up the call stack, returning the pre-registered (deserialized but not yet fully resolved) schema instead of recursing infinitely
  • Path normalization: Unified document path normalization via NormalizeDocumentPath() — URLs are normalized with Uri.GetLeftPart(UriPartial.Path) and file paths with Path.GetFullPath() — preventing cache misses from path representation differences
  • 8 new tests: Circular file refs (2-way, 3-way), fan-out with back-references, transitive chains, external file refs with definition fragments, nested definition grouping, and circular fragment refs

Fixes #618, #616, #588, #566

Test plan

  • All 8 new LazyReferenceResolutionTests pass (including timeout-guarded circular ref tests)
  • All 32 existing reference-related tests pass — no regressions
  • Full test suite: 464 passed, 0 failed, 7 skipped (pre-existing)

🤖 Generated with Claude Code

Resolves issues where schemas with circular external file/URL $ref
chains (e.g. FHIR schemas, Azure ARM schemas) cause infinite recursion
or timeouts during UpdateSchemaReferencesAsync.

Changes to JsonReferenceResolver:
- Add _inProgressDocuments tracking to detect when a document is already
  being resolved up the call stack, breaking circular resolution chains
- Normalize document paths consistently via NormalizeDocumentPath() to
  prevent cache misses from path representation differences
- When a circular reference is detected, return the pre-registered
  (deserialized but not yet fully resolved) schema from AddDocumentReference

Fixes RicoSuter#618, RicoSuter#616, RicoSuter#588, RicoSuter#566

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@lahma lahma requested a review from RicoSuter March 8, 2026 14:41
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.

Implement lazy $ref resolution

1 participant