You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given the following test program, that attempt to marshal a struct containing a nil instance of encoding.TextMarshaler, json.Marshal panic. https://play.golang.org/p/ypv_Qj-QFOf
If we rewrite the program reproducing the panic to use the json.Marshaler interface instead, we observe that the panic no longer occurs. https://play.golang.org/p/cZVFIU7N216
The following line of the textMarshalerEncoder function should use the same check.
I am surprised that the TestNilMarshal test is limited to the json.Marshal interface, and that the marshalerEncoder and textMarshalerEncoder functions are not equivalent in behavior. I can't think of ant reasonable reason for this, so I can only guess this is an unfortunate mistake.
The original issue that reported the panic for the json.Marshaler interface is #16042.
@mvdan Found it the same way as the issue in #33675, by writing some tests for my custom JSON encoder. I guess there's good sides on rewriting the wheel.
I'll send a CL asap.
Edit: What do you think of duplicating the test cases of TestNilMarshaler for encoding.TextMarshaler while we're at it ?
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Given the following test program, that attempt to marshal a struct containing a
nil
instance ofencoding.TextMarshaler
,json.Marshal
panic.https://play.golang.org/p/ypv_Qj-QFOf
What did you expect to see?
The foollowing JSON string:
{"M":null}
What did you see instead?
Notes
The program reproducing the panic is an adaptation of an existing testcase of the
encoding/json
package, that can be found at the following line.go/src/encoding/json/encode_test.go
Line 480 in 23f7398
The
marshalerEncoder
function checks that the following type assertion succeeds, or writenull
.go/src/encoding/json/encode.go
Line 447 in 23f7398
If we rewrite the program reproducing the panic to use the
json.Marshaler
interface instead, we observe that the panic no longer occurs.https://play.golang.org/p/cZVFIU7N216
The following line of the
textMarshalerEncoder
function should use the same check.go/src/encoding/json/encode.go
Line 484 in 23f7398
I am surprised that the
TestNilMarshal
test is limited to thejson.Marshal
interface, and that themarshalerEncoder
andtextMarshalerEncoder
functions are not equivalent in behavior. I can't think of ant reasonable reason for this, so I can only guess this is an unfortunate mistake.The original issue that reported the panic for the
json.Marshaler
interface is #16042./cc @mvdan
The text was updated successfully, but these errors were encountered: