Repository navigation
Description
What happened
Generation overflows the goroutine stack inside propagateRemoteRefs when a self-recursive schema in an external file is composed via allOf from more than one place.
Composing it once works. Composing it twice crashes. Nothing references back into the main document — the recursion is entirely inside the external file.
Reproduction
common.yaml — a self-recursive schema, no reference to the main document:
openapi: 3.0.3
info: {title: common, version: "1.0"}
paths: {}
components:
schemas:
Tree:
type: object
properties:
name: { type: string }
kids:
type: array
items: { $ref: '#/components/schemas/Tree' }main.yaml:
openapi: 3.0.3
info: {title: main, version: "1.0"}
paths: {}
components:
schemas:
A:
allOf:
- $ref: './common.yaml#/components/schemas/Tree'
- type: object
properties: { extra: { type: string } }
# Remove B and generation succeeds. Add it back and the stack overflows.
B:
allOf:
- $ref: './common.yaml#/components/schemas/Tree'
- type: object
properties: { other: { type: string } }config.yaml:
package: repro
generate:
models: true
output-options:
skip-prune: true
import-mapping:
./common.yaml: github.com/example/common$ oapi-codegen -config config.yaml main.yaml
runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflowThe trace is ~90 repeating codegen.propagateRemoteRefs frames entered once from codegen.valueWithPropagatedRef.
Root cause
valueWithPropagatedRef (pkg/codegen/merge_schemas.go) takes a shallow copy of the referenced schema:
schema := *ref.ValueProperties, Items and the *SchemaRef values inside are therefore the same objects as in the loaded document. propagateRemoteRefs then rewrites local refs in place, mutating the shared document permanently.
Its only termination rule is the # test:
if len(value.Ref) > 0 && value.Ref[0] == '#' {
value.Ref = remoteComponent + value.Ref // rewrite, do not descend
} else if value.Value != nil {
propagateRemoteRefs(remoteComponent, value.Value) // descend
}Stopping at a local $ref is what makes a self-recursive external schema safe — on the first pass. But the rewrite destroys the condition that stopped it: afterwards the ref reads ./common.yaml#/components/schemas/Tree, which no longer starts with #. The next walk over the same nodes falls through to else if value.Value != nil and descends into the resolved schema, i.e. straight around the cycle, forever.
So the rewrite is not idempotent, and because it mutates shared state the failure is order-dependent: whichever component the alphabetical iteration reaches first performs the rewrite, and the next one to touch the same external schema crashes. In a real multi-file setup — shared types in common.yaml, composed via allOf from several components — this will look intermittent and sensitive to unrelated schema renames.
Scope
Reproduced on main @ 2679cec6. Independent of #2542 / #2543: it is a different phase (rewriting $ref strings before any Go schema is generated) and a different code path, and it reproduces with none of that work in the tree.
Possible fixes
- Make the rewrite idempotent — also stop when the ref is already qualified with this
remoteComponent. One line, fixes this repro. - Guard the walk with a
seen map[*openapi3.Schema]bool. Covers shapes the prefix test might miss. - Stop mutating shared state — deep-copy the schema before rewriting. Addresses the order-dependence rather than just this symptom, at the cost of a larger change.
(1) or (2) is a contained fix; (3) is the principled one, since the current behaviour means generating component A can change what component B generates.
🤖 Investigated and drafted with Claude Code; the repro above was verified against main @ 2679cec6 (v2.8.0).
Activity
fix: stop following $refs when qualifying a flattened remote schema
fix: stop following $refs when qualifying a flattened remote schema (#…
feat(v3): allOf can only annotate external and x-go-type schemas (#2568)