diff --git a/openapi3gen/openapi3gen.go b/openapi3gen/openapi3gen.go index 9eae7ccbb..78e6901dc 100644 --- a/openapi3gen/openapi3gen.go +++ b/openapi3gen/openapi3gen.go @@ -195,14 +195,16 @@ func (g *Generator) generateWithoutSaving(parents []*theTypeInfo, t reflect.Type return nil, &CycleError{} } } - - if cap(parents) == 0 { + isRoot := cap(parents) == 0 + if isRoot { parents = make([]*theTypeInfo, 0, 4) } parents = append(parents, typeInfo) + isNullable := false for t.Kind() == reflect.Ptr { t = t.Elem() + isNullable = !isRoot } if strings.HasSuffix(t.Name(), "Ref") { @@ -231,6 +233,7 @@ func (g *Generator) generateWithoutSaving(parents []*theTypeInfo, t reflect.Type } schema := &openapi3.Schema{} + schema.Nullable = isNullable switch t.Kind() { case reflect.Func, reflect.Chan: diff --git a/openapi3gen/openapi3gen_test.go b/openapi3gen/openapi3gen_test.go index 8263983a7..4d9c1e208 100644 --- a/openapi3gen/openapi3gen_test.go +++ b/openapi3gen/openapi3gen_test.go @@ -108,11 +108,13 @@ func ExampleGenerator_SchemaRefs() { // "json": {}, // "map": { // "additionalProperties": { + // "nullable": true, // "type": "string" // }, // "type": "object" // }, // "ptr": { + // "nullable": true, // "type": "string" // }, // "slice": { @@ -178,6 +180,7 @@ func ExampleThrowErrorOnCycle() { // schemaRef: { // "properties": { // "a": { + // "nullable": true, // "properties": { // "b": { // "$ref": "#/components/schemas/CyclicType0" @@ -192,6 +195,7 @@ func ExampleThrowErrorOnCycle() { // "CyclicType0": { // "properties": { // "a": { + // "nullable": true, // "properties": { // "b": { // "$ref": "#/components/schemas/CyclicType0" diff --git a/openapi3gen/simple_test.go b/openapi3gen/simple_test.go index 99e94ae12..11b3b26a4 100644 --- a/openapi3gen/simple_test.go +++ b/openapi3gen/simple_test.go @@ -70,11 +70,13 @@ func Example() { // "json": {}, // "map": { // "additionalProperties": { + // "nullable": true, // "type": "string" // }, // "type": "object" // }, // "ptr": { + // "nullable": true, // "type": "string" // }, // "slice": {