mailru / easyjson

Fast JSON serializer for golang.
MIT License
4.48k stars 421 forks source link

Generated code skips fields in an anonymous embedded struct #337

Open autarch opened 3 years ago

autarch commented 3 years ago
package x

type Foo struct {
    Name string `json:"name"`
}

type Bar struct {
    Stuff struct {
        Foo
        Size int `json:"size"`
    }
    Color string `json:"color"`
}
package main

import (
    "fmt"

    "github.com/autarch/ej/x"
    "github.com/mailru/easyjson"
)

func main() {
    b := x.Bar{
        Color: "blue",
    }
    b.Stuff.Foo = x.Foo{Name: "Joe"}
    b.Stuff.Size = 42

    out, _ := easyjson.Marshal(b)
    fmt.Println(string(out))
}

The generated marshaller for the Bar struct is broken. It will not serialize the Size field, only the embedded Foo.Name.

If the Bar.Stuff field anonymous struct is turned into a named struct, it is serialized correctly.

autarch commented 3 years ago

I did some more digging. This only happens with the -all flag. I will have a PR up for this soon.