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

Fix unmarshal null to existing value #407

Merged
merged 1 commit into from
Dec 21, 2024

Conversation

neal
Copy link
Contributor

@neal neal commented Dec 19, 2024

Unmarshaling null value on an existing field does not set it to nil.

The generated unmarshal code today adds this block of code that skips the field if it is null:

easyjson/gen/decoder.go

Lines 517 to 521 in 853c497

fmt.Fprintln(g.out, " if in.IsNull() {")
fmt.Fprintln(g.out, " in.Skip()")
fmt.Fprintln(g.out, " in.WantComma()")
fmt.Fprintln(g.out, " continue")
fmt.Fprintln(g.out, " }")

This means the field is skipped and does not get set to nil.

However, there is already an if in.IsNull() check on the individual field that sets the field to nil as expected:

easyjson/gen/decoder.go

Lines 234 to 240 in 529b1f6

fmt.Fprintln(g.out, ws+"if in.IsNull() {")
fmt.Fprintln(g.out, ws+" in.Skip()")
fmt.Fprintln(g.out, ws+" "+out+" = nil")
fmt.Fprintln(g.out, ws+"} else {")
fmt.Fprintln(g.out, ws+" if "+out+" == nil {")
fmt.Fprintln(g.out, ws+" "+out+" = new("+g.getType(t.Elem())+")")
fmt.Fprintln(g.out, ws+" }")

So removing the first check fixes this issue.

The added test fails today because it does not overwrite the Ptr field to nil, which is the stdlib behavior as shown here: https://goplay.tools/snippet/8Y3_1RK1C3u

As far as I can tell, the removed code should not affect anything else as the check is handled correctly after anyways, but please let me know if I am missing anything and how we can achieve the same behavior as stdlib.

@rvasily rvasily merged commit 8580601 into mailru:master Dec 21, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants