-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENG-22892 fix: UnmarshalRawMessage of a gql quoted string (#153)
(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
1 parent
f271e55
commit 0d965b2
Showing
2 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |