Skip to content

Commit 99eadbe

Browse files
committed
fix(codegen): generate nullable.Nullable in arrays
As a follow-up to #2185, we can make sure we generate a Nullable type if required to do so.
1 parent 12a9b20 commit 99eadbe

File tree

6 files changed

+65
-4
lines changed

6 files changed

+65
-4
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# yaml-language-server: $schema=../../../../configuration-schema.json
2+
package: issue2185
3+
output: issue2185.gen.go
4+
generate:
5+
models: true
6+
output-options:
7+
skip-prune: true
8+
nullable-type: true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package issue2185
2+
3+
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml spec.yaml

internal/test/issues/issue-2185/issue2185.gen.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package issue2185
2+
3+
import (
4+
"testing"
5+
6+
"github.com/oapi-codegen/nullable"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestContainer_UsesNullableType(t *testing.T) {
11+
c := Container{
12+
MayBeNull: []nullable.Nullable[string]{
13+
nullable.NewNullNullable[string](),
14+
},
15+
}
16+
17+
require.Len(t, c.MayBeNull, 1)
18+
require.True(t, c.MayBeNull[0].IsNull())
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
openapi: "3.0.3"
2+
info:
3+
title: test
4+
version: 1.0.0
5+
components:
6+
schemas:
7+
Container:
8+
required:
9+
- "may-be-null"
10+
properties:
11+
"may-be-null":
12+
type: array
13+
items:
14+
type: string
15+
nullable: true

pkg/codegen/schema.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,8 +603,6 @@ func oapiSchemaToGoType(schema *openapi3.Schema, path []string, outSchema *Schem
603603
return fmt.Errorf("error generating type for array: %w", err)
604604
}
605605

606-
var itemPrefix string
607-
608606
if (arrayType.HasAdditionalProperties || len(arrayType.UnionElements) != 0) && arrayType.RefType == "" {
609607
// If we have items which have additional properties or union values,
610608
// but are not a pre-defined type, we need to define a type
@@ -622,12 +620,17 @@ func oapiSchemaToGoType(schema *openapi3.Schema, path []string, outSchema *Schem
622620
arrayType.RefType = typeName
623621
}
624622

623+
typeDeclaration := arrayType.TypeDecl()
625624
if arrayType.OAPISchema != nil && arrayType.OAPISchema.Nullable {
626-
itemPrefix = "*"
625+
if globalState.options.OutputOptions.NullableType {
626+
typeDeclaration = "nullable.Nullable[" + typeDeclaration + "]"
627+
} else {
628+
typeDeclaration = "*" + typeDeclaration
629+
}
627630
}
628631

629632
outSchema.ArrayType = &arrayType
630-
outSchema.GoType = "[]" + itemPrefix + arrayType.TypeDecl()
633+
outSchema.GoType = "[]" + typeDeclaration
631634
outSchema.AdditionalTypes = arrayType.AdditionalTypes
632635
outSchema.Properties = arrayType.Properties
633636
outSchema.DefineViaAlias = true

0 commit comments

Comments
 (0)