Title: A oneOf/anyOf branch that is a whole-file `$ref` generates Go that doesn't parse · Issue #2639 · oapi-codegen/oapi-codegen · GitHub
Open Graph Title: A oneOf/anyOf branch that is a whole-file `$ref` generates Go that doesn't parse · Issue #2639 · oapi-codegen/oapi-codegen
X Title: A oneOf/anyOf branch that is a whole-file `$ref` generates Go that doesn't parse · Issue #2639 · oapi-codegen/oapi-codegen
Description: When a oneOf or anyOf branch is a $ref to a whole file ($ref: ./user.yaml, no # fragment), the union's As*/From*/Merge* accessors are named after the branch's inline Go type, so the generated code has methods like AsStruct {\n Name *stri...
Open Graph Description: When a oneOf or anyOf branch is a $ref to a whole file ($ref: ./user.yaml, no # fragment), the union's As*/From*/Merge* accessors are named after the branch's inline Go type, so the generated code ...
X Description: When a oneOf or anyOf branch is a $ref to a whole file ($ref: ./user.yaml, no # fragment), the union's As*/From*/Merge* accessors are named after the branch's inline Go type, so the generat...
Opengraph URL: https://github.com/oapi-codegen/oapi-codegen/issues/2639
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"A oneOf/anyOf branch that is a whole-file `$ref` generates Go that doesn't parse","articleBody":"When a `oneOf` or `anyOf` branch is a `$ref` to a whole file (`$ref: ./user.yaml`, no `#` fragment), the union's `As*`/`From*`/`Merge*` accessors are named after the branch's inline Go type, so the generated code has methods like `AsStruct {\\n Name *string ... }()` and formatting fails.\n\nWhole-file refs are otherwise generated inline (`IsGoTypeReference` returns false for them, #308): the same file used directly as a body schema or as a property works, and an inline branch (`- {type: object, properties: ...}`) in the same union gets its own type, `GetItems200JSONResponseBody0`.\n\n## Reproduction\n\n`openapi.yaml`:\n\n```yaml\nopenapi: 3.0.3\ninfo: {title: t, version: \"1\"}\npaths:\n /items:\n get:\n responses:\n \"200\":\n description: ok\n content:\n application/json:\n schema:\n oneOf:\n - $ref: ./user.yaml\n - $ref: ./group.yaml\n```\n\n`user.yaml`:\n\n```yaml\ntype: object\nproperties:\n name: {type: string}\n```\n\n`group.yaml`:\n\n```yaml\ntype: object\nproperties:\n id: {type: integer}\n```\n\n```console\n$ oapi-codegen -generate types -package api -o api.gen.go openapi.yaml\nerror generating code: error formatting Go code at line 53: api.go:53:5: expected declaration, found Name (and 2 more errors)\n```\n\nNo configuration file. The unformatted output (with `output-options: skip-fmt: true`) shows where it breaks:\n\n```go\n // AsStruct {\n Name *string`json:\"name,omitempty\"`\n} returns the union data inside the GetItems200JSONResponseBody as a struct {\n Name *string`json:\"name,omitempty\"`\n}\n func (t GetItems200JSONResponseBody) AsStruct {\n Name *string`json:\"name,omitempty\"`\n}() (struct {\n```\n\nThe same happens with `anyOf`, with only one branch being a whole-file ref, with `openapi: 3.1.0`, for a property's `oneOf`, and for a component schema (`components.schemas.Item: {oneOf: [$ref: ./user.yaml, $ref: ./group.yaml]}`, referenced from the response). The component form fails the same way on v2.4.1 through v2.6.0; the inline body form has failed since v2.7.0, when inline body unions started getting accessors.\n\n## Expected\n\nThe branches are generated like inline ones: `GetItems200JSONResponseBody0` and `GetItems200JSONResponseBody1`, with `AsGetItems200JSONResponseBody0()` and so on, and the code compiles. Or, if whole-file refs aren't meant to be supported as union branches, an error that says so.\n\n## Versions\n\nv2.8.1 and `main` (15f5714, `v2.8.1-0.20261028140811-15f571472ff8`, error at line 56 there), Go 1.27.3, linux/amd64.\n\n## Likely cause\n\n`generateUnionElement` (`pkg/codegen/union.go:88` on main) names and defines a branch only when `element.Ref == \"\"`. A whole-file ref has a non-empty `Ref`, but `generateGoSchema` inlined it, so its `GoType` is the struct literal. Checking `!IsGoTypeReference(element.Ref)` there instead makes both forms above generate the `...0`/`...1` types, and the output compiles; I haven't run the test suite on that change.\n","author":{"url":"https://github.com/BogdanYarotsky","@type":"Person","name":"BogdanYarotsky"},"datePublished":"2026-10-29T06:28:20Z","url":"https://github.com/oapi-codegen/oapi-codegen/issues/2639"}
| 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:266887df-32e1-4901-848e-f5b14ffb502c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AEA4:18034D:2B0115A:38706B3:6AF9BAD9 |
| html-safe-nonce | cecec4f7242ef35fefa8350331d72ecde3ca834f5caecbf86688d0e3455091ae |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRUE0OjE4MDM0RDoyQjAxMTVBOjM4NzA2QjM6NkFGOUJBRDkiLCJ2aXNpdG9yX2lkIjoiMzYxNTQ5MDYwMjgxODQyMzkzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 8be570d4cc35c1ed3bcd758e16967d3c0834bdea0345b7206c28c68d72961cfe |
| hovercard-subject-tag | issue:6094266634 |
| 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/2639/issue_layout |
| twitter:image | https://opengraph.githubassets.com/84fbdfe8f7dedfa0e8e4b1467995fb59b74f828f79d62460349e2cfe6538236f/oapi-codegen/oapi-codegen/issues/2639 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/84fbdfe8f7dedfa0e8e4b1467995fb59b74f828f79d62460349e2cfe6538236f/oapi-codegen/oapi-codegen/issues/2639 |
| og:image:alt | When a oneOf or anyOf branch is a $ref to a whole file ($ref: ./user.yaml, no # fragment), the union's As*/From*/Merge* accessors are named after the branch's inline Go type, so the generated code ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | BogdanYarotsky |
| 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 | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width