Skip to content

fix: recursion detection to allow same resolved $ref in different branches#1241

Open
catosaurusrex2003 wants to merge 5 commits intoasyncapi:masterfrom
catosaurusrex2003:fixes-in-recursion-detection-logic
Open

fix: recursion detection to allow same resolved $ref in different branches#1241
catosaurusrex2003 wants to merge 5 commits intoasyncapi:masterfrom
catosaurusrex2003:fixes-in-recursion-detection-logic

Conversation

@catosaurusrex2003
Copy link
Copy Markdown
Contributor

The Problem

The recursion detection algorithm incorrectly throws "too much recursion" errors when the same external $ref appears in multiple branches (e.g. in the schema provided in #1236 c1 and c2 both referencing the same external component).

This happens because the algorithm used a global WeakSet that flagged any object seen before, even if it was in a different branch.

Solution

Replaced global WeakSet tracking with path-based tracking using Set<object>. Each branch now gets its own copy of the path, allowing the same object to appear in different branches while still detecting true circular references.

Key Changes

  • Changed WeakSetSet<object> to enable path copying
  • Renamed visitedvisitedInPath for clarity
  • Create new path copy for each branch instead of sharing global set

Related issue(s)

#1236

@catosaurusrex2003 catosaurusrex2003 changed the title fix: Recursion detection to allow same resolved $ref in different branches fix: recursion detection to allow same resolved $ref in different branches Feb 19, 2026
@sonarqubecloud
Copy link
Copy Markdown

@MichakrawSB
Copy link
Copy Markdown

@magicmatatjahu @fmvilas @AceTheCreator not a big PR, any chance to review and merge it?

@MichakrawSB
Copy link
Copy Markdown

@magicmatatjahu @fmvilas @AceTheCreator any updates?

@YOU54F
Copy link
Copy Markdown

YOU54F commented Apr 16, 2026

Would also like to see this merged, @catosaurusrex2003 are you able to rebase as there are some conflicts with the base branch.

Are the changes in the playground necessary (tsconfig / package.json) for this change, or were they just for testing purposes?

I would probably advise squashing into a single commit and use conventional commit notation in your message fix: false positive recursion error when same external appears in multiple branches to match the existing convention in the repo

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Member

@AceTheCreator AceTheCreator left a comment

Choose a reason for hiding this comment

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

Hey @catosaurusrex2003, just a small perf improvement.

I like the idea of creating a new path copy for each branch instead of sharing a global set. What if, during the recursion, we clean up after? currently the clone copies the entire Set at every level. For every tree N levels deep, that's O(N²) work just for tracking.

@catosaurusrex2003
Copy link
Copy Markdown
Contributor Author

Nice suggestion
I will look into this and update the pr asap

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.

4 participants