Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Invalid JSON is generated with gogo/protobuf's jsonpb.Marshaler #216

Closed
liranp opened this issue Oct 17, 2016 · 2 comments
Closed

Invalid JSON is generated with gogo/protobuf's jsonpb.Marshaler #216

liranp opened this issue Oct 17, 2016 · 2 comments

Comments

@liranp
Copy link

liranp commented Oct 17, 2016

In continue to issue #212, I'm opening a new issue cause they're not necessarily related to each other.

I have changed my gRPC gateway server to use gogo/protobuf's jsonpb.Marshaler instead the one from golang/protobuf. I'm having another issue now; since the change, all JSON messages are marshaled by the jsonpb.Marshaler, but it uses the json field from the protobuf tag instead of the json tag (even if I'm using [(gogoproto.jsontag) = "..."]; which just overrides the json tag if I understood correctly).

Let's say I have the following message in my .proto file:

message Value {
    string foo_bar = 1;
}

In the .pb.go this translated to:

type Value struct {
    FooBar    string     `protobuf:"bytes,2,opt,name=foo_bar,json=fooBar,proto3" json:"foo_bar"`
}

So, the JSON looks like:

{
    "value": {
        "fooBar": "baz"
    }
}

While (at least for my understanding) the expected result is (like it worked before):

{
    "value": {
        "foo_bar": "baz"
    }
}

What am I missing?
Any help will be much appreciated!

@liranp
Copy link
Author

liranp commented Oct 17, 2016

I've found the answer, but perhaps we should keep this issue as closed for documentation purposes.

If no WithMarshalerOption is provided to the grpc-gateway's NewServeMux function, it uses a default marshaler which is the jsopb.Marshaler from golang/protobuf, and it instantiates it like this:

defaultMarshaler = &JSONPb{OrigName: true}

So, the solution was just to set OrigName to true as well.

@awalterschulze
Copy link
Member

Thanks for providing the answer, this is great :)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants