Skip to content

Stop following $refs when qualifying a flattened remote schema - #2558

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2557
Sep 17, 2026
Merged

mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2557

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Fixes #2557.

The bug

Flattening an external schema into an allOf rewrites its local #/... refs so they stay pointed at the document they came from (#2288). valueWithPropagatedRef copies 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:

if len(value.Ref) > 0 && value.Ref[0] == '#' {
    value.Ref = remoteComponent + value.Ref   // now starts with '.', not '#'
} else if value.Value != nil {
    propagateRemoteRefs(remoteComponent, value.Value)
}

On the second walk the ref reads ./common.yaml#/..., the first branch no longer matches, and the else if follows 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: Ref empty (inline — correct to descend) and Ref non-empty but non-local (external — wrong to descend). Splitting them apart fixes three things:

  • The crash. A rewritten ref is non-empty, so a later walk stops instead of following it.
  • Termination in general. A schema can only refer back to itself through a $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.
  • A latent correctness bug. The old code descended into a foreign schema's body and re-qualified its local refs with the wrong document's path. A referenced schema becomes a Go type generated from its own document, so its body was never ours to rewrite.

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 generate across every module produces no change to any committed fixture, so no existing output moves.

make test and make lint pass (pkg/codegen green, internal/test 77/77, 0 lint issues).


🤖 Investigated and drafted with Claude Code.

@mromaszewicz
mromaszewicz requested a review from a team as a code owner September 17, 2026 05:28
@mromaszewicz mromaszewicz added the bug Something isn't working label Sep 17, 2026
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]>
@greptile-apps

greptile-apps Bot commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; the traversal change matches reference ownership semantics and is covered by focused regressions.

Summary

This PR prevents remote-schema qualification from following non-empty $ref values, eliminating repeated-walk recursion and avoiding mutation of foreign referenced schema bodies.

  • Extracts the repeated qualification logic into qualifyRemoteRef.
  • Continues recursively walking inline schemas while treating local and external references as traversal boundaries.
  • Adds regression coverage for repeated recursive flattening, foreign references, and generated external recursive types.

Reviews (1) · Last reviewed commit: "fix: stop following $refs when qualifyin..."

@mromaszewicz
mromaszewicz merged commit b45ce62 into oapi-codegen:main Sep 17, 2026
15 checks passed
@mromaszewicz
mromaszewicz deleted the fix/issue-2557 branch September 17, 2026 05:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stack overflow in propagateRemoteRefs when a self-recursive external schema is composed via allOf more than once

1 participant