Support nullable slice elements and map values (#2185) · oapi-codegen/oapi-codegen@f4ac5df · GitHub
Skip to content

Commit f4ac5df

Browse files
Support nullable slice elements and map values (#2185)
1 parent 59fb0e8 commit f4ac5df

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
@@ -602,6 +602,9 @@ func oapiSchemaToGoType(schema *openapi3.Schema, path []string, outSchema *Schem
602602
if err != nil {
603603
return fmt.Errorf("error generating type for array: %w", err)
604604
}
605+
606+
var itemPrefix string
607+
605608
if (arrayType.HasAdditionalProperties || len(arrayType.UnionElements) != 0) && arrayType.RefType == "" {
606609
// If we have items which have additional properties or union values,
607610
// but are not a pre-defined type, we need to define a type
@@ -618,8 +621,13 @@ func oapiSchemaToGoType(schema *openapi3.Schema, path []string, outSchema *Schem
618621

619622
arrayType.RefType = typeName
620623
}
624+
625+
if arrayType.OAPISchema != nil && arrayType.OAPISchema.Nullable {
626+
itemPrefix = "*"
627+
}
628+
621629
outSchema.ArrayType = &arrayType
622-
outSchema.GoType = "[]" + arrayType.TypeDecl()
630+
outSchema.GoType = "[]" + itemPrefix + arrayType.TypeDecl()
623631
outSchema.AdditionalTypes = arrayType.AdditionalTypes
624632
outSchema.Properties = arrayType.Properties
625633
outSchema.DefineViaAlias = true

pkg/codegen/test_spec.yaml

Lines changed: 30 additions & 0 deletions

0 commit comments

Comments
 (0)