Skip to content

Commit 8583f53

Browse files
gofulljshuogofulljs
authored
fix: add omitempty support for list types with pointer_omitempty (#413)
fixed `pointer_omitempty` not being applied to list types when `use_struct_references` is enabled. List fields like `[String!]` now correctly get the `omitempty` JSON tag --------- Co-authored-by: huo <huo@gmail.com> Co-authored-by: gofulljs <gofulljs@gmail.com>
1 parent f0f1687 commit 8583f53

7 files changed

Lines changed: 184 additions & 1 deletion

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Note that genqlient now requires Go 1.23 or higher, and is tested through Go 1.2
3232

3333
### Bug fixes:
3434

35+
- fixed `pointer_omitempty` not being applied to list types when `use_struct_references` is enabled. List fields like `[String!]` now correctly get the `omitempty` JSON tag.
3536
- fixed minor typos and grammatical issues across the project
3637

3738
## v0.8.1

generate/convert.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,20 @@ func (g *generator) convertType(
239239
// Type is a list.
240240
elem, err := g.convertType(
241241
namePrefix, typ.Elem, selectionSet, options, queryOptions)
242-
return &goSliceType{elem}, err
242+
// For list types, we need to handle pointer_omitempty here because
243+
// the code below won't be executed for slices.
244+
// If the list itself is nullable (e.g., [String!] vs [String!]!),
245+
// and pointer_omitempty is configured, we should set omitempty.
246+
if err != nil {
247+
return nil, err
248+
}
249+
if g.Config.Optional == "pointer_omitempty" && !typ.NonNull {
250+
if options.Omitempty == nil {
251+
oe := true
252+
options.Omitempty = &oe
253+
}
254+
}
255+
return &goSliceType{elem}, nil
243256
}
244257

245258
// If this is a builtin type or custom scalar, just refer to it.

generate/generate_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ func TestGenerateWithConfig(t *testing.T) {
245245
"PointersOmitEmpty.graphql",
246246
"Omitempty.graphql",
247247
"ListInput.graphql",
248+
"ListInputOmitempty.graphql",
248249
}, &Config{
249250
Optional: "pointer_omitempty",
250251
Bindings: map[string]*TypeBinding{
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
query ListInputOmitemptyQuery($names: [String!]) {
2+
user(query: {names: $names}) {
3+
id
4+
}
5+
}

generate/testdata/snapshots/TestGenerate-ListInputOmitempty.graphql-ListInputOmitempty.graphql.go

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"operations": [
3+
{
4+
"operationName": "ListInputOmitemptyQuery",
5+
"query": "\nquery ListInputOmitemptyQuery ($names: [String!]) {\n\tuser(query: {names:$names}) {\n\t\tid\n\t}\n}\n",
6+
"sourceLocation": "testdata/queries/ListInputOmitempty.graphql"
7+
}
8+
]
9+
}

generate/testdata/snapshots/TestGenerateWithConfig-OptionalPointerOmitEmpty-ListInputOmitempty.graphql-testdata-queries-generated.go

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)