Skip to content

Commit 15f507b

Browse files
fix: premarshal structs get generated with omitempty tag (#267)
This PR addresses a bug described in #263 Basically, whenever a struct involves a custom genqlient binding, a secondary "premarshal" struct gets generated. The bug was that this "premarshal" struct was not propagating the `omitempty` JSON tags, which was resulting in unexpected behavior. The fix involved a few changed lines in the Go template, and a few changes in the unit tests. I have: - [x] Written a clear PR title and description (above) - [x] Signed the [Khan Academy CLA](https://www.khanacademy.org/r/cla) - [x] Added tests covering my changes, if applicable - [x] Included a link to the issue fixed, if applicable - [x] Included documentation, for new features (n/a) - [x] Added an entry to the changelog
1 parent 4073a49 commit 15f507b

8 files changed

+177
-22
lines changed

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ When releasing a new version:
3434
- Fixed non-deterministic generated code when querying graphql interfaces.
3535
- Fixed generated code when last component of package name is not a valid identifier (e.g. `"path/to/my-package"`).
3636
- Fixed incorrect documentation of `for` directive.
37+
- Fixed bug where `omitempty` JSON tags were not being correctly applied to `__premarshal` structs.
3738

3839
## v0.5.0
3940

generate/marshal.go.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
type __premarshal{{.GoName}} struct{
3232
{{range .FlattenedFields -}}
3333
{{if .NeedsMarshaling -}}
34-
{{.GoName}} {{repeat .GoType.SliceDepth "[]"}}{{ref "encoding/json.RawMessage"}} `json:"{{.JSONName}}"`
34+
{{.GoName}} {{repeat .GoType.SliceDepth "[]"}}{{ref "encoding/json.RawMessage"}} `json:"{{.JSONName}}{{if .Omitempty -}},omitempty{{end}}"`
3535
{{else}}
36-
{{.GoName}} {{.GoType.Reference}} `json:"{{.JSONName}}"`
36+
{{.GoName}} {{.GoType.Reference}} `json:"{{.JSONName}}{{if .Omitempty -}},omitempty{{end}}"`
3737
{{end}}
3838
{{end}}
3939
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# @genqlient(pointer: true)
2+
query GetPokemon($where: getPokemonBoolExp!) {
3+
getPokemon(where: $where) {
4+
species
5+
level
6+
}
7+
}

generate/testdata/queries/schema.graphql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,28 @@ type Query {
180180
listOfListsOfListsOfContent: [[[Content!]!]!]!
181181
recur(input: RecursiveInput!): Recursive
182182
acceptsListOfListOfListsOfDates(datesss: [[[Date!]!]!]!): Boolean
183+
getPokemon(where: getPokemonBoolExp): [Pokemon!]!
183184
}
184185

185186
type Mutation {
186187
createUser(name: String!, email: String): User
187188
}
189+
190+
input getPokemonBoolExp {
191+
_and: [getPokemonBoolExp!]
192+
_not: getPokemonBoolExp
193+
_or: [getPokemonBoolExp!]
194+
level: IntComparisonExp
195+
}
196+
197+
input IntComparisonExp {
198+
_eq: Int
199+
_gt: Int
200+
_gte: Int
201+
_in: [Int!]
202+
_isNull: Boolean
203+
_lt: Int
204+
_lte: Int
205+
_neq: Int
206+
_nin: [Int!]
207+
}

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

Lines changed: 118 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": "GetPokemon",
5+
"query": "\nquery GetPokemon ($where: getPokemonBoolExp!) {\n\tgetPokemon(where: $where) {\n\t\tspecies\n\t\tlevel\n\t}\n}\n",
6+
"sourceLocation": "testdata/queries/Hasura.graphql"
7+
}
8+
]
9+
}

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

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

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

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

0 commit comments

Comments
 (0)