Skip to content
Merged
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
8 changes: 8 additions & 0 deletions internal/test/issues/issue-2185/config.yaml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions internal/test/issues/issue-2185/generate.go
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions internal/test/issues/issue-2185/issue2185.gen.go

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

19 changes: 19 additions & 0 deletions internal/test/issues/issue-2185/issue2185_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
15 changes: 15 additions & 0 deletions internal/test/issues/issue-2185/spec.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 7 additions & 4 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down