Title: ValueByDiscriminator() generates incorrect method call when union type is referenced via external ref in allOf · Issue #2470 · oapi-codegen/oapi-codegen · GitHub
Open Graph Title: ValueByDiscriminator() generates incorrect method call when union type is referenced via external ref in allOf · Issue #2470 · oapi-codegen/oapi-codegen
X Title: ValueByDiscriminator() generates incorrect method call when union type is referenced via external ref in allOf · Issue #2470 · oapi-codegen/oapi-codegen
Description: Description When a schema is defined as allOf: [ExternalUnion, LocalObject] where ExternalUnion is a discriminated oneOf from another file, ValueByDiscriminator() is generated with incorrect syntax. Reproduction Spec structure (two files...
Open Graph Description: Description When a schema is defined as allOf: [ExternalUnion, LocalObject] where ExternalUnion is a discriminated oneOf from another file, ValueByDiscriminator() is generated with incorrect syntax...
X Description: Description When a schema is defined as allOf: [ExternalUnion, LocalObject] where ExternalUnion is a discriminated oneOf from another file, ValueByDiscriminator() is generated with incorrect syntax...
Opengraph URL: https://github.com/oapi-codegen/oapi-codegen/issues/2470
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ValueByDiscriminator() generates incorrect method call when union type is referenced via external ref in allOf","articleBody":"##### Description\n\nWhen a schema is defined as `allOf: [ExternalUnion, LocalObject]` where `ExternalUnion` is a discriminated `oneOf` from another file, `ValueByDiscriminator()` is generated with incorrect syntax.\n\n##### Reproduction\n\n**Spec structure (two files):**\n\n`diff.yaml`:\n\n```yaml\nDiffFile:\n oneOf:\n - $ref: '#/components/schemas/GitDiffFile'\n - $ref: '#/components/schemas/AddedImageDiffFile'\n discriminator:\n propertyName: diff_type\n```\n\n`pulls.yaml` (references diff.yaml as external):\n\n```yaml\nPRFile:\n allOf:\n - $ref: './diff.yaml#/components/schemas/DiffFile'\n - $ref: '#/components/schemas/ViewedStatus'\n```\n\nWith `import-mapping: {\"./diff.yaml\": \"mypkg/diff\"}` in oapi-codegen config.\n\n##### Generated code (broken)\n\n```go\nfunc (t PRFile) ValueByDiscriminator() (interface{}, error) {\n discriminator, err := t.Discriminator()\n switch discriminator {\n case \"GitDiffFile\":\n return t.AsexternalRef0.GitDiffFile() // ← compile error: undefined\n }\n}\n```\n\n##### Expected\n\n```go\ncase \"GitDiffFile\":\n return t.AsExternalRef0GitDiffFile() // matches the actually generated As* method\n```\n\n##### Root cause\n\nIn `union.tmpl`, `ValueByDiscriminator()` uses `$type` from `$discriminator.Mapping` directly:\n\n```\nreturn t.As{{$type}}()\n```\n\nFor external refs, `$type` = `\"externalRef0.GitDiffFile\"` which produces invalid Go (`t.AsexternalRef0.GitDiffFile()`).\n\nThe `As*` / `From*` / `Merge*` methods correctly use `{{ .Method }}` from the union element, which produces `ExternalRef0GitDiffFile`. `ValueByDiscriminator()` should do the same.\n\n##### Patch\n\n```\n--- a/pkg/codegen/templates/union.tmpl\n+++ b/pkg/codegen/templates/union.tmpl\n@@ -82,7 +82,9 @@ func (t {{.TypeName}}) ValueByDiscriminator() (interface{}, error) {\n switch discriminator{\n {{range $value, $type := $discriminator.Mapping -}}\n case \"{{$value}}\":\n- return t.As{{$type}}()\n+ {{- range $el := .Schema.UnionElements -}}\n+ {{- if eq (print $el) $type}}return t.As{{$el.Method}}(){{end -}}\n+ {{- end}}\n {{end -}}\n default:\n return nil, errors.New(\"unknown discriminator value: \"+discriminator)\n```\n\n##### Version\n\noapi-codegen v2.7.0","author":{"url":"https://github.com/alekgit","@type":"Person","name":"alekgit"},"datePublished":"2026-07-16T14:40:08Z","url":"https://github.com/oapi-codegen/oapi-codegen/issues/2470"}
| 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:e9d1b90f-6896-d3ea-69db-2979cab29da2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9040:1B304B:2A4080:3B2727:6AF9CCC6 |
| html-safe-nonce | 70adaaf749548eae53ca07cebe4c7713c560455bbb4f500b607ce445ad89ad8e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MDQwOjFCMzA0QjoyQTQwODA6M0IyNzI3OjZBRjlDQ0M2IiwidmlzaXRvcl9pZCI6Ijg4NDg5MzEwOTQ2NTc2MTcwOTQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 91f0917c9f67c1baca6e173cb545d79b3705611d37119fb4801444af9ae08e12 |
| hovercard-subject-tag | issue:4903142224 |
| 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/2470/issue_layout |
| twitter:image | https://opengraph.githubassets.com/56e683a65e860eb8691f259b5378c6c91dcc606e0e8c37c54e38db8d93629929/oapi-codegen/oapi-codegen/issues/2470 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/56e683a65e860eb8691f259b5378c6c91dcc606e0e8c37c54e38db8d93629929/oapi-codegen/oapi-codegen/issues/2470 |
| og:image:alt | Description When a schema is defined as allOf: [ExternalUnion, LocalObject] where ExternalUnion is a discriminated oneOf from another file, ValueByDiscriminator() is generated with incorrect syntax... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | alekgit |
| 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