Repository navigation
Stop following $refs when qualifying a flattened remote schema - #2558
Merged
Merged
Conversation
Flattening an external schema into an allOf rewrites its local "#/..." refs in place so they stay pointed at the document they came from. That rewrite happens on the shared document — valueWithPropagatedRef copies only the top schema node — so the same schema can be walked again by a later flatten. The walk stopped at a local $ref, rewriting it without descending, which is what kept a self-recursive remote schema from cycling. But the rewrite destroys the condition that stopped it: afterwards the ref no longer begins with "#", so the next walk fell through to the descend branch and followed it around the cycle until the stack overflowed. Composing such a schema once worked; composing it from a second place crashed (oapi-codegen#2557). Split the two cases the old condition ran together. A position holding a $ref is qualified if it is local and otherwise left alone, and is never descended into; only an inline schema is walked. A referenced schema becomes a Go type generated from its own document, so its body was never ours to rewrite — the old code re-qualified a foreign schema's local refs with the wrong document's path, which this also fixes. Not following refs is what makes the walk terminate, rather than a visit set would: a schema can only refer back to itself through a $ref, so a walk over inline schemas alone cannot cycle. The five positions doing this all reduce to one helper. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Contributor
|
mromaszewicz
force-pushed
the
fix/issue-2557
branch
from
September 17, 2026 05:32
2b700c7 to
9d1b9dd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2557.
The bug
Flattening an external schema into an
allOfrewrites its local#/...refs so they stay pointed at the document they came from (#2288).valueWithPropagatedRefcopies only the top schema node, so that rewrite lands on the shared document and the same schema can be walked again by a later flatten.The walk stopped at a local
$ref— rewriting it without descending — which is what kept a self-recursive remote schema from cycling. But the rewrite destroys the condition that stopped it:On the second walk the ref reads
./common.yaml#/..., the first branch no longer matches, and theelse iffollows it into the resolved schema — around the cycle, forever.So composing a self-recursive external schema once worked, and composing it from a second place overflowed the stack. Because the mutation is on shared state, which component crashes depends on the order components are generated in, so in a real multi-file setup this looks intermittent.
The fix
The old condition ran two different cases together:
Refempty (inline — correct to descend) andRefnon-empty but non-local (external — wrong to descend). Splitting them apart fixes three things:$ref, so a walk that never follows one cannot cycle. That is a structural guarantee, which is why I preferred it to a visited-set or a prefix check — both of those patch this repro without ruling out the next one.The five positions doing this all reduce to one helper, so the change is slightly shorter than what it replaces.
Tests
Three, all of which fail without the fix — the first two by overflowing the stack, the third with a plain assertion failure:
TestPropagateRemoteRefsIsIdempotent— walking the same self-recursive schema twice leaves the qualified ref alone and terminates.TestMergeSchemasRemoteRecursiveSchemaFlattenedTwice— the end-to-end shape from the issue: two components composing the same recursive schema from another file, both ending up with*[]externalRef0.Tree.TestPropagateRemoteRefsLeavesForeignRefsAlone— a ref into a third document keeps its path, and its body is not re-qualified.Compatibility
make generateacross every module produces no change to any committed fixture, so no existing output moves.make testandmake lintpass (pkg/codegengreen,internal/test77/77, 0 lint issues).🤖 Investigated and drafted with Claude Code.