omitempty validation on input fields with/without defaults #338
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 theomitempty
actually generated or not in generated code.Separately each line of changelog:
omitempty
on non-nullable input field, if the field has a default (pretty much InputObject field defaults are ignored/overridden #228 (comment))&& field.DefaultValue == nil
to the"omitempty may only be used on optional arguments"
erroromitempty: false
on an input field, even when it is non-nullable (# @genqlient(for: "Input.nonOptionalField", omitempty: false)
not working #290)fieldDir.Omitempty != nil
changed tofieldOptions.GetOmitempty()
omitempty: false
(including implicit behaviour) when using pointer on non-null, no-default input fieldgenerate/testdata/errors/DefaultInputsNoOmitPointerForDirective.graphql
would previously generate following (below - which has a possibility to send invalid graphql input), but now the generation fails.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: