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

json tags not set for oneof fields #140

Closed
tyleradams opened this issue Mar 2, 2016 · 2 comments
Closed

json tags not set for oneof fields #140

tyleradams opened this issue Mar 2, 2016 · 2 comments

Comments

@tyleradams
Copy link

A struct like

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.

@dsymonds
Copy link
Contributor

dsymonds commented Mar 2, 2016

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.

@dsymonds dsymonds closed this as completed Mar 2, 2016
@marwan-at-work
Copy link

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?

Thanks

@golang golang locked and limited conversation to collaborators Jun 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants