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
message A {
optional string e = 3;
oneof b {
string c = 1;
string d = 2;
}
}
Generates the following go code
type A struct {
E *E `protobuf:"bytes,3,opt,name=e" json:"e,omitempty"`
// Types that are valid to be assigned to B
// * B_C
// * B_D
B isA_B `protobuf_oneof:"value"`
}
...
type struct {
C string `protobuf:"bytes,1,opt,name=c,oneof"`
}
type struct {
D string `protobuf:"bytes,2,opt,name=d,oneof"`
}
...
The oneof field structs should also have json go tags so they can serialize to JSON correctly.
The text was updated successfully, but these errors were encountered:
It's working correctly. The correct way to serialise a protocol buffer to JSON is to use the jsonpb package, which doesn't use the json struct field tags.
If that's the case, I'm not sure why there are json:"<fieldName>" tags in all generated Go struct fields except oneof?
It's also worth mentioning that many protobuf-generated Go structs are used in contexts where they are automatically serialized with encoding/json without the programmer having access to change that to jsonpb.
One way to solve it is to have protoc-gen-go implement the json.Marshaler interface and uses jsonpb under the hood to ensure that anyone using encoding/json would get the correct outcome. Would that work?
A struct like
Generates the following go code
The oneof field structs should also have json go tags so they can serialize to JSON correctly.
The text was updated successfully, but these errors were encountered: