Skip to content

Commit

Permalink
ENG-22892 fix: UnmarshalRawMessage of a gql quoted string (#153)
Browse files Browse the repository at this point in the history
(Fix idea suggested by @jarrodb)

Bug with this GQL schema: `json: JSON!`
and with the input `json: "json quotes strings"`

Output is wrong: `json.RawMessage("json quotes strings")`
For example this always fails:

```
var res any
json.Unmarshal(json.RawMessage("json quotes strings"), &res)
// invalid character 'j' looking for beginning of value
```

Correcting to:   `json.RawMessage("\"json quotes strings\"")`

---------

Signed-off-by: Jakub Bielecki <47531708+jabielecki@users.noreply.github.com>
  • Loading branch information
jabielecki authored Sep 28, 2023
1 parent f271e55 commit 0d965b2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
2 changes: 0 additions & 2 deletions entx/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ func MarshalRawMessage(t json.RawMessage) graphql.Marshaler {
// UnmarshalRawMessage provides a graphql.Unmarshaler for json.RawMessage
func UnmarshalRawMessage(v interface{}) (json.RawMessage, error) {
switch j := v.(type) {
case string:
return UnmarshalRawMessage([]byte(j))
case []byte:
return json.RawMessage(j), nil
case map[string]interface{}:
Expand Down
60 changes: 60 additions & 0 deletions entx/json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2023 The Infratographer Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package entx

import (
"encoding/json"
"reflect"
"testing"
)

func TestUnmarshalRawMessage(t *testing.T) {
tests := []struct {
name string
arg interface{}
want json.RawMessage
wantErr bool
}{{
name: "map",
arg: map[string]any{"a": true},
want: json.RawMessage(`{"a":true}`),
}, {
name: "array",
arg: []int{1, 2},
want: json.RawMessage(`[1,2]`),
}, {
name: "bytes",
arg: []byte{'"', 'a', '"'},
want: json.RawMessage(`"a"`),
}, {
// In practice, this is the way graphql Unmarshal is processing input like {json: "a"}:
name: "string",
arg: "a",
want: json.RawMessage(`"a"`),
}}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := UnmarshalRawMessage(tt.arg)
if (err != nil) != tt.wantErr {
t.Errorf("UnmarshalRawMessage() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("UnmarshalRawMessage() = %s, want %s", got, tt.want)
}
})
}
}

0 comments on commit 0d965b2

Please sign in to comment.