Skip to content

Commit

Permalink
encoding/protojson: allow missing value for Any of type Empty
Browse files Browse the repository at this point in the history
Some other implementations do not send the value field when encoding
an Any representing a message of the Empty Well-Known-Type.

Fixes golang/protobuf#1620

Change-Id: I89bffd5f92656ba3ac462c0b6365ed4b49e6189d
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/618395
Reviewed-by: Damien Neil <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Michael Stapelberg <[email protected]>
  • Loading branch information
justinsb authored and stapelberg committed Oct 9, 2024
1 parent d340238 commit fb995f1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 3 additions & 1 deletion encoding/protojson/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2203,7 +2203,9 @@ func TestUnmarshal(t *testing.T) {
inputText: `{
"@type": "type.googleapis.com/google.protobuf.Empty"
}`,
wantErr: `(line 3:1): missing "value" field`,
wantMessage: &anypb.Any{
TypeUrl: "type.googleapis.com/google.protobuf.Empty",
},
}, {
desc: "Any with StringValue containing invalid UTF8",
inputMessage: &anypb.Any{},
Expand Down
6 changes: 5 additions & 1 deletion encoding/protojson/well_known_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,11 @@ func (d decoder) unmarshalAnyValue(unmarshal unmarshalFunc, m protoreflect.Messa
switch tok.Kind() {
case json.ObjectClose:
if !found {
return d.newError(tok.Pos(), `missing "value" field`)
// We tolerate an omitted `value` field with the google.protobuf.Empty Well-Known-Type,
// for compatibility with other proto runtimes that have interpreted the spec differently.
if m.Descriptor().FullName() != genid.Empty_message_fullname {
return d.newError(tok.Pos(), `missing "value" field`)
}
}
return nil

Expand Down

0 comments on commit fb995f1

Please sign in to comment.