Support nullable slice elements and map values · oapi-codegen/oapi-codegen@bf89b07 · GitHub
Skip to content

Commit bf89b07

Browse files
Support nullable slice elements and map values
1 parent 09919e7 commit bf89b07

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

pkg/codegen/codegen_test.go

Lines changed: 8 additions & 1 deletion

pkg/codegen/schema.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,9 @@ func oapiSchemaToGoType(schema *openapi3.Schema, path []string, outSchema *Schem
587587
if err != nil {
588588
return fmt.Errorf("error generating type for array: %w", err)
589589
}
590+
591+
var itemPrefix string
592+
590593
if (arrayType.HasAdditionalProperties || len(arrayType.UnionElements) != 0) && arrayType.RefType == "" {
591594
// If we have items which have additional properties or union values,
592595
// but are not a pre-defined type, we need to define a type
@@ -603,8 +606,13 @@ func oapiSchemaToGoType(schema *openapi3.Schema, path []string, outSchema *Schem
603606

604607
arrayType.RefType = typeName
605608
}
609+
610+
if arrayType.OAPISchema != nil && arrayType.OAPISchema.Nullable {
611+
itemPrefix = "*"
612+
}
613+
606614
outSchema.ArrayType = &arrayType
607-
outSchema.GoType = "[]" + arrayType.TypeDecl()
615+
outSchema.GoType = "[]" + itemPrefix + arrayType.TypeDecl()
608616
outSchema.AdditionalTypes = arrayType.AdditionalTypes
609617
outSchema.Properties = arrayType.Properties
610618
outSchema.DefineViaAlias = true

pkg/codegen/test_spec.yaml

Lines changed: 30 additions & 0 deletions

0 commit comments

Comments
 (0)