Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spanner: Different behavior between string and spanner.NullString when unmarshal from json #5259

Closed
CuberL opened this issue Jan 2, 2022 · 0 comments · Fixed by #5263
Closed
Assignees
Labels
api: spanner Issues related to the Spanner API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@CuberL
Copy link

CuberL commented Jan 2, 2022

Client

Spanner

Environment

Local

Go Environment

$ go version
go version go1.17.2 darwin/amd64
$ go env

Code

package main

import (
	"encoding/json"
	"fmt"

	"cloud.google.com/go/spanner"
)

type Example struct {
	FieldA spanner.NullString `json:"field_a"`
	FieldB string             `json:"field_b"`
}

func main() {
	exampleStr := []byte(`
{
	"field_a": "{\"sub_a\": \"value_1\"}",
        "field_b": "{\"sub_b\": \"value_2\"}"
}
`)
	e := &Example{}
	json.Unmarshal(exampleStr, e)
	fmt.Println(e.FieldA)
	fmt.Println(e.FieldB)
}

Expected behavior

Output:

{"sub_a": "value_1"}
{"sub_b": "value_2"}

Actual behavior

{\"sub_a\": \"value_1\"}
{"sub_b": "value_2"}

Additional context

payload, err := trimDoubleQuotes(payload)

Seems that the code didn't handle this case and will have different behavior between string and spanner.NullString when unmarshal from json and some escape characters is in the string.

@CuberL CuberL added the triage me I really want to be triaged. label Jan 2, 2022
@product-auto-label product-auto-label bot added the api: spanner Issues related to the Spanner API. label Jan 2, 2022
@rahul2393 rahul2393 added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. and removed triage me I really want to be triaged. labels Jan 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the Spanner API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
2 participants