Skip to content

Commit 7eaaa47

Browse files
authored
fix(spanner): result from unmarshal of string and spanner.NullString type from json should be consistent. (#5263)
Co-authored-by: Rahul Yadav <irahul@google.com>
1 parent 22df34b commit 7eaaa47

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

spanner/value.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,17 @@ func (n *NullString) UnmarshalJSON(payload []byte) error {
286286
n.Valid = false
287287
return nil
288288
}
289-
payload, err := trimDoubleQuotes(payload)
290-
if err != nil {
289+
var s *string
290+
if err := json.Unmarshal(payload, &s); err != nil {
291291
return err
292292
}
293-
n.StringVal = string(payload)
294-
n.Valid = true
293+
if s != nil {
294+
n.StringVal = *s
295+
n.Valid = true
296+
} else {
297+
n.StringVal = ""
298+
n.Valid = false
299+
}
295300
return nil
296301
}
297302

spanner/value_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,6 +2647,7 @@ func TestJSONUnmarshal_NullTypes(t *testing.T) {
26472647
{input: []byte(`"this is a test string"`), got: NullString{}, isNull: false, expect: "this is a test string", expectError: false},
26482648
{input: []byte(`""`), got: NullString{}, isNull: false, expect: "", expectError: false},
26492649
{input: []byte("null"), got: NullString{}, isNull: true, expect: nullString, expectError: false},
2650+
{input: []byte(`"{\"sub_a\": \"value_1\"}"`), got: NullString{}, isNull: false, expect: `{"sub_a": "value_1"}`, expectError: false},
26502651
{input: nil, got: NullString{}, isNull: true, expect: nullString, expectError: true},
26512652
{input: []byte(""), got: NullString{}, isNull: true, expect: nullString, expectError: true},
26522653
{input: []byte(`"hello`), got: NullString{}, isNull: true, expect: nullString, expectError: true},

0 commit comments

Comments
 (0)