Skip to content
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

Add omitempty json tags for nullable fields #2436

Merged
merged 1 commit into from
Mar 9, 2023
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
2 changes: 1 addition & 1 deletion _examples/scalars/model/generated.go

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

8 changes: 4 additions & 4 deletions _examples/selection/models_gen.go

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

2 changes: 1 addition & 1 deletion _examples/starwars/models/generated.go

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

2 changes: 1 addition & 1 deletion _examples/todo/models_gen.go

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

38 changes: 19 additions & 19 deletions codegen/testserver/followschema/models-gen.go

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

38 changes: 19 additions & 19 deletions codegen/testserver/singlefile/models-gen.go

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

12 changes: 6 additions & 6 deletions integration/models-go/generated.go

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

4 changes: 2 additions & 2 deletions plugin/federation/testdata/entityresolver/generated/models.go

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

9 changes: 8 additions & 1 deletion plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (m *Plugin) generateFields(cfg *config.Config, schemaType *ast.Definition)
GoName: name,
Type: typ,
Description: field.Description,
Tag: `json:"` + field.Name + `"`,
Tag: getStructTagFromField(field),
}

if m.FieldHook != nil {
Expand All @@ -388,6 +388,13 @@ func (m *Plugin) generateFields(cfg *config.Config, schemaType *ast.Definition)
return fields, nil
}

func getStructTagFromField(field *ast.FieldDefinition) string {
if !field.Type.NonNull {
return `json:"` + field.Name + `,omitempty"`
}
return `json:"` + field.Name + `"`
}

// GoTagFieldHook prepends the goTag directive to the generated Field f.
// When applying the Tag to the field, the field
// name is used if no value argument is present.
Expand Down
16 changes: 8 additions & 8 deletions plugin/modelgen/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func TestModelGeneration(t *testing.T) {

expectedTags := []string{
`json:"missing2" database:"MissingTypeNotNullmissing2"`,
`json:"name" database:"MissingInputname"`,
`json:"missing2" database:"MissingTypeNullablemissing2"`,
`json:"name" database:"TypeWithDescriptionname"`,
`json:"name,omitempty" database:"MissingInputname"`,
`json:"missing2,omitempty" database:"MissingTypeNullablemissing2"`,
`json:"name,omitempty" database:"TypeWithDescriptionname"`,
}

for _, tag := range expectedTags {
require.True(t, strings.Contains(fileText, tag))
require.True(t, strings.Contains(fileText, tag), "\nexpected:\n"+tag+"\ngot\n"+fileText)
}
})

Expand All @@ -82,10 +82,10 @@ func TestModelGeneration(t *testing.T) {
fileText := string(file)

expectedTags := []string{
`json:"name" anotherTag:"tag"`,
`json:"enum" yetAnotherTag:"12"`,
`json:"noVal" yaml:"noVal" repeated:"true"`,
`json:"repeated" someTag:"value" repeated:"true"`,
`json:"name,omitempty" anotherTag:"tag"`,
`json:"enum,omitempty" yetAnotherTag:"12"`,
`json:"noVal,omitempty" yaml:"noVal" repeated:"true"`,
`json:"repeated,omitempty" someTag:"value" repeated:"true"`,
}

for _, tag := range expectedTags {
Expand Down
Loading