Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #1530 use discriminator with several mappings for one schema #1534

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/test/issues/issue-1530/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package: param
generate:
models: true
output: issue1530.gen.go
3 changes: 3 additions & 0 deletions internal/test/issues/issue-1530/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package param

//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml issue1530.yaml
141 changes: 141 additions & 0 deletions internal/test/issues/issue-1530/issue1530.gen.go

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

40 changes: 40 additions & 0 deletions internal/test/issues/issue-1530/issue1530.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
openapi: 3.0.0
paths:
/test:
get:
operationId: Test
responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/pet'
components:
schemas:
dog:
type: object
required:
- breed
properties:
breed:
type: string
enum: [ poodle, beagle ]
cat:
type: object
required:
- breed
properties:
breed:
type: string
enum: [ maine_coon, ragdoll ]
pet:
oneOf:
- $ref: "#/components/schemas/dog"
- $ref: "#/components/schemas/cat"
discriminator:
propertyName: breed
mapping:
beagle: "#/components/schemas/dog"
maine_coon: "#/components/schemas/cat"
poodle: "#/components/schemas/dog"
ragdoll: "#/components/schemas/cat"
3 changes: 1 addition & 2 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,6 @@ func generateUnion(outSchema *Schema, elements openapi3.SchemaRefs, discriminato
if v == element.Ref {
outSchema.Discriminator.Mapping[k] = elementSchema.GoType
mapped = true
break
}
}
// Implicit mapping.
Expand All @@ -874,7 +873,7 @@ func generateUnion(outSchema *Schema, elements openapi3.SchemaRefs, discriminato
outSchema.UnionElements = append(outSchema.UnionElements, UnionElement(elementSchema.GoType))
}

if (outSchema.Discriminator != nil) && len(outSchema.Discriminator.Mapping) != len(elements) {
if (outSchema.Discriminator != nil) && len(outSchema.Discriminator.Mapping) < len(elements) {
return errors.New("discriminator: not all schemas were mapped")
}

Expand Down