encoding/json: calls UnmarshalJSON with a nil receiver on anonymous fields #17877
Labels
FrozenDueToAge
NeedsDecision
Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go version go1.7.1 darwin/amd64
What operating system and processor architecture are you using (
go env
)?What did you do?
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
Custom
UnmarshalJSON
methods do not properly update an anonymous field on an outer struct when it is unmarshaled.https://play.golang.org/p/wkQhDFiO82
It works correctly if the outer struct's field is not anonymous. If the field is anonymous it only works if you initialize it first
x.SomeType = &SomeType{}
.https://play.golang.org/p/DYuygZrLgk
edit - also note this works fine if you do not have custom
UnmarshalJSON
https://play.golang.org/p/2HhHJ19wpQWhat did you expect to see?
The anonymous field should be set and have
Price: 99
like in the second example.What did you see instead?
The anonymous field is nil
I'm not sure if this counts as a bug or what underlying issue is causing it. But it is very confusing from a user's perspective and it seems like it might be a compiler issue(maybe?).
The text was updated successfully, but these errors were encountered: