Skip to content

Commit 8b39a84

Browse files
committed
encoding/protojson: fix google.protobuf.Empty serialization
Go Protobuf was diverging from other Protobuf implementations. Worse: The other Protobuf implementations cannot decode what Go Protobuf encodes for a google.protobuf.Empty message. By now, the spec has been updated to explicitly spell out that google.protobuf.Empty is not considered special and should result in {"@type": "type.googleapis.com/google.protobuf.Empty"} without an extra `"value": {}`. The fixed encoding is decoded correctly with Go Protobuf v1.35.2 (released November 2024) or newer. Fixes golang/protobuf#1707 Fixes golang/protobuf#1620 For protocolbuffers/protobuf#5390 For protocolbuffers/protobuf#17099 For protocolbuffers/protobuf#24445 Change-Id: I54f4d842e7272743191ec274b8e7434ddb74203f Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/735120 Reviewed-by: Lasse Folger <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Cassondra Foesch <[email protected]>
1 parent 20262ed commit 8b39a84

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

encoding/protojson/encode_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,8 +1717,7 @@ func TestMarshal(t *testing.T) {
17171717
}
17181718
}(),
17191719
want: `{
1720-
"@type": "type.googleapis.com/google.protobuf.Empty",
1721-
"value": {}
1720+
"@type": "type.googleapis.com/google.protobuf.Empty"
17221721
}`,
17231722
}, {
17241723
desc: "Any with StringValue containing invalid UTF8",
@@ -1927,8 +1926,7 @@ func TestMarshal(t *testing.T) {
19271926
"optValue": "world",
19281927
"optEmpty": {},
19291928
"optAny": {
1930-
"@type": "google.protobuf.Empty",
1931-
"value": {}
1929+
"@type": "google.protobuf.Empty"
19321930
},
19331931
"optFieldmask": "fooBar,barFoo"
19341932
}`,

encoding/protojson/well_known_types.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ func wellKnownTypeMarshaler(name protoreflect.FullName) marshalFunc {
5252
case genid.FieldMask_message_name:
5353
return encoder.marshalFieldMask
5454
case genid.Empty_message_name:
55-
return encoder.marshalEmpty
55+
// The spec explicitly specifies that the Empty message
56+
// is not considered to have any special JSON mapping:
57+
// https://protobuf.dev/programming-guides/json/#any
58+
return nil
5659
}
5760
}
5861
return nil

0 commit comments

Comments
 (0)