Skip to content

Commit

Permalink
fix: premarshal structs get generated with omitempty tag (#267)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kevinmichaelchen authored May 5, 2023
1 parent 4073a49 commit 15f507b
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 22 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ When releasing a new version:
- Fixed non-deterministic generated code when querying graphql interfaces.
- Fixed generated code when last component of package name is not a valid identifier (e.g. `"path/to/my-package"`).
- Fixed incorrect documentation of `for` directive.
- Fixed bug where `omitempty` JSON tags were not being correctly applied to `__premarshal` structs.

## v0.5.0

Expand Down
4 changes: 2 additions & 2 deletions generate/marshal.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
type __premarshal{{.GoName}} struct{
{{range .FlattenedFields -}}
{{if .NeedsMarshaling -}}
{{.GoName}} {{repeat .GoType.SliceDepth "[]"}}{{ref "encoding/json.RawMessage"}} `json:"{{.JSONName}}"`
{{.GoName}} {{repeat .GoType.SliceDepth "[]"}}{{ref "encoding/json.RawMessage"}} `json:"{{.JSONName}}{{if .Omitempty -}},omitempty{{end}}"`
{{else}}
{{.GoName}} {{.GoType.Reference}} `json:"{{.JSONName}}"`
{{.GoName}} {{.GoType.Reference}} `json:"{{.JSONName}}{{if .Omitempty -}},omitempty{{end}}"`
{{end}}
{{end}}
}
Expand Down
7 changes: 7 additions & 0 deletions generate/testdata/queries/Hasura.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @genqlient(pointer: true)
query GetPokemon($where: getPokemonBoolExp!) {
getPokemon(where: $where) {
species
level
}
}
20 changes: 20 additions & 0 deletions generate/testdata/queries/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,28 @@ type Query {
listOfListsOfListsOfContent: [[[Content!]!]!]!
recur(input: RecursiveInput!): Recursive
acceptsListOfListOfListsOfDates(datesss: [[[Date!]!]!]!): Boolean
getPokemon(where: getPokemonBoolExp): [Pokemon!]!
}

type Mutation {
createUser(name: String!, email: String): User
}

input getPokemonBoolExp {
_and: [getPokemonBoolExp!]
_not: getPokemonBoolExp
_or: [getPokemonBoolExp!]
level: IntComparisonExp
}

input IntComparisonExp {
_eq: Int
_gt: Int
_gte: Int
_in: [Int!]
_isNull: Boolean
_lt: Int
_lte: Int
_neq: Int
_nin: [Int!]
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"operations": [
{
"operationName": "GetPokemon",
"query": "\nquery GetPokemon ($where: getPokemonBoolExp!) {\n\tgetPokemon(where: $where) {\n\t\tspecies\n\t\tlevel\n\t}\n}\n",
"sourceLocation": "testdata/queries/Hasura.graphql"
}
]
}

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

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

0 comments on commit 15f507b

Please sign in to comment.