Skip to content

fix: qualify response refs from externally-ref'd path items#2421

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

fix: qualify response refs from externally-ref'd path items#2421
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2308

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Closes: #2308

When a path item is referenced from an external file (an OpenAPI 3.1 components.pathItems entry referenced from paths), and its operation's response is in turn a $ref relative to that same external file (#/components/responses/...), the generated strict-server response struct embedded an unqualified type name. That type only exists in the imported package, so the generated code referenced an undefined symbol and failed to compile:

type GetV1Version200JSONResponse struct{ VersionGetResponseJSONResponse }

GenerateResponseDefinitions already tracks the external package of the path item (externalPkg) and applies that prefix to inline response content, but not to the response-is-a-$ref branch. There, RefPathToGoType returns a bare name for a #/-relative ref -- it cannot know the ref originated in an external file once the path item has been merged into the root document.

Qualify the ref with externalPkg when the operation came from an external path item and the response ref is relative (#-prefixed). Refs that already target another file are qualified by RefPathToGoType, so those are left untouched to avoid double-prefixing.

A regression test is added under internal/test/issues/issue-2308.

@mromaszewicz
mromaszewicz requested a review from a team as a code owner June 21, 2026 02:39
@mromaszewicz mromaszewicz added the enhancement New feature or request label Jun 21, 2026
@greptile-apps

greptile-apps Bot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes response type qualification for externally referenced OpenAPI path items. The main changes are:

  • Qualifies relative response refs from external path items with the imported package name.
  • Updates client response wrapper type resolution for externally sourced response refs.
  • Adds an issue-2308 regression fixture with base and external specs.
  • Adds generated code and a compile-time test for the strict-server response wrapper case.

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 response type generation so relative response refs from external path items resolve through the imported package.
internal/test/issues/issue-2308/issue_test.go Adds compile-time coverage for the external path-item response wrapper type.

Reviews (4): Last reviewed commit: "fix: qualify response refs from external..." | Re-trigger Greptile

Comment thread pkg/codegen/operations.go
@mromaszewicz
mromaszewicz force-pushed the fix/issue-2308 branch 2 times, most recently from 3258696 to b200a65 Compare June 21, 2026 04:07
Comment thread pkg/codegen/operations.go
Closes: oapi-codegen#2308

When a path item is referenced from an external file (an OpenAPI 3.1
`components.pathItems` entry referenced from `paths`), and its operation's
response is in turn a `$ref` relative to that same external file
(`#/components/responses/...`), the generated strict-server response struct
embedded an unqualified type name. That type only exists in the imported
package, so the generated code referenced an undefined symbol and failed to
compile:

	type GetV1Version200JSONResponse struct{ VersionGetResponseJSONResponse }

GenerateResponseDefinitions already tracks the external package of the path
item (externalPkg) and applies that prefix to inline response content, but
not to the response-is-a-`$ref` branch. There, RefPathToGoType returns a
bare name for a `#/`-relative ref -- it cannot know the ref originated in an
external file once the path item has been merged into the root document.

Qualify the ref with externalPkg when the operation came from an external
path item and the response ref is relative (`#`-prefixed). Refs that already
target another file are qualified by RefPathToGoType, so those are left
untouched to avoid double-prefixing.

A regression test is added under internal/test/issues/issue-2308.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@mromaszewicz
mromaszewicz merged commit 5b7f232 into oapi-codegen:main Jun 21, 2026
14 checks passed
@mromaszewicz
mromaszewicz deleted the fix/issue-2308 branch June 21, 2026 04:25
lwc pushed a commit to lwc/oapi-codegen that referenced this pull request Jun 23, 2026
…egen#2421)

Closes: oapi-codegen#2308

When a path item is referenced from an external file (an OpenAPI 3.1
`components.pathItems` entry referenced from `paths`), and its operation's
response is in turn a `$ref` relative to that same external file
(`#/components/responses/...`), the generated strict-server response struct
embedded an unqualified type name. That type only exists in the imported
package, so the generated code referenced an undefined symbol and failed to
compile:

	type GetV1Version200JSONResponse struct{ VersionGetResponseJSONResponse }

GenerateResponseDefinitions already tracks the external package of the path
item (externalPkg) and applies that prefix to inline response content, but
not to the response-is-a-`$ref` branch. There, RefPathToGoType returns a
bare name for a `#/`-relative ref -- it cannot know the ref originated in an
external file once the path item has been merged into the root document.

Qualify the ref with externalPkg when the operation came from an external
path item and the response ref is relative (`#`-prefixed). Refs that already
target another file are qualified by RefPathToGoType, so those are left
untouched to avoid double-prefixing.

A regression test is added under internal/test/issues/issue-2308.

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

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PathItem support missing

1 participant