Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions internal/test/issues/issue-2113/common/spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
openapi: "3.0.4"
info:
title: Common
version: "0.0.1"
paths: {}
components:
schemas:
ProblemDetails:
type: object
properties:
title:
type: string
status:
type: integer
required: [title, status]
responses:
StandardError:
description: Error response
content:
application/json:
schema:
$ref: "#/components/schemas/ProblemDetails"
11 changes: 11 additions & 0 deletions internal/test/issues/issue-2113/config.api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# yaml-language-server: $schema=../../../../configuration-schema.json
package: api
generate:
chi-server: true
strict-server: true
models: true
import-mapping:
./common/spec.yaml: github.com/oapi-codegen/oapi-codegen/v2/internal/test/issues/issue-2113/gen/common
output: gen/api/api.gen.go
output-options:
skip-prune: true
7 changes: 7 additions & 0 deletions internal/test/issues/issue-2113/config.common.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# yaml-language-server: $schema=../../../../configuration-schema.json
package: common
generate:
models: true
output: gen/common/common.gen.go
output-options:
skip-prune: true
4 changes: 4 additions & 0 deletions internal/test/issues/issue-2113/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package issue2113

//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.common.yaml common/spec.yaml
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.api.yaml spec.yaml
271 changes: 271 additions & 0 deletions internal/test/issues/issue-2113/gen/api/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions internal/test/issues/issue-2113/gen/common/common.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions internal/test/issues/issue-2113/issue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package issue2113

import (
"testing"

"github.com/oapi-codegen/oapi-codegen/v2/internal/test/issues/issue-2113/gen/api"
"github.com/oapi-codegen/oapi-codegen/v2/internal/test/issues/issue-2113/gen/common"
)

// TestExternalRefInResponse verifies that a $ref to an external
// components/responses object correctly qualifies the schema type
// with the external package import. See
// https://github.com/oapi-codegen/oapi-codegen/issues/2113
func TestExternalRefInResponse(t *testing.T) {
// This will fail to compile if the generated code uses
// ProblemDetails instead of common.ProblemDetails (via the
// externalRef alias) in the default response type.
_ = api.ListThingsdefaultJSONResponse{
Body: common.ProblemDetails{Title: "err", Status: 500},
StatusCode: 500,
}
}
21 changes: 21 additions & 0 deletions internal/test/issues/issue-2113/spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
openapi: "3.0.4"
info:
title: API
version: "0.0.1"
paths:
/things:
get:
operationId: listThings
responses:
"200":
description: OK
content:
application/json:
schema:
type: array
items:
type: string
"400":
$ref: "./common/spec.yaml#/components/responses/StandardError"
default:
$ref: "./common/spec.yaml#/components/responses/StandardError"
6 changes: 6 additions & 0 deletions pkg/codegen/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,12 @@ func GenerateResponseDefinitions(operationID string, responses map[string]*opena
rd.Ref = refType
refSet[refType] = struct{}{}
}
// Ensure content schemas get the external ref qualifier so that
// non-fixed status code paths (e.g. "default") emit the qualified type.
for i, rcd := range rd.Contents {
ensureExternalRefsInSchema(&rcd.Schema, responseOrRef.Ref)
rd.Contents[i] = rcd
}
}
responseDefinitions = append(responseDefinitions, rd)
}
Expand Down