Fix bugs relating to optional fields with custom (un)marshalers (#116)

## Summary:
There were a few bugs here, one of which Craig came across when pulling
the custom-unmarshaler change into webapp:
1. If you have an optional field with a custom unmarshaler, and the
   server omits the field from the response entirely (i.e. does not
   write `"myField": null`), we would still call your unmarshaler with
   an input of `[]byte(nil)`.  This is just wrong; it's our job to do
   the nil-check.  (This is the one Craig found; in practice gqlgen
   servers do not do this and I think the spec says not to although it's
   a bit fuzzy on the matter of serialization.  But in practice we have
   mocks that do it -- for required fields even! -- and it seems better
   to handle it than pass you data on which you'll probably err or even
   panic.)
2. If you have an optional field with a custom unmarshaler, and the
   server returns an explicit null (i.e. `"myField": null`), we would
   call your unmarshaler with `[]byte("null")`.  In principle the intent
   was you're supposed to implement that, as [`json.Unmarshaler`
   advises][1].  But (a) I forgot to document that, and (b) in practice
   `json.Unmarshal` [does *not* call you in that case][2], i.e. its
   advice is unnecessary.  So I think it's better for us to just match
   it, and not call you.  (And in that case I see no reason to bother
   documenting the advice.)
3. If you have an optional, `pointer: true` field with a custom
   marshaler, the reverse of (2) applies: if the pointer is nil, we
   shouldn't really call you.  (Indeed if you were a real
   `json.Marshaler` with a value-method rather than a pointer-method,
   trying to call you might panic!)  Note we don't need to explicitly
   write "null"; we just leave the `json.RawMessage` as nil, and
   `json.Marshal` [handles that][3].
4. We handle interface types effectively the same as custom
   unmarshalers, just we generate the unmarshaler.  So if you have an
   optional field with interface type, (1) would also apply there; our
   generated unmarshaler returns an error in this case.
5. While (2) doesn't apply to such optional interface fields (because we
   do the customary `if string(b) == "null"` check -- this I at least
   thought to test), if you set `pointer: true` on the field, we would
   still call the unmarshaler on the value, and it would no-op, but only
   *after* we initialized the pointer.  Put more simply, we'd return a
   non-nil pointer to nil interface, rather than a nil pointer; this is
   wrong since the whole point of `pointer: true` is you only get a
   non-nil pointer if your value is nil!  Of course, in practice there's
   little reason to use `pointer: true` on interface fields, and indeed
   this stuff gets so confusing my test was even wrong.

In this commit I fix all the bugs, by adding appropriate nil-checks to
wrap the unmarshaler-calls.  The templates are, as always, a bit
confusing, but the generated code makes it clear what changed.

Note we'll want to land this before cutting a release with custom
marshaler/unmarshaler support, because the first three bugs are
potentially quite noticeable.  (The latter two are in `v0.1.0`, but
presumably quite rare.)

[1]: https://pkg.go.dev/encoding/json#Unmarshaler
[2]: https://play.golang.org/p/Pw6zNN8trGO
[3]: https://play.golang.org/p/crTfnT7ePte

Issue: https://phabricator.khanacademy.org/D74453#inline-558571

## Test plan:
make tesc


Author: benjaminjkraft

Reviewers: csilvers, StevenACoffman, benjaminjkraft, aberkan, dnerdy, jvoll, mahtabsabet, MiguelCastillo

Required Reviewers: 

Approved By: csilvers, StevenACoffman

Checks:  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Lint,  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Lint

Pull Request URL: https://github.com/Khan/genqlient/pull/116
This commit is contained in:
Ben Kraft
2021-09-27 20:35:39 -07:00
committed by GitHub
parent 47e9cea72e
commit 65c3e20ee6
24 changed files with 598 additions and 219 deletions
@@ -206,11 +206,13 @@ func (v *ComplexInlineFragmentsNestedStuffTopic) UnmarshalJSON(b []byte) error {
len(src))
for i, src := range src {
dst := &(*dst)[i]
err = __unmarshalComplexInlineFragmentsNestedStuffTopicChildrenContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal ComplexInlineFragmentsNestedStuffTopic.Children: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalComplexInlineFragmentsNestedStuffTopicChildrenContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal ComplexInlineFragmentsNestedStuffTopic.Children: %w", err)
}
}
}
}
@@ -257,11 +259,13 @@ func (v *ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParen
len(src))
for i, src := range src {
dst := &(*dst)[i]
err = __unmarshalComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopic.Children: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopicChildrenContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal ComplexInlineFragmentsNestedStuffTopicChildrenArticleParentContentParentTopic.Children: %w", err)
}
}
}
}
@@ -819,44 +823,52 @@ func (v *ComplexInlineFragmentsResponse) UnmarshalJSON(b []byte) error {
{
dst := &v.RandomItem
src := firstPass.RandomItem
err = __unmarshalComplexInlineFragmentsRandomItemContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal ComplexInlineFragmentsResponse.RandomItem: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalComplexInlineFragmentsRandomItemContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal ComplexInlineFragmentsResponse.RandomItem: %w", err)
}
}
}
{
dst := &v.RepeatedStuff
src := firstPass.RepeatedStuff
err = __unmarshalComplexInlineFragmentsRepeatedStuffContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal ComplexInlineFragmentsResponse.RepeatedStuff: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalComplexInlineFragmentsRepeatedStuffContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal ComplexInlineFragmentsResponse.RepeatedStuff: %w", err)
}
}
}
{
dst := &v.ConflictingStuff
src := firstPass.ConflictingStuff
err = __unmarshalComplexInlineFragmentsConflictingStuffContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal ComplexInlineFragmentsResponse.ConflictingStuff: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalComplexInlineFragmentsConflictingStuffContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal ComplexInlineFragmentsResponse.ConflictingStuff: %w", err)
}
}
}
{
dst := &v.NestedStuff
src := firstPass.NestedStuff
err = __unmarshalComplexInlineFragmentsNestedStuffContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal ComplexInlineFragmentsResponse.NestedStuff: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalComplexInlineFragmentsNestedStuffContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal ComplexInlineFragmentsResponse.NestedStuff: %w", err)
}
}
}
return nil
@@ -173,33 +173,39 @@ func (v *InnerQueryFragment) UnmarshalJSON(b []byte) error {
{
dst := &v.RandomItem
src := firstPass.RandomItem
err = __unmarshalInnerQueryFragmentRandomItemContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InnerQueryFragment.RandomItem: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalInnerQueryFragmentRandomItemContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InnerQueryFragment.RandomItem: %w", err)
}
}
}
{
dst := &v.RandomLeaf
src := firstPass.RandomLeaf
err = __unmarshalInnerQueryFragmentRandomLeafLeafContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InnerQueryFragment.RandomLeaf: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalInnerQueryFragmentRandomLeafLeafContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InnerQueryFragment.RandomLeaf: %w", err)
}
}
}
{
dst := &v.OtherLeaf
src := firstPass.OtherLeaf
err = __unmarshalInnerQueryFragmentOtherLeafLeafContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InnerQueryFragment.OtherLeaf: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalInnerQueryFragmentOtherLeafLeafContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InnerQueryFragment.OtherLeaf: %w", err)
}
}
}
return nil
@@ -703,11 +709,13 @@ func (v *MoreVideoFieldsParentTopic) UnmarshalJSON(b []byte) error {
len(src))
for i, src := range src {
dst := &(*dst)[i]
err = __unmarshalMoreVideoFieldsParentTopicChildrenContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal MoreVideoFieldsParentTopic.Children: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalMoreVideoFieldsParentTopicChildrenContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal MoreVideoFieldsParentTopic.Children: %w", err)
}
}
}
}
@@ -49,11 +49,13 @@ func (v *CustomMarshalUsersBornOnUser) UnmarshalJSON(b []byte) error {
{
dst := &v.Birthdate
src := firstPass.Birthdate
err = testutil.UnmarshalDate(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal CustomMarshalUsersBornOnUser.Birthdate: %w", err)
if len(src) != 0 && string(src) != "null" {
err = testutil.UnmarshalDate(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal CustomMarshalUsersBornOnUser.Birthdate: %w", err)
}
}
}
return nil
@@ -82,12 +82,14 @@ func (v *__CustomMarshalSliceInput) MarshalJSON() ([]byte, error) {
len(src))
for i, src := range src {
dst := &(*dst)[i]
var err error
*dst, err = testutil.MarshalDate(
src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal __CustomMarshalSliceInput.Datesssp: %w", err)
if src != nil {
var err error
*dst, err = testutil.MarshalDate(
src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal __CustomMarshalSliceInput.Datesssp: %w", err)
}
}
}
}
@@ -50,11 +50,13 @@ func (v *InterfaceListFieldRootTopic) UnmarshalJSON(b []byte) error {
len(src))
for i, src := range src {
dst := &(*dst)[i]
err = __unmarshalInterfaceListFieldRootTopicChildrenContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceListFieldRootTopic.Children: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalInterfaceListFieldRootTopicChildrenContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceListFieldRootTopic.Children: %w", err)
}
}
}
}
@@ -209,11 +211,13 @@ func (v *InterfaceListFieldWithPointerTopic) UnmarshalJSON(b []byte) error {
len(src))
for i, src := range src {
dst := &(*dst)[i]
err = __unmarshalInterfaceListFieldWithPointerTopicChildrenContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceListFieldWithPointerTopic.Children: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalInterfaceListFieldWithPointerTopicChildrenContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceListFieldWithPointerTopic.Children: %w", err)
}
}
}
}
@@ -185,11 +185,13 @@ func (v *InterfaceListOfListOfListsFieldResponse) UnmarshalJSON(b []byte) error
len(src))
for i, src := range src {
dst := &(*dst)[i]
err = __unmarshalInterfaceListOfListOfListsFieldListOfListsOfListsOfContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceListOfListOfListsFieldResponse.ListOfListsOfListsOfContent: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalInterfaceListOfListOfListsFieldListOfListsOfListsOfContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceListOfListOfListsFieldResponse.ListOfListsOfListsOfContent: %w", err)
}
}
}
}
@@ -214,12 +216,14 @@ func (v *InterfaceListOfListOfListsFieldResponse) UnmarshalJSON(b []byte) error
len(src))
for i, src := range src {
dst := &(*dst)[i]
*dst = new(InterfaceListOfListOfListsFieldWithPointerContent)
err = __unmarshalInterfaceListOfListOfListsFieldWithPointerContent(
src, *dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceListOfListOfListsFieldResponse.WithPointer: %w", err)
if len(src) != 0 && string(src) != "null" {
*dst = new(InterfaceListOfListOfListsFieldWithPointerContent)
err = __unmarshalInterfaceListOfListOfListsFieldWithPointerContent(
src, *dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceListOfListOfListsFieldResponse.WithPointer: %w", err)
}
}
}
}
@@ -48,11 +48,13 @@ func (v *InterfaceNestingRootTopic) UnmarshalJSON(b []byte) error {
len(src))
for i, src := range src {
dst := &(*dst)[i]
err = __unmarshalInterfaceNestingRootTopicChildrenContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceNestingRootTopic.Children: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalInterfaceNestingRootTopicChildrenContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceNestingRootTopic.Children: %w", err)
}
}
}
}
@@ -196,11 +198,13 @@ func (v *InterfaceNestingRootTopicChildrenContentParentTopic) UnmarshalJSON(b []
len(src))
for i, src := range src {
dst := &(*dst)[i]
err = __unmarshalInterfaceNestingRootTopicChildrenContentParentTopicChildrenContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceNestingRootTopicChildrenContentParentTopic.Children: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalInterfaceNestingRootTopicChildrenContentParentTopicChildrenContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceNestingRootTopicChildrenContentParentTopic.Children: %w", err)
}
}
}
}
@@ -275,34 +275,40 @@ func (v *InterfaceNoFragmentsQueryResponse) UnmarshalJSON(b []byte) error {
{
dst := &v.RandomItem
src := firstPass.RandomItem
err = __unmarshalInterfaceNoFragmentsQueryRandomItemContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceNoFragmentsQueryResponse.RandomItem: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalInterfaceNoFragmentsQueryRandomItemContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceNoFragmentsQueryResponse.RandomItem: %w", err)
}
}
}
{
dst := &v.RandomItemWithTypeName
src := firstPass.RandomItemWithTypeName
err = __unmarshalInterfaceNoFragmentsQueryRandomItemWithTypeNameContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceNoFragmentsQueryResponse.RandomItemWithTypeName: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalInterfaceNoFragmentsQueryRandomItemWithTypeNameContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceNoFragmentsQueryResponse.RandomItemWithTypeName: %w", err)
}
}
}
{
dst := &v.WithPointer
src := firstPass.WithPointer
*dst = new(InterfaceNoFragmentsQueryWithPointerContent)
err = __unmarshalInterfaceNoFragmentsQueryWithPointerContent(
src, *dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceNoFragmentsQueryResponse.WithPointer: %w", err)
if len(src) != 0 && string(src) != "null" {
*dst = new(InterfaceNoFragmentsQueryWithPointerContent)
err = __unmarshalInterfaceNoFragmentsQueryWithPointerContent(
src, *dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal InterfaceNoFragmentsQueryResponse.WithPointer: %w", err)
}
}
}
return nil
@@ -40,12 +40,14 @@ func (v *MyInput) MarshalJSON() ([]byte, error) {
dst := &fullObject.Birthdate
src := v.Birthdate
var err error
*dst, err = testutil.MarshalDate(
src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal MyInput.Birthdate: %w", err)
if src != nil {
var err error
*dst, err = testutil.MarshalDate(
src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal MyInput.Birthdate: %w", err)
}
}
}
@@ -127,12 +129,14 @@ func (v *UserQueryInput) MarshalJSON() ([]byte, error) {
dst := &fullObject.Birthdate
src := v.Birthdate
var err error
*dst, err = testutil.MarshalDate(
src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal UserQueryInput.Birthdate: %w", err)
if src != nil {
var err error
*dst, err = testutil.MarshalDate(
src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal UserQueryInput.Birthdate: %w", err)
}
}
}
@@ -95,12 +95,14 @@ func (v *UserQueryInput) MarshalJSON() ([]byte, error) {
dst := &fullObject.Birthdate
src := v.Birthdate
var err error
*dst, err = testutil.MarshalDate(
src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal UserQueryInput.Birthdate: %w", err)
if src != nil {
var err error
*dst, err = testutil.MarshalDate(
src)
if err != nil {
return nil, fmt.Errorf(
"Unable to marshal UserQueryInput.Birthdate: %w", err)
}
}
}
@@ -152,11 +152,13 @@ func (v *SimpleInlineFragmentResponse) UnmarshalJSON(b []byte) error {
{
dst := &v.RandomItem
src := firstPass.RandomItem
err = __unmarshalSimpleInlineFragmentRandomItemContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal SimpleInlineFragmentResponse.RandomItem: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalSimpleInlineFragmentRandomItemContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal SimpleInlineFragmentResponse.RandomItem: %w", err)
}
}
}
return nil
@@ -269,22 +269,26 @@ func (v *SimpleNamedFragmentResponse) UnmarshalJSON(b []byte) error {
{
dst := &v.RandomItem
src := firstPass.RandomItem
err = __unmarshalSimpleNamedFragmentRandomItemContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal SimpleNamedFragmentResponse.RandomItem: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalSimpleNamedFragmentRandomItemContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal SimpleNamedFragmentResponse.RandomItem: %w", err)
}
}
}
{
dst := &v.RandomLeaf
src := firstPass.RandomLeaf
err = __unmarshalSimpleNamedFragmentRandomLeafLeafContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal SimpleNamedFragmentResponse.RandomLeaf: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalSimpleNamedFragmentRandomLeafLeafContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal SimpleNamedFragmentResponse.RandomLeaf: %w", err)
}
}
}
return nil
@@ -86,11 +86,13 @@ func (v *StructOptionRootTopicChildrenContentParentTopic) UnmarshalJSON(b []byte
len(src))
for i, src := range src {
dst := &(*dst)[i]
err = __unmarshalStructOptionRootTopicChildrenContentParentTopicInterfaceChildrenContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal StructOptionRootTopicChildrenContentParentTopic.InterfaceChildren: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalStructOptionRootTopicChildrenContentParentTopicInterfaceChildrenContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal StructOptionRootTopicChildrenContentParentTopic.InterfaceChildren: %w", err)
}
}
}
}
@@ -153,11 +153,13 @@ func (v *Resp) UnmarshalJSON(b []byte) error {
{
dst := &v.RandomItem
src := firstPass.RandomItem
err = __unmarshalItem(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal Resp.RandomItem: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalItem(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal Resp.RandomItem: %w", err)
}
}
}
return nil
@@ -100,11 +100,13 @@ func (v *UnionNoFragmentsQueryResponse) UnmarshalJSON(b []byte) error {
{
dst := &v.RandomLeaf
src := firstPass.RandomLeaf
err = __unmarshalUnionNoFragmentsQueryRandomLeafLeafContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal UnionNoFragmentsQueryResponse.RandomLeaf: %w", err)
if len(src) != 0 && string(src) != "null" {
err = __unmarshalUnionNoFragmentsQueryRandomLeafLeafContent(
src, dst)
if err != nil {
return fmt.Errorf(
"Unable to unmarshal UnionNoFragmentsQueryResponse.RandomLeaf: %w", err)
}
}
}
return nil