easyjson icon indicating copy to clipboard operation
easyjson copied to clipboard

[{}] with omitempty incorrectly unmarshals into [nil] instead of [map]

Open creker opened this issue 5 years ago • 1 comments

Given this kind of structure

type s struct {
	m []map[string]string `json:",omitempty"`
}

and JSON {"m": [{}]}

Unmarshal produces this (slice with a nil map)

s {
    m: []map[string]string{map[string]string(nil)}
}

Instead of this (slice with a an empty map)

s {
    m: []map[string]string{map[string]string{}}
}

encoding/json produces the latter. It respects omitempty tag only for the fields themselves, not the nested values inside them. Looks like this bug was introduced in #260.

I looked at the code by I'm not really sure how to fix this properly. What I'm thinking is that fields tags shouldn't be passed to nested types at all. For example, here's a snipped for map types https://github.com/mailru/easyjson/blob/8edcc4e51f39ddbd3505a3386aff3f435a7fd028/gen/decoder.go#L273-L281 As you can see, tags are passed inside forcing the code to respect them even for types embedded into the map. Instead I suggest the following

if err := g.genTypeDecoder(elem, tmpVar, fieldTags{}, indent+2); err != nil {
    return err
}

The tests look fine, so I will submit a pull request for this.

creker avatar Feb 20 '20 14:02 creker

Fix reverted, need new fix and tests for opt types

// This message for me

GoWebProd avatar Apr 15 '20 07:04 GoWebProd