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
Add an EmitUnpopulatedPrimitiveFields
option
#1536
Comments
In response to your second part, this seems to be a divergence in implementing the standard. The standard provides:
It seems that CPP has presumed that this should only apply to primitive values, while Golang has elected to have it apply to all fields. I would argue the Golang implementation is more true to the standard as phrased. The two representations should however be semantically identical, that is, rendering into a
I’m not sure how useful it would be to offer a second marshalling option that is almost essentially the same as But since this is a disagreement between two implementations of the protobuf standard, it might be helpful to the main protobuf isuses board and seek discussion about the resolution there. As is, the way I see it, the Golang implementation is within standard, and the CPP implementation is out-of-spec, but also as noted: neither is incompatible, and both should be semantically identical. |
Since message, map, and repeated fields don't have default values, I think you could interpret "default values" in the proto3 JSON specification as referring to the zero values of primitive fields such as int. A |
Are you sure that message and repeated fields do not have default values?
And through transitive properties, maps should as well:
|
I'm not talking about maps and repeated fields - I actually do want to emit them. Actually from this:
The exact value is language-dependent, but is not set. To be honest there is a way to get what I want but I think it pollutes the .proto file and it is to set the message as
This is how my journey began, and to my surprise, when looking at responses from Google APIs, I noticed that |
Notice that there are 3 states:
Obviously I agree that 3 is not even an option here, it's a totally different message. |
While there are three versions of outputs possible, it is also true that both state 1, and state 2 that you present should compose into semantically the same data-structure in Go. That is, it should be |
I agree, so I'm not sure if I convinced you that it's fine to add such option or not :-) (Of course naming is pending, can also have the flag be an additional flag to |
🤷♀️ I suppose the only thing left is to make a Gerrit change request. |
Ok, I'll create one |
Introduce the EmitDefaultValues in addition to the existing EmitUnpopulated option. EmitDefaultValues is added to emit json messages more compatible with the `always_print_primitive_fields` option of the cpp protobuf library. EmitUnpopulated overrides EmitDefaultValues since the former generates a strict superset of the latter. See descussion: golang/protobuf#1536 Change-Id: Ib29b69d630fa3e8d8fdeb0de43b5683f30152151 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/521215 Reviewed-by: Damien Neil <dneil@google.com> Reviewed-by: Cassondra Foesch <cfoesch@gmail.com> Reviewed-by: Lasse Folger <lassefolger@google.com>
The problem I am facing:
I have a gRPC-protobuf service which follows Google's resource oriented design (https://cloud.google.com/apis/design/resources).
In some resources I want to be able to specify sub-messages that are INPUT_ONLY:
I want to
params
to be specified on input, but when I invokeGet
/List
endpoints on that resource, I want toparams
to be omitted.In addition I want to support GRPC-transcoding to JSON, both with envoy (cpp) and with grpc-gateway (Golang)
When I look at Google's APIs I notice the following behavior in transcoding: empty strings, zero numbers, false booleans are a part of the response, but empty sub-messages are not.
So for the given example with the following input:
id := ""
params := null
I get the following json:
And I suspect its due to the following flag in the cpp json transcoding in envoy:
Which always emits primitive fields, but does not emit empty messages.
When I transcode using
grpc-gateway
which usesprotojson
I get:And I assume this is due to the
EmitUnpopulated
option inprotojson
:which is set to
true
.If I set it to
false
I would getSo it seems that it is not possible to have the same json transcoding in cpp and Golang in regards to "unpopulated/default" field values.
Adding EmitUnpopulatedPrimitiveFields might give the option to have the same transcoding to other tools like grpc-gateway
The text was updated successfully, but these errors were encountered: