Title: Stack overflow in propagateRemoteRefs when a self-recursive external schema is composed via allOf more than once · Issue #2557 · oapi-codegen/oapi-codegen · GitHub
Open Graph Title: Stack overflow in propagateRemoteRefs when a self-recursive external schema is composed via allOf more than once · Issue #2557 · oapi-codegen/oapi-codegen
X Title: Stack overflow in propagateRemoteRefs when a self-recursive external schema is composed via allOf more than once · Issue #2557 · oapi-codegen/oapi-codegen
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. Nothi...
Open Graph 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 ...
X 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 ...
Opengraph URL: https://github.com/oapi-codegen/oapi-codegen/issues/2557
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Stack overflow in propagateRemoteRefs when a self-recursive external schema is composed via allOf more than once","articleBody":"### What happened\n\nGeneration overflows the goroutine stack inside `propagateRemoteRefs` when a **self-recursive schema in an external file** is composed via `allOf` from **more than one place**.\n\nComposing it once works. Composing it twice crashes. Nothing references back into the main document — the recursion is entirely inside the external file.\n\n### Reproduction\n\n`common.yaml` — a self-recursive schema, no reference to the main document:\n\n```yaml\nopenapi: 3.0.3\ninfo: {title: common, version: \"1.0\"}\npaths: {}\ncomponents:\n schemas:\n Tree:\n type: object\n properties:\n name: { type: string }\n kids:\n type: array\n items: { $ref: '#/components/schemas/Tree' }\n```\n\n`main.yaml`:\n\n```yaml\nopenapi: 3.0.3\ninfo: {title: main, version: \"1.0\"}\npaths: {}\ncomponents:\n schemas:\n A:\n allOf:\n - $ref: './common.yaml#/components/schemas/Tree'\n - type: object\n properties: { extra: { type: string } }\n # Remove B and generation succeeds. Add it back and the stack overflows.\n B:\n allOf:\n - $ref: './common.yaml#/components/schemas/Tree'\n - type: object\n properties: { other: { type: string } }\n```\n\n`config.yaml`:\n\n```yaml\npackage: repro\ngenerate:\n models: true\noutput-options:\n skip-prune: true\nimport-mapping:\n ./common.yaml: github.com/example/common\n```\n\n```console\n$ oapi-codegen -config config.yaml main.yaml\nruntime: goroutine stack exceeds 1000000000-byte limit\nfatal error: stack overflow\n```\n\nThe trace is ~90 repeating `codegen.propagateRemoteRefs` frames entered once from `codegen.valueWithPropagatedRef`.\n\n### Root cause\n\n`valueWithPropagatedRef` (`pkg/codegen/merge_schemas.go`) takes a **shallow** copy of the referenced schema:\n\n```go\nschema := *ref.Value\n```\n\n`Properties`, `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.\n\nIts only termination rule is the `#` test:\n\n```go\nif len(value.Ref) > 0 && value.Ref[0] == '#' {\n value.Ref = remoteComponent + value.Ref // rewrite, do not descend\n} else if value.Value != nil {\n propagateRemoteRefs(remoteComponent, value.Value) // descend\n}\n```\n\nStopping 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.\n\nSo 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.\n\n### Scope\n\nReproduced 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.\n\n### Possible fixes\n\n1. **Make the rewrite idempotent** — also stop when the ref is already qualified with this `remoteComponent`. One line, fixes this repro.\n2. **Guard the walk** with a `seen map[*openapi3.Schema]bool`. Covers shapes the prefix test might miss.\n3. **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.\n\n(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.\n\n---\n\n\u003csub>🤖 Investigated and drafted with [Claude Code](https://claude.com/claude-code); the repro above was verified against `main` @ `2679cec6` (v2.8.0).\u003c/sub>\n","author":{"url":"https://github.com/mromaszewicz","@type":"Person","name":"mromaszewicz"},"datePublished":"2026-09-17T05:21:04Z","url":"https://github.com/oapi-codegen/oapi-codegen/issues/2557"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:5b0970d3-ff2a-1508-012c-596cd104b7bd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A934:352312:2A2155:3B62C7:6AF9CD00 |
| html-safe-nonce | cd083d48a956fa1f1727508757e7c92309943a9fde76a7762d702d65f478b8fe |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOTM0OjM1MjMxMjoyQTIxNTU6M0I2MkM3OjZBRjlDRDAwIiwidmlzaXRvcl9pZCI6IjUxNDI1MDIxNzkzODYwMTkwNzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 774fcbc2614b3ca68db70002ff7bb085710cd328bdf73c5fa113b805fdc1477e |
| hovercard-subject-tag | issue:5483279805 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/oapi-codegen/oapi-codegen/2557/issue_layout |
| twitter:image | https://opengraph.githubassets.com/477b97f135e86896fb79d018771f092276146cf2809c0cbb07e33ff33088f2a9/oapi-codegen/oapi-codegen/issues/2557 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/477b97f135e86896fb79d018771f092276146cf2809c0cbb07e33ff33088f2a9/oapi-codegen/oapi-codegen/issues/2557 |
| og:image:alt | 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 ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mromaszewicz |
| hostname | github.com |
| expected-hostname | github.com |
| None | 612d3e6ffd693e7ea8d9fcf452370995872baef31d825b1fedef704cd3d24ac7 |
| turbo-cache-control | no-preview |
| go-import | github.com/oapi-codegen/oapi-codegen git https://github.com/oapi-codegen/oapi-codegen.git |
| octolytics-dimension-user_id | 142752710 |
| octolytics-dimension-user_login | oapi-codegen |
| octolytics-dimension-repository_id | 173009358 |
| octolytics-dimension-repository_nwo | oapi-codegen/oapi-codegen |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 173009358 |
| octolytics-dimension-repository_network_root_nwo | oapi-codegen/oapi-codegen |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 072e34035c13c31543fba9f025cbbbda4210dae4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width