Skip to content

Commit 60302f3

Browse files
author
huo
committed
fix: add omitempty support for list types with pointer_omitempty
When using use_struct_references: true with optional: pointer_omitempty, list types like [String!] were not getting the omitempty tag because the convertType function returned early for slice types, skipping the pointer_omitempty handling logic. This fix ensures that list types also respect the pointer_omitempty configuration when the list itself is nullable.
1 parent 5b0aabc commit 60302f3

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

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.

0 commit comments

Comments
 (0)