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.
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)
Fixes https://github.com/Khan/genqlient/issues/290 and https://github.com/Khan/genqlient/issues/228#issuecomment-2023396706 (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 https://github.com/Khan/genqlient/issues/228#issuecomment-2023396706)&& 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 (https://github.com/Khan/genqlient/issues/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: