Skip to content

fix: honour external package's Go name for direct response refs#2423

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2422
Jun 21, 2026
Merged

fix: honour external package's Go name for direct response refs#2423
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2422

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

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 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 #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 #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.

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>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner June 21, 2026 04:34
@mromaszewicz mromaszewicz added the bug Something isn't working label Jun 21, 2026
@greptile-apps

greptile-apps Bot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes response-wrapper type resolution for direct external response references. The main changes are:

  • Direct external response refs now use the imported package name.
  • External response x-go-name values are honored for JSON client response fields.
  • A new issue-2422 regression fixture covers the base and external spec generation path.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

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

@mromaszewicz
mromaszewicz merged commit 2ec2dc6 into oapi-codegen:main Jun 21, 2026
15 checks passed
@mromaszewicz
mromaszewicz deleted the fix/issue-2422 branch June 21, 2026 04:41
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

External response refs ignore the imported package's Go name (x-go-name / collision renames)

1 participant