Skip to content

Commit

Permalink
fixes #968 - openapi3gen: generate "nullable: true" for pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
endertunc committed Jul 19, 2024
1 parent 1a819a1 commit d869f20
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions openapi3gen/openapi3gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions openapi3gen/openapi3gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ func ExampleGenerator_SchemaRefs() {
// "json": {},
// "map": {
// "additionalProperties": {
// "nullable": true,
// "type": "string"
// },
// "type": "object"
// },
// "ptr": {
// "nullable": true,
// "type": "string"
// },
// "slice": {
Expand Down Expand Up @@ -178,6 +180,7 @@ func ExampleThrowErrorOnCycle() {
// schemaRef: {
// "properties": {
// "a": {
// "nullable": true,
// "properties": {
// "b": {
// "$ref": "#/components/schemas/CyclicType0"
Expand All @@ -192,6 +195,7 @@ func ExampleThrowErrorOnCycle() {
// "CyclicType0": {
// "properties": {
// "a": {
// "nullable": true,
// "properties": {
// "b": {
// "$ref": "#/components/schemas/CyclicType0"
Expand Down
2 changes: 2 additions & 0 deletions openapi3gen/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ func Example() {
// "json": {},
// "map": {
// "additionalProperties": {
// "nullable": true,
// "type": "string"
// },
// "type": "object"
// },
// "ptr": {
// "nullable": true,
// "type": "string"
// },
// "slice": {
Expand Down

0 comments on commit d869f20

Please sign in to comment.