Skip to content
This repository was archived by the owner on Apr 14, 2026. It is now read-only.

Commit d04991e

Browse files
mromaszewiczclaude
andauthored
fix: qualify external ref schema types in default response codes (oapi-codegen#2241)
When a response uses a $ref to an external components/responses definition, the generated strict-server code for non-fixed status codes (e.g. default) used the inner schema type without the external package qualifier, causing a compile error. Apply ensureExternalRefsInSchema to response content schemas after resolving external response refs. Closes oapi-codegen#2113 Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 15fc536 commit d04991e

9 files changed

Lines changed: 377 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
openapi: "3.0.4"
2+
info:
3+
title: Common
4+
version: "0.0.1"
5+
paths: {}
6+
components:
7+
schemas:
8+
ProblemDetails:
9+
type: object
10+
properties:
11+
title:
12+
type: string
13+
status:
14+
type: integer
15+
required: [title, status]
16+
responses:
17+
StandardError:
18+
description: Error response
19+
content:
20+
application/json:
21+
schema:
22+
$ref: "#/components/schemas/ProblemDetails"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# yaml-language-server: $schema=../../../../configuration-schema.json
2+
package: api
3+
generate:
4+
chi-server: true
5+
strict-server: true
6+
models: true
7+
import-mapping:
8+
./common/spec.yaml: github.com/oapi-codegen/oapi-codegen/v2/internal/test/issues/issue-2113/gen/common
9+
output: gen/api/api.gen.go
10+
output-options:
11+
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package issue2113
2+
3+
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.common.yaml common/spec.yaml
4+
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.api.yaml spec.yaml

internal/test/issues/issue-2113/gen/api/api.gen.go

Lines changed: 271 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/test/issues/issue-2113/gen/common/common.gen.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package issue2113
2+
3+
import (
4+
"testing"
5+
6+
"github.com/oapi-codegen/oapi-codegen/v2/internal/test/issues/issue-2113/gen/api"
7+
"github.com/oapi-codegen/oapi-codegen/v2/internal/test/issues/issue-2113/gen/common"
8+
)
9+
10+
// TestExternalRefInResponse verifies that a $ref to an external
11+
// components/responses object correctly qualifies the schema type
12+
// with the external package import. See
13+
// https://github.com/oapi-codegen/oapi-codegen/issues/2113
14+
func TestExternalRefInResponse(t *testing.T) {
15+
// This will fail to compile if the generated code uses
16+
// ProblemDetails instead of common.ProblemDetails (via the
17+
// externalRef alias) in the default response type.
18+
_ = api.ListThingsdefaultJSONResponse{
19+
Body: common.ProblemDetails{Title: "err", Status: 500},
20+
StatusCode: 500,
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
openapi: "3.0.4"
2+
info:
3+
title: API
4+
version: "0.0.1"
5+
paths:
6+
/things:
7+
get:
8+
operationId: listThings
9+
responses:
10+
"200":
11+
description: OK
12+
content:
13+
application/json:
14+
schema:
15+
type: array
16+
items:
17+
type: string
18+
"400":
19+
$ref: "./common/spec.yaml#/components/responses/StandardError"
20+
default:
21+
$ref: "./common/spec.yaml#/components/responses/StandardError"

pkg/codegen/operations.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,12 @@ func GenerateResponseDefinitions(operationID string, responses map[string]*opena
914914
rd.Ref = refType
915915
refSet[refType] = struct{}{}
916916
}
917+
// Ensure content schemas get the external ref qualifier so that
918+
// non-fixed status code paths (e.g. "default") emit the qualified type.
919+
for i, rcd := range rd.Contents {
920+
ensureExternalRefsInSchema(&rcd.Schema, responseOrRef.Ref)
921+
rd.Contents[i] = rcd
922+
}
917923
}
918924
responseDefinitions = append(responseDefinitions, rd)
919925
}

0 commit comments

Comments
 (0)