diff --git a/internal/test/issues/issue-2185/config.yaml b/internal/test/issues/issue-2185/config.yaml new file mode 100644 index 0000000000..71a7ba132e --- /dev/null +++ b/internal/test/issues/issue-2185/config.yaml @@ -0,0 +1,8 @@ +# yaml-language-server: $schema=../../../../configuration-schema.json +package: issue2185 +output: issue2185.gen.go +generate: + models: true +output-options: + skip-prune: true + nullable-type: true diff --git a/internal/test/issues/issue-2185/generate.go b/internal/test/issues/issue-2185/generate.go new file mode 100644 index 0000000000..9f5224eaeb --- /dev/null +++ b/internal/test/issues/issue-2185/generate.go @@ -0,0 +1,3 @@ +package issue2185 + +//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml spec.yaml diff --git a/internal/test/issues/issue-2185/issue2185.gen.go b/internal/test/issues/issue-2185/issue2185.gen.go new file mode 100644 index 0000000000..6474c9c4d0 --- /dev/null +++ b/internal/test/issues/issue-2185/issue2185.gen.go @@ -0,0 +1,13 @@ +// Package issue2185 provides primitives to interact with the openapi HTTP API. +// +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.0.0-00010101000000-000000000000 DO NOT EDIT. +package issue2185 + +import ( + "github.com/oapi-codegen/nullable" +) + +// Container defines model for Container. +type Container struct { + MayBeNull []nullable.Nullable[string] `json:"may-be-null"` +} diff --git a/internal/test/issues/issue-2185/issue2185_test.go b/internal/test/issues/issue-2185/issue2185_test.go new file mode 100644 index 0000000000..cc98e2b4d3 --- /dev/null +++ b/internal/test/issues/issue-2185/issue2185_test.go @@ -0,0 +1,19 @@ +package issue2185 + +import ( + "testing" + + "github.com/oapi-codegen/nullable" + "github.com/stretchr/testify/require" +) + +func TestContainer_UsesNullableType(t *testing.T) { + c := Container{ + MayBeNull: []nullable.Nullable[string]{ + nullable.NewNullNullable[string](), + }, + } + + require.Len(t, c.MayBeNull, 1) + require.True(t, c.MayBeNull[0].IsNull()) +} diff --git a/internal/test/issues/issue-2185/spec.yaml b/internal/test/issues/issue-2185/spec.yaml new file mode 100644 index 0000000000..814048bc8f --- /dev/null +++ b/internal/test/issues/issue-2185/spec.yaml @@ -0,0 +1,15 @@ +openapi: "3.0.3" +info: + title: test + version: 1.0.0 +components: + schemas: + Container: + required: + - "may-be-null" + properties: + "may-be-null": + type: array + items: + type: string + nullable: true diff --git a/pkg/codegen/schema.go b/pkg/codegen/schema.go index cc14db731f..ea6e137473 100644 --- a/pkg/codegen/schema.go +++ b/pkg/codegen/schema.go @@ -603,8 +603,6 @@ func oapiSchemaToGoType(schema *openapi3.Schema, path []string, outSchema *Schem return fmt.Errorf("error generating type for array: %w", err) } - var itemPrefix string - if (arrayType.HasAdditionalProperties || len(arrayType.UnionElements) != 0) && arrayType.RefType == "" { // If we have items which have additional properties or union values, // 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 arrayType.RefType = typeName } + typeDeclaration := arrayType.TypeDecl() if arrayType.OAPISchema != nil && arrayType.OAPISchema.Nullable { - itemPrefix = "*" + if globalState.options.OutputOptions.NullableType { + typeDeclaration = "nullable.Nullable[" + typeDeclaration + "]" + } else { + typeDeclaration = "*" + typeDeclaration + } } outSchema.ArrayType = &arrayType - outSchema.GoType = "[]" + itemPrefix + arrayType.TypeDecl() + outSchema.GoType = "[]" + typeDeclaration outSchema.AdditionalTypes = arrayType.AdditionalTypes outSchema.Properties = arrayType.Properties outSchema.DefineViaAlias = true