Skip to content

Commit

Permalink
fix: #1530 use discriminator with several mappings for one schema
Browse files Browse the repository at this point in the history
  • Loading branch information
jKiler committed Apr 8, 2024
1 parent 80f0b97 commit 605d9e3
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 2 deletions.
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

0 comments on commit 605d9e3

Please sign in to comment.