-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
omitempty validation on input fields with/without defaults (#338)
Fixes #290 and #228 (comment) (for latter, only the comment of mine, not the whole issue) This is only changing where the `omitempty` is allowed and forbidden - it does not change where is the `omitempty` actually generated or not in generated code. Separately each line of changelog: - allow `omitempty` on non-nullable input field, if the field has a default (pretty much #228 (comment)) - added a `&& field.DefaultValue == nil` to the `"omitempty may only be used on optional arguments"` error - allow `omitempty: false` on an input field, even when it is non-nullable (#290) - `fieldDir.Omitempty != nil` changed to `fieldOptions.GetOmitempty()` - forbid `omitempty: false` (including implicit behaviour) when using pointer on non-null, no-default input field - as setting a correct combination of directives (and potentially some options, which changes implicit pointer and/or omitempty), and this library promises "Compile-time validation of GraphQL queries: never ship an invalid GraphQL query again!", I found it fitting to guard against the most simple case, that can be enforced in Go type system. - this, however, is a breaking change, so not sure if I should include it here. No previously present test failed after such change, but for example `generate/testdata/errors/DefaultInputsNoOmitPointerForDirective.graphql` would previously generate following (below - which has a possibility to send invalid graphql input), but now the generation fails. ``` type InputWithDefaults struct { Field *string `json:"field"` NullableField string `json:"nullableField"` } ``` - - alternative would be to force omitempty tag in such cases (even if there is no omitempty directive/option) - so that generation would not fail. But I'm not sure if I can afford to do that. That would probably still be breaking change (different generated code for same query), but a bit better. Maybe just setting omitempty flag instead of returning error would be sufficient. In general, I have also moved the omitempty check from directives to the time of creating Go types/tags of field. This seems more consistent, as not all possibilities were caught before (i.e. general `@genqlient(omitemtpy: true)` vs `@genqlient(for: ..., omitempty: true)`). When creating Go type/tags, all the options/directives are already merged, so the final result is being checked. There is minor difference in error message (instead of reference to directive, the error refers to the whole operation, but also includes type.field name) 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 - [ ] Included documentation, for new features - [x] Added an entry to the changelog
- Loading branch information
Showing
28 changed files
with
504 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# very similar to DefaultInputsNoOmitPointerForDirective.graphql - same expected behaviour, but takes a different code path(?) | ||
# @genqlient(pointer: true) | ||
query DefaultInputs( | ||
$input: InputWithDefaults! | ||
) { | ||
default(input: $input) | ||
} |
8 changes: 8 additions & 0 deletions
8
generate/testdata/errors/DefaultInputsNoOmitPointerForDirective.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Non-nullable input field with default cannot be pointer without omitempty - as that | ||
# would send `null`, which is invalid value. | ||
# @genqlient(for: "InputWithDefaults.field", pointer: true) | ||
query DefaultInputs( | ||
$input: InputWithDefaults! | ||
) { | ||
default(input: $input) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# One of the input filed is non-nullable -> cannot omit it | ||
# @genqlient(omitempty: true) | ||
query OmitemptyDirective ( | ||
$input: OmitemptyInput | ||
) { | ||
omitempty(input: $input) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# The specific input field is non-nullable -> cannot omit it | ||
# @genqlient(for: "OmitemptyInput.field", omitempty: true) | ||
query OmitemptyDirective ( | ||
$input: OmitemptyInput | ||
) { | ||
omitempty(input: $input) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Without any extra directives or configuration, the defaults are never considered, | ||
# as the client sends at least zero-value (struct with empty string). | ||
query DefaultInputs( | ||
$input: InputWithDefaults! | ||
) { | ||
default(input: $input) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# The `InputWithDefaults.field` cannot be `pointer: true`, together with implicit `omitempty: false`, as `null` is | ||
# not a valid value there. However, nullableField should still be ok | ||
# (this will send null, overwriting the server's default) | ||
# @genqlient(for: "InputWithDefaults.nullableField", pointer: true) | ||
query DefaultInputs( | ||
$input: InputWithDefaults! | ||
) { | ||
default(input: $input) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# very similar to DefaultInputsWithForDirective.graphql - same expected behaviour, but takes a different code path(?) | ||
# @genqlient(omitempty: true) | ||
query DefaultInputs( | ||
$input: InputWithDefaults! | ||
) { | ||
default(input: $input) | ||
} |
7 changes: 7 additions & 0 deletions
7
generate/testdata/queries/DefaultInputsWithForDirective.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# @genqlient(for: "InputWithDefaults.field", omitempty: true) | ||
# @genqlient(for: "InputWithDefaults.nullableField", omitempty: true) | ||
query DefaultInputs( | ||
$input: InputWithDefaults! | ||
) { | ||
default(input: $input) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# @genqlient(omitempty: true) | ||
# @genqlient(for: "OmitemptyInput.field",omitempty: false) | ||
query OmitemptyFalse( | ||
$input: OmitemptyInput | ||
) { | ||
omitempty(input: $input) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
generate/testdata/snapshots/TestGenerate-DefaultInputs.graphql-DefaultInputs.graphql.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
generate/testdata/snapshots/TestGenerate-DefaultInputs.graphql-DefaultInputs.graphql.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"operations": [ | ||
{ | ||
"operationName": "DefaultInputs", | ||
"query": "\nquery DefaultInputs ($input: InputWithDefaults!) {\n\tdefault(input: $input)\n}\n", | ||
"sourceLocation": "testdata/queries/DefaultInputs.graphql" | ||
} | ||
] | ||
} |
70 changes: 70 additions & 0 deletions
70
...tdata/snapshots/TestGenerate-DefaultInputsPointer.graphql-DefaultInputsPointer.graphql.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
...ata/snapshots/TestGenerate-DefaultInputsPointer.graphql-DefaultInputsPointer.graphql.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"operations": [ | ||
{ | ||
"operationName": "DefaultInputs", | ||
"query": "\nquery DefaultInputs ($input: InputWithDefaults!) {\n\tdefault(input: $input)\n}\n", | ||
"sourceLocation": "testdata/queries/DefaultInputsPointer.graphql" | ||
} | ||
] | ||
} |
Oops, something went wrong.