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

Codes fail on round trip marshal/unmarshal #1273

Closed
dackroyd opened this issue Oct 23, 2020 · 0 comments · Fixed by #1276
Closed

Codes fail on round trip marshal/unmarshal #1273

dackroyd opened this issue Oct 23, 2020 · 0 comments · Fixed by #1276
Labels
bug Something isn't working help wanted Extra attention is needed
Milestone

Comments

@dackroyd
Copy link

dackroyd commented Oct 23, 2020

Unmarshalling a codes.Code value that was previously marshalled to JSON fails.

Test case reproducing the issue:

func TestRoundTripCodes(t *testing.T) {
	tests := []struct {
		input  codes.Code
	}{
		{codes.Unset},
		{codes.Error},
		{codes.Ok},
	}
	for _, test := range tests {
		c := test.input
		out := new(codes.Code)

		b, err := c.MarshalJSON()
		if err != nil {
			t.Fatalf("Code(%s).MarshalJSON() errored: %v", test.input, err)
		}

		if err := out.UnmarshalJSON(b); err != nil {
			t.Fatalf("Code.UnmarshalJSON(%q) errored: %v", c, err)
		}

		if *out != test.input {
			t.Errorf("failed to round trip %q, output was %v", test.input, out)
		}
	}
}

Fails with (all codes fail, not just Unset):

=== RUN   TestRoundTripCodes
    TestRoundTripCodes: roundtrip_codes_test.go:27: Code.UnmarshalJSON("Unset") errored: invalid code: "\"Unset\""
--- FAIL: TestRoundTripCodes (0.00s)

Unmarshal/marshal should round trip successfully, but the wrapping quotes in the incoming bytes when performing an unmarshal aren't expected by the mapping code, so it fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants