encoding/protojson: allow missing value for Any of type Empty

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 <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Stapelberg <stapelberg@google.com>
This commit is contained in:
justinsb 2024-10-02 18:40:58 -04:00 committed by Michael Stapelberg
parent d34023800d
commit fb995f184a
2 changed files with 8 additions and 2 deletions

View File

@ -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{},

View File

@ -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