Skip to content
Closed
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
40 changes: 16 additions & 24 deletions internal/test/components/components.gen.go

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

18 changes: 16 additions & 2 deletions pkg/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,20 +863,34 @@ func GenerateAdditionalPropertyBoilerplate(t *template.Template, typeDefs []Type

func GenerateUnionBoilerplate(t *template.Template, typeDefs []TypeDefinition) (string, error) {
var filteredTypes []TypeDefinition
unionTypes := make(map[string]TypeDefinition)
for _, t := range typeDefs {
if len(t.Schema.UnionElements) != 0 {
filteredTypes = append(filteredTypes, t)
for _, el := range t.Schema.UnionElements {
unionTypes[el.String()] = TypeDefinition{}
}
}
}

if len(filteredTypes) == 0 {
return "", nil
}

for _, t := range typeDefs {
for k, _ := range unionTypes {
if t.TypeName == k {
unionTypes[k] = t
}
}
}

context := struct {
Types []TypeDefinition
Types []TypeDefinition
UnionTypes map[string]TypeDefinition
}{
Types: filteredTypes,
Types: filteredTypes,
UnionTypes: unionTypes,
}

return GenerateTemplates([]string{"union.tmpl"}, t, context)
Expand Down
29 changes: 27 additions & 2 deletions pkg/codegen/templates/union.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{$unionTypes := .UnionTypes}}
{{range .Types}}
{{$typeName := .TypeName -}}
{{$discriminator := .Schema.Discriminator}}
Expand All @@ -23,7 +24,19 @@
{{$hasProperty = true -}}
{{end -}}
{{end -}}
{{if not $hasProperty}}v.{{$discriminator.PropertyName}} = "{{$value}}"{{end}}
{{if not $hasProperty -}}
{{$discriminatorType := (index $unionTypes $element.String) -}}
{{range $discriminatorType.Schema.Properties -}}
{{if eq .JsonFieldName $discriminator.Property -}}
{{if .Nullable -}}
d := {{ .Schema.GoType }}("{{$value}}")
v.{{$discriminator.PropertyName}} = &d
{{else -}}
v.{{$discriminator.PropertyName}} = {{ .Schema.GoType }}("{{$value}}")
{{end -}}
{{end -}}
{{end -}}
{{end -}}
{{end -}}
{{end -}}
{{end -}}
Expand All @@ -44,7 +57,19 @@
{{$hasProperty = true -}}
{{end -}}
{{end -}}
{{if not $hasProperty}}v.{{$discriminator.PropertyName}} = "{{$value}}"{{end}}
{{if not $hasProperty -}}
{{$discriminatorType := (index $unionTypes $element.String) -}}
{{range $discriminatorType.Schema.Properties -}}
{{if eq .JsonFieldName $discriminator.Property -}}
{{if .Nullable -}}
d := {{ .Schema.GoType }}("{{$value}}")
v.{{$discriminator.PropertyName}} = &d
{{else -}}
v.{{$discriminator.PropertyName}} = {{ .Schema.GoType }}("{{$value}}")
{{end -}}
{{end -}}
{{end -}}
{{end -}}
{{end -}}
{{end -}}
{{end -}}
Expand Down
76 changes: 75 additions & 1 deletion pkg/codegen/test_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,25 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'

/stuff:
post:
description: Create stuff
operationId: createStuff
security:
- Bearer: []
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/CreateStuff"
responses:
"201":
description: created stuff
content:
application/json:
schema:
$ref: "#/components/schemas/Stuff"
components:
schemas:

Expand Down Expand Up @@ -205,3 +223,59 @@ components:
name:
type: string
description: User name
CreateStuff:
oneOf:
- $ref: "#/components/schemas/CreateStuffBla"
- $ref: "#/components/schemas/CreateStuffNope"
discriminator:
propertyName: which
mapping:
bla: "#/components/schemas/CreateStuffBla"
nope: "#/components/schemas/CreateStuffNope"
CreateStuffProperties:
type: object
required:
- which
properties:
which:
$ref: "#/components/schemas/WhichStuff"
CreateStuffBla:
allOf:
- $ref: "#/components/schemas/CreateStuffProperties"
- type: object
properties:
settings:
$ref: "#/components/schemas/StuffBla"
CreateStuffNope:
allOf:
- $ref: "#/components/schemas/CreateStuffProperties"
- type: object
properties:
settings:
$ref: "#/components/schemas/StuffNope"
Stuff:
type: object
required:
- which
properties:
which:
$ref: "#/components/schemas/WhichStuff"
WhichStuff:
type: string
nullable: true
enum:
[
bla,
nope,
]
default: bla
StuffBla:
type: object
properties:
something:
type: string
StuffNope:
type: object
properties:
nothing:
type: string