Skip to content

Commit 1e31932

Browse files
mromaszewiczclaude
andauthored
Qualify external header schema refs in generated response headers (oapi-codegen#2463)
Closes: oapi-codegen#2060 When a response header is an external `$ref` (it lives in another file mapped via import-mapping) and that header's schema is itself a `$ref` to a named type, the generated response-headers struct referenced the type by a bare local name (e.g. `ETag ETagSchema`) instead of the imported one (`ETag externalRef0.ETagSchema`). Because the named type is only generated into the imported package, the referencing package failed to compile with an undefined `ETagSchema`. The header's schema `$ref` is written relative to the external file (e.g. `#/components/schemas/ETagSchema`), so `GenerateGoSchema` resolves it against the root spec as a bare local name. Since the header component carries its own `$ref` telling us which file it came from, qualify the schema with that external package via `ensureExternalRefsInSchema`, mirroring how response content schemas are already handled. This is guarded on the schema being a reference so an external header with an inline primitive schema (e.g. `type: string`) is not mangled into `externalRef0.string`. The fix corrects both the strict-server `<Op><Status>ResponseHeaders` struct and the client response wrapper's header fields introduced in oapi-codegen#2462. Adds internal/test/references/multipackage/header_ref exercising an external header ref with a ref'd schema across strict-server and client generation; the committed generated files must compile as part of the test module, which guards against the undefined-symbol regression. Co-authored-by: Claude Fable 5 <[email protected]>
1 parent 9efd95a commit 1e31932

9 files changed

Lines changed: 690 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# From issue-2060: common spec providing an externally referenced header whose
2+
# schema is a $ref to a named type in this same package.
3+
openapi: "3.0.4"
4+
info:
5+
title: Common
6+
version: "0.0.1"
7+
paths: {}
8+
components:
9+
schemas:
10+
ETagSchema:
11+
type: string
12+
description: RFC 7232 entity tag
13+
headers:
14+
ETag:
15+
description: Entity tag header
16+
schema:
17+
$ref: "#/components/schemas/ETagSchema"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# yaml-language-server: $schema=../../../../../configuration-schema.json
2+
package: api
3+
generate:
4+
chi-server: true
5+
strict-server: true
6+
client: true
7+
models: true
8+
import-mapping:
9+
./common/spec.yaml: github.com/oapi-codegen/oapi-codegen/v2/internal/test/references/multipackage/header_ref/gen/common
10+
output: gen/api/api.gen.go
11+
output-options:
12+
skip-prune: true
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# yaml-language-server: $schema=../../../../../configuration-schema.json
2+
package: common
3+
generate:
4+
models: true
5+
output: gen/common/common.gen.go
6+
output-options:
7+
skip-prune: true
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Package headerref is a regression test for issue-2060: a response header
2+
// that is an external `$ref` whose schema is itself a `$ref` to a named type
3+
// must qualify that type with the imported package (e.g.
4+
// externalRef0.ETagSchema), both in the strict-server response headers struct
5+
// and in the typed client response wrappers. Before the fix the header field
6+
// referenced an undefined local type and the generated package failed to
7+
// compile.
8+
package headerref
9+
10+
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.common.yaml common/spec.yaml
11+
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.api.yaml spec.yaml

0 commit comments

Comments
 (0)