Skip to content

fix: set nullable when it's a pointer of Struct #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion openapi/generator.go
Original file line number Diff line number Diff line change
@@ -895,7 +895,17 @@ func (g *Generator) newSchemaFromType(t reflect.Type) *SchemaOrRef {
case reflect.Slice, reflect.Array, reflect.Map:
return g.buildSchemaRecursive(t)
case reflect.Struct:
return g.newSchemaFromStruct(t)
sor := g.newSchemaFromStruct(t)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already check if the type is "nullable" above when dereferencing the pointer. We could simply check if the schema pointed by the ref is non-nil and set the Nullable property accordingly.

var schema *Schema
if sor != nil && sor.Schema != nil {
schema = sor.Schema
} else if sor != nil {
schema = g.resolveSchema(sor)
}
if schema != nil {
schema.Nullable = nullable
}
return sor
}
}
if dt == TypeAny {
1 change: 1 addition & 0 deletions openapi/generator_test.go
Original file line number Diff line number Diff line change
@@ -228,6 +228,7 @@ func TestSchemaFromComplex(t *testing.T) {
sor = g.API().Components.Schemas["Y"]
schema = g.resolveSchema(sor)
assert.NotNil(t, schema)
assert.True(t, schema.Nullable)

actual, err = json.Marshal(schema)
if err != nil {