René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:e9d1b90f-6896-d3ea-69db-2979cab29da2
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9040:1B304B:2A4080:3B2727:6AF9CCC6
html-safe-nonce70adaaf749548eae53ca07cebe4c7713c560455bbb4f500b607ce445ad89ad8e
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MDQwOjFCMzA0QjoyQTQwODA6M0IyNzI3OjZBRjlDQ0M2IiwidmlzaXRvcl9pZCI6Ijg4NDg5MzEwOTQ2NTc2MTcwOTQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac91f0917c9f67c1baca6e173cb545d79b3705611d37119fb4801444af9ae08e12
hovercard-subject-tagissue:4903142224
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/oapi-codegen/oapi-codegen/2470/issue_layout
twitter:imagehttps://opengraph.githubassets.com/56e683a65e860eb8691f259b5378c6c91dcc606e0e8c37c54e38db8d93629929/oapi-codegen/oapi-codegen/issues/2470
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/56e683a65e860eb8691f259b5378c6c91dcc606e0e8c37c54e38db8d93629929/oapi-codegen/oapi-codegen/issues/2470
og:image:altDescription 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamealekgit
hostnamegithub.com
expected-hostnamegithub.com
None612d3e6ffd693e7ea8d9fcf452370995872baef31d825b1fedef704cd3d24ac7
turbo-cache-controlno-preview
go-importgithub.com/oapi-codegen/oapi-codegen git https://github.com/oapi-codegen/oapi-codegen.git
octolytics-dimension-user_id142752710
octolytics-dimension-user_loginoapi-codegen
octolytics-dimension-repository_id173009358
octolytics-dimension-repository_nwooapi-codegen/oapi-codegen
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id173009358
octolytics-dimension-repository_network_root_nwooapi-codegen/oapi-codegen
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release072e34035c13c31543fba9f025cbbbda4210dae4
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/oapi-codegen/oapi-codegen/issues/2470#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Foapi-codegen%2Foapi-codegen%2Fissues%2F2470
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Foapi-codegen%2Foapi-codegen%2Fissues%2F2470
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=oapi-codegen%2Foapi-codegen
Reloadhttps://github.com/oapi-codegen/oapi-codegen/issues/2470
Reloadhttps://github.com/oapi-codegen/oapi-codegen/issues/2470
Reloadhttps://github.com/oapi-codegen/oapi-codegen/issues/2470
oapi-codegenhttps://github.com/oapi-codegen
oapi-codegenhttps://github.com/oapi-codegen/oapi-codegen
Codehttps://github.com/oapi-codegen/oapi-codegen
Issues228 (228)https://github.com/oapi-codegen/oapi-codegen/issues
Pull requests70 (70)https://github.com/oapi-codegen/oapi-codegen/pulls
Discussionshttps://github.com/oapi-codegen/oapi-codegen/discussions
Actionshttps://github.com/oapi-codegen/oapi-codegen/actions
Projectshttps://github.com/oapi-codegen/oapi-codegen/projects
Security and quality11 (11)https://github.com/oapi-codegen/oapi-codegen/security
Insightshttps://github.com/oapi-codegen/oapi-codegen/pulse
https://github.com/oapi-codegen/oapi-codegen
New issuehttps://github.com/login?return_to=https://github.com/oapi-codegen/oapi-codegen/issues/2470
New issuehttps://github.com/login?return_to=https://github.com/oapi-codegen/oapi-codegen/issues/2470
#2474https://github.com/oapi-codegen/oapi-codegen/pull/2474
ValueByDiscriminator() generates incorrect method call when union type is referenced via external ref in allOfhttps://github.com/oapi-codegen/oapi-codegen/issues/2470#top
#2474https://github.com/oapi-codegen/oapi-codegen/pull/2474
https://github.com/alekgit
alekgithttps://github.com/alekgit
on Jul 16, 2026https://github.com/oapi-codegen/oapi-codegen/issues/2470#issue-4903142224
mromaszewiczhttps://github.com/mromaszewicz
on Jul 16, 2026https://github.com/oapi-codegen/oapi-codegen/issues/2470#event-28083435878
Fix ValueByDiscriminator() with external refshttps://github.com/mromaszewicz/oapi-codegen/commit/12ce1a88c3b8e7faafd8af24b703799931a66805
https://docs.github.com/articles/closing-issues-via-commit-messages
12ce1a8https://github.com/mromaszewicz/oapi-codegen/commit/12ce1a88c3b8e7faafd8af24b703799931a66805
Fix ValueByDiscriminator() with external refshttps://github.com/mromaszewicz/oapi-codegen/commit/fdc6c6a6ebf3f65241d9809134044283ff6e12dc
https://docs.github.com/articles/closing-issues-via-commit-messages
fdc6c6ahttps://github.com/mromaszewicz/oapi-codegen/commit/fdc6c6a6ebf3f65241d9809134044283ff6e12dc
mromaszewiczhttps://github.com/mromaszewicz
completedhttps://github.com/oapi-codegen/oapi-codegen/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#2474https://github.com/oapi-codegen/oapi-codegen/pull/2474
on Jul 16, 2026https://github.com/oapi-codegen/oapi-codegen/issues/2470#event-28084323526
mromaszewiczhttps://github.com/mromaszewicz
on Jul 16, 2026https://github.com/oapi-codegen/oapi-codegen/issues/2470#event-28084359504
Fix ValueByDiscriminator() with external refs (oapi-codegen#2474)https://github.com/mromaszewicz/oapi-codegen/commit/145792280910f78f9f2c7208c507f80a34c5c572
oapi-codegen#2474https://github.com/oapi-codegen/oapi-codegen/pull/2474
https://docs.github.com/articles/closing-issues-via-commit-messages
1457922https://github.com/mromaszewicz/oapi-codegen/commit/145792280910f78f9f2c7208c507f80a34c5c572
tobiohttps://github.com/tobio
on Aug 26, 2026https://github.com/oapi-codegen/oapi-codegen/issues/2470#event-30069078525
Preserve recursive schema shape via back-ref substitutionhttps://github.com/tobio/oapi-codegen/commit/f9de2838eea2ff98b647a9cd25bcc450fdd6112a
f9de283https://github.com/tobio/oapi-codegen/commit/f9de2838eea2ff98b647a9cd25bcc450fdd6112a
mromaszewiczhttps://github.com/mromaszewicz
on Sep 17, 2026https://github.com/oapi-codegen/oapi-codegen/issues/2470#event-31295579436
Prevent stack overflow on recursive schemas (#2543)https://github.com/oapi-codegen/oapi-codegen/commit/a6d9d25c1094435542df240c607935cc313131dc
#2543https://github.com/oapi-codegen/oapi-codegen/pull/2543
a6d9d25https://github.com/oapi-codegen/oapi-codegen/commit/a6d9d25c1094435542df240c607935cc313131dc
mromaszewiczhttps://github.com/mromaszewicz
on Sep 24, 2026https://github.com/oapi-codegen/oapi-codegen/issues/2470#event-31777482487
feat(v3): allOf can only annotate external and x-go-type schemas (#2568)https://github.com/oapi-codegen/oapi-codegen/commit/a1f70777dbf2366ceaf943398a6708ac0ccbf42f
#2568https://github.com/oapi-codegen/oapi-codegen/pull/2568
a1f7077https://github.com/oapi-codegen/oapi-codegen/commit/a1f70777dbf2366ceaf943398a6708ac0ccbf42f
Sign up for freehttps://github.com/signup?return_to=https://github.com/oapi-codegen/oapi-codegen/issues/2470
Sign in to commenthttps://github.com/login?return_to=https://github.com/oapi-codegen/oapi-codegen/issues/2470
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.