From 2f0adde62190264a33772f69aca318d43a8d1676 Mon Sep 17 00:00:00 2001 From: Yamashou <1230124fw@gmail.com> Date: Thu, 4 Jul 2024 00:36:06 +0900 Subject: [PATCH 1/6] fix --- clientv2/client.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clientv2/client.go b/clientv2/client.go index 1b08563..3ed2266 100644 --- a/clientv2/client.go +++ b/clientv2/client.go @@ -417,6 +417,10 @@ func checkImplements[I any](v reflect.Value) bool { // encode returns an appropriate encoder function for the provided value. func encode(v reflect.Value) ([]byte, error) { + if v.IsNil() { + return []byte("null"), nil + } + if checkImplements[graphql.Marshaler](v) { return encodeGQLMarshaler(v.Interface()) } From 441f6fa72d914cf1db81b84c44f5b4e4c2c47ca1 Mon Sep 17 00:00:00 2001 From: Yamashou <1230124fw@gmail.com> Date: Thu, 4 Jul 2024 00:51:33 +0900 Subject: [PATCH 2/6] fix --- clientv2/client.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clientv2/client.go b/clientv2/client.go index 3ed2266..acceab9 100644 --- a/clientv2/client.go +++ b/clientv2/client.go @@ -417,10 +417,6 @@ func checkImplements[I any](v reflect.Value) bool { // encode returns an appropriate encoder function for the provided value. func encode(v reflect.Value) ([]byte, error) { - if v.IsNil() { - return []byte("null"), nil - } - if checkImplements[graphql.Marshaler](v) { return encodeGQLMarshaler(v.Interface()) } @@ -461,6 +457,10 @@ func encode(v reflect.Value) ([]byte, error) { } func encodeGQLMarshaler(v any) ([]byte, error) { + if v == nil { + return []byte("null"), nil + } + var buf bytes.Buffer if val, ok := v.(graphql.Marshaler); ok { val.MarshalGQL(&buf) From c5ad4ebd0843da503c39d54ba1a7263311288060 Mon Sep 17 00:00:00 2001 From: Yamashou <1230124fw@gmail.com> Date: Thu, 4 Jul 2024 01:03:44 +0900 Subject: [PATCH 3/6] back --- clientv2/client.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/clientv2/client.go b/clientv2/client.go index acceab9..1b08563 100644 --- a/clientv2/client.go +++ b/clientv2/client.go @@ -457,10 +457,6 @@ func encode(v reflect.Value) ([]byte, error) { } func encodeGQLMarshaler(v any) ([]byte, error) { - if v == nil { - return []byte("null"), nil - } - var buf bytes.Buffer if val, ok := v.(graphql.Marshaler); ok { val.MarshalGQL(&buf) From 173af5f416f87557bf0c619ad0f558fbf14bcbc0 Mon Sep 17 00:00:00 2001 From: Yamashou <1230124fw@gmail.com> Date: Thu, 4 Jul 2024 01:39:01 +0900 Subject: [PATCH 4/6] fix --- clientv2/client.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clientv2/client.go b/clientv2/client.go index 1b08563..9d8a4d4 100644 --- a/clientv2/client.go +++ b/clientv2/client.go @@ -459,6 +459,10 @@ func encode(v reflect.Value) ([]byte, error) { func encodeGQLMarshaler(v any) ([]byte, error) { var buf bytes.Buffer if val, ok := v.(graphql.Marshaler); ok { + if val == nil { + return []byte("null"), nil + } + val.MarshalGQL(&buf) } else { return nil, fmt.Errorf("failed to encode graphql.Marshaler: %v", v) From 54630e0bdb96d0a8f28f9d0cf27fa96babc5fc6c Mon Sep 17 00:00:00 2001 From: Yamashou <1230124fw@gmail.com> Date: Thu, 4 Jul 2024 02:44:50 +0900 Subject: [PATCH 5/6] fix --- clientv2/client.go | 16 ++++++++++------ clientv2/client_test.go | 20 +++++++++++++++++++- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/clientv2/client.go b/clientv2/client.go index 9d8a4d4..d0182fc 100644 --- a/clientv2/client.go +++ b/clientv2/client.go @@ -396,12 +396,12 @@ func (c *Client) unmarshal(data []byte, res interface{}) error { func MarshalJSON(v interface{}) ([]byte, error) { if v == nil { - return []byte("null"), nil // Directly return "null" for nil interface{} + return []byte("null"), nil } val := reflect.ValueOf(v) if !val.IsValid() || (val.Kind() == reflect.Ptr && val.IsNil()) { - return []byte("null"), nil // Return "null" for nil pointer or invalid reflect value + return []byte("null"), nil } return encode(val) @@ -417,6 +417,10 @@ func checkImplements[I any](v reflect.Value) bool { // encode returns an appropriate encoder function for the provided value. func encode(v reflect.Value) ([]byte, error) { + if !v.IsValid() || (v.Kind() == reflect.Ptr && v.IsNil()) { + return []byte("null"), nil + } + if checkImplements[graphql.Marshaler](v) { return encodeGQLMarshaler(v.Interface()) } @@ -457,12 +461,12 @@ func encode(v reflect.Value) ([]byte, error) { } func encodeGQLMarshaler(v any) ([]byte, error) { + if v == nil { + return []byte("null"), nil + } + var buf bytes.Buffer if val, ok := v.(graphql.Marshaler); ok { - if val == nil { - return []byte("null"), nil - } - val.MarshalGQL(&buf) } else { return nil, fmt.Errorf("failed to encode graphql.Marshaler: %v", v) diff --git a/clientv2/client_test.go b/clientv2/client_test.go index 124d659..43e7a56 100644 --- a/clientv2/client_test.go +++ b/clientv2/client_test.go @@ -579,6 +579,12 @@ func TestMarshalJSON(t *testing.T) { Number Number `json:"number"` } + type Example2 struct { + Name string `json:"name"` + Number *Number `json:"number,omitempty"` + } + var b *Number + // example nested struct type WhereInput struct { Not *WhereInput `json:"not,omitempty"` @@ -640,6 +646,18 @@ func TestMarshalJSON(t *testing.T) { }, want: []byte(`{"operationName":"query", "query":"query ($input: Number!) { input }","variables":{"where":{"not":{"id":"1"}}}}`), }, + { + name: "marshal nil", + args: args{ + v: Request{ + OperationName: "query", + Variables: map[string]any{ + "v": b, + }, + }, + }, + want: []byte(`{"operationName":"query", "query":"","variables":{"v":null}}`), + }, { name: "marshal a struct with custom marshaler", args: args{ @@ -738,7 +756,7 @@ func TestMarshalJSON(t *testing.T) { return } if err := json.Unmarshal(tt.want, &wantMap); err != nil { - t.Errorf("Failed to unmarshal 'want': %s", tt.want) + t.Errorf("Failed to unmarshal err: %s", err) return } From 747916a375997fd2245e4972d88cf6a4b39e0372 Mon Sep 17 00:00:00 2001 From: Yamashou <1230124fw@gmail.com> Date: Thu, 4 Jul 2024 20:54:21 +0900 Subject: [PATCH 6/6] delete test struct --- clientv2/client_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/clientv2/client_test.go b/clientv2/client_test.go index 43e7a56..23415b9 100644 --- a/clientv2/client_test.go +++ b/clientv2/client_test.go @@ -579,10 +579,6 @@ func TestMarshalJSON(t *testing.T) { Number Number `json:"number"` } - type Example2 struct { - Name string `json:"name"` - Number *Number `json:"number,omitempty"` - } var b *Number // example nested struct