protobuf
protobuf copied to clipboard
protojson: marshal fieldmask with "*" path
We are following AIP recommendations for our APIs. For update methods (https://google.aip.dev/134#request-message) it is recommended to support a special *-fieldmask for modelling PUT (default is PATCH).
In go this would look something like:
&fieldmaskpb.FieldMask{
Paths: []string{"*"},
},
However a message containing such *-fieldmasks are not possible to marshal with protojson, failing with:
proto: google.protobuf.FieldMask.paths contains invalid path: "*"
It is surprising that something that is fine to marshal with proto.Marshal can not be marshalled with protojson.Marshal. Is the validation in protojson overly strict wrt to this statement from the spec?
Fields name in each path are converted to/from lower-camel naming conventions. https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#json-encoding-of-field-masks
Well known types are weirdly inconsistent with regard to binary marshaling versus text-based marshaling because they were invented after binary marshaling already existed. This is not specific to Go, but a problem in the wider protobuf ecosystem.
There is fundamentally an inconsistency in documentation where https://google.aip.dev/134#request-message recommends a practice that https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/field_mask.proto forbids. Given that the latter is a more authoritative source, it seems that protojson is doing the right thing until the protobuf project amends the specification.
I’m not so sure. The field_mask.proto is silent about wildcards, while the former link sets it out as a MUST.