fix: honour external package's Go name for direct response refs#2423
Merged
Conversation
Closes: oapi-codegen#2422 When a base spec references a response defined in an external file directly (`$ref: 'spec-ext.yaml#/components/responses/Outcome'`, not via a path item) and that response is renamed in the external package via `x-go-name`, the generated `client-with-responses` wrapper pointed at the raw component name. The imported package declares the renamed model (e.g. `OutcomeResult`), so the base client referenced an undefined `externalRef0.Outcome` and failed to compile. GetResponseTypeDefinitions resolved external response names with RefPathToGoType / resolvedNameForRefPath, which key off the root spec and the raw component name; neither consults the external component's `x-go-name`. The oapi-codegen#2308 fix already resolved this for externally-ref'd *path items* by reading the name from the resolved response value's context. This unifies that branch to also cover direct external refs: the imported package is detected via the path item's ref (relative response) or the response ref itself (direct), and the model name honours `x-go-name` the same way the external package generated it. Behaviour is unchanged for direct external refs without `x-go-name`: resolvedNameForRefPath already returns "" for non-`#/` refs, so the previous path also fell back to `<name> + mediaTypeSuffix`. The strict-server response envelope (`<name>JSONResponse`) ignores `x-go-name` on both sides and is unaffected. The cross-package collision-rename case noted in oapi-codegen#2422 remains out of scope: it is unrecoverable without resolving the external document, and affects all external refs equally. A regression test is added under internal/test/issues/issue-2422. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
Greptile SummaryThis PR fixes response-wrapper type resolution for direct external response references. The main changes are:
Confidence Score: 5/5This looks safe to merge.
|
| Filename | Overview |
|---|---|
| pkg/codegen/operations.go | Updates JSON response wrapper type resolution to handle direct external response refs and external x-go-name values. |
| internal/test/issues/issue-2422/issue_test.go | Adds compile-time regression coverage for direct external response refs using renamed external response models. |
Reviews (1): Last reviewed commit: "fix: honour external package's Go name f..." | Re-trigger Greptile
lwc
pushed a commit
to lwc/oapi-codegen
that referenced
this pull request
Jun 23, 2026
…-codegen#2423) Closes: oapi-codegen#2422 When a base spec references a response defined in an external file directly (`$ref: 'spec-ext.yaml#/components/responses/Outcome'`, not via a path item) and that response is renamed in the external package via `x-go-name`, the generated `client-with-responses` wrapper pointed at the raw component name. The imported package declares the renamed model (e.g. `OutcomeResult`), so the base client referenced an undefined `externalRef0.Outcome` and failed to compile. GetResponseTypeDefinitions resolved external response names with RefPathToGoType / resolvedNameForRefPath, which key off the root spec and the raw component name; neither consults the external component's `x-go-name`. The oapi-codegen#2308 fix already resolved this for externally-ref'd *path items* by reading the name from the resolved response value's context. This unifies that branch to also cover direct external refs: the imported package is detected via the path item's ref (relative response) or the response ref itself (direct), and the model name honours `x-go-name` the same way the external package generated it. Behaviour is unchanged for direct external refs without `x-go-name`: resolvedNameForRefPath already returns "" for non-`#/` refs, so the previous path also fell back to `<name> + mediaTypeSuffix`. The strict-server response envelope (`<name>JSONResponse`) ignores `x-go-name` on both sides and is unaffected. The cross-package collision-rename case noted in oapi-codegen#2422 remains out of scope: it is unrecoverable without resolving the external document, and affects all external refs equally. A regression test is added under internal/test/issues/issue-2422. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes: #2422
When a base spec references a response defined in an external file directly (
$ref: 'spec-ext.yaml#/components/responses/Outcome', not via a path item) and that response is renamed in the external package viax-go-name, the generatedclient-with-responseswrapper pointed at the raw component name. The imported package declares the renamed model (e.g.OutcomeResult), so the base client referenced an undefinedexternalRef0.Outcomeand failed to compile.GetResponseTypeDefinitions resolved external response names with RefPathToGoType / resolvedNameForRefPath, which key off the root spec and the raw component name; neither consults the external component's
x-go-name. The #2308 fix already resolved this for externally-ref'd path items by reading the name from the resolved response value's context. This unifies that branch to also cover direct external refs: the imported package is detected via the path item's ref (relative response) or the response ref itself (direct), and the model name honoursx-go-namethe same way the external package generated it.Behaviour is unchanged for direct external refs without
x-go-name: resolvedNameForRefPath already returns "" for non-#/refs, so the previous path also fell back to<name> + mediaTypeSuffix. The strict-server response envelope (<name>JSONResponse) ignoresx-go-nameon both sides and is unaffected.The cross-package collision-rename case noted in #2422 remains out of scope: it is unrecoverable without resolving the external document, and affects all external refs equally.
A regression test is added under internal/test/issues/issue-2422.