encoding/jsonpb: fix unmarshaling of NullValue field

A JSON "null" field should set the NullValue enum field because
NullValue has the custom encoding format of "null".

Change-Id: I2bfa0900de64d7e2874f7c6db04b1cbc0b61b904
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/170107
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/encoding/jsonpb/decode.go b/encoding/jsonpb/decode.go
index 8511a8d..7655054 100644
--- a/encoding/jsonpb/decode.go
+++ b/encoding/jsonpb/decode.go
@@ -225,8 +225,8 @@
 		seenNums.Set(num)
 
 		// No need to set values for JSON null unless the field type is
-		// google.protobuf.Value.
-		if o.decoder.Peek() == json.Null && !isKnownValue(fd) {
+		// google.protobuf.Value or google.protobuf.NullValue.
+		if o.decoder.Peek() == json.Null && !isKnownValue(fd) && !isNullValue(fd) {
 			o.decoder.Read()
 			continue
 		}
@@ -277,6 +277,16 @@
 	return nil, protoregistry.NotFound
 }
 
+func isKnownValue(fd pref.FieldDescriptor) bool {
+	md := fd.MessageType()
+	return md != nil && md.FullName() == "google.protobuf.Value"
+}
+
+func isNullValue(fd pref.FieldDescriptor) bool {
+	ed := fd.EnumType()
+	return ed != nil && ed.FullName() == "google.protobuf.NullValue"
+}
+
 // unmarshalSingular unmarshals to the non-repeated field specified by the given
 // FieldDescriptor.
 func (o UnmarshalOptions) unmarshalSingular(knownFields pref.KnownFields, fd pref.FieldDescriptor) error {
@@ -509,6 +519,12 @@
 			return pref.Value{}, err
 		}
 		return pref.ValueOf(pref.EnumNumber(n)), nil
+
+	case json.Null:
+		// This is only valid for google.protobuf.NullValue.
+		if isNullValue(fd) {
+			return pref.ValueOf(pref.EnumNumber(0)), nil
+		}
 	}
 
 	return pref.Value{}, unexpectedJSONError{jval}
diff --git a/encoding/jsonpb/decode_test.go b/encoding/jsonpb/decode_test.go
index 1e9bd98..e7f4239 100644
--- a/encoding/jsonpb/decode_test.go
+++ b/encoding/jsonpb/decode_test.go
@@ -439,24 +439,31 @@
 		desc:         "enum set to number string",
 		inputMessage: &pb3.Enums{},
 		inputText: `{
-  "sEnum": "1",
+  "sEnum": "1"
 }`,
 		wantErr: true,
 	}, {
 		desc:         "enum set to invalid named",
 		inputMessage: &pb3.Enums{},
 		inputText: `{
-  "sEnum": "UNNAMED",
+  "sEnum": "UNNAMED"
 }`,
 		wantErr: true,
 	}, {
 		desc:         "enum set to not enum",
 		inputMessage: &pb3.Enums{},
 		inputText: `{
-  "sEnum": true,
+  "sEnum": true
 }`,
 		wantErr: true,
 	}, {
+		desc:         "enum set to JSON null",
+		inputMessage: &pb3.Enums{},
+		inputText: `{
+  "sEnum": null
+}`,
+		wantMessage: &pb3.Enums{},
+	}, {
 		desc:         "proto name",
 		inputMessage: &pb3.JSONNames{},
 		inputText: `{
@@ -1478,6 +1485,20 @@
 		},
 		wantErr: true,
 	}, {
+		desc:         "NullValue field with JSON null",
+		inputMessage: &pb2.KnownTypes{},
+		inputText: `{
+  "optNull": null
+}`,
+		wantMessage: &pb2.KnownTypes{OptNull: new(knownpb.NullValue)},
+	}, {
+		desc:         "NullValue field with string",
+		inputMessage: &pb2.KnownTypes{},
+		inputText: `{
+  "optNull": "NULL_VALUE"
+}`,
+		wantMessage: &pb2.KnownTypes{OptNull: new(knownpb.NullValue)},
+	}, {
 		desc:         "BytesValue",
 		inputMessage: &knownpb.BytesValue{},
 		inputText:    `"aGVsbG8="`,
diff --git a/encoding/jsonpb/encode_test.go b/encoding/jsonpb/encode_test.go
index 005de06..96e21f2 100644
--- a/encoding/jsonpb/encode_test.go
+++ b/encoding/jsonpb/encode_test.go
@@ -1177,6 +1177,12 @@
 		input: &knownpb.Empty{},
 		want:  `{}`,
 	}, {
+		desc:  "NullValue field",
+		input: &pb2.KnownTypes{OptNull: new(knownpb.NullValue)},
+		want: `{
+  "optNull": null
+}`,
+	}, {
 		desc:    "Value empty",
 		input:   &knownpb.Value{},
 		wantErr: true,
diff --git a/encoding/jsonpb/well_known_types.go b/encoding/jsonpb/well_known_types.go
index db1e365..788e314 100644
--- a/encoding/jsonpb/well_known_types.go
+++ b/encoding/jsonpb/well_known_types.go
@@ -547,11 +547,6 @@
 	return errors.New("%s: none of the variants is set", msgType.FullName())
 }
 
-func isKnownValue(fd pref.FieldDescriptor) bool {
-	md := fd.MessageType()
-	return md != nil && md.FullName() == "google.protobuf.Value"
-}
-
 func (o UnmarshalOptions) unmarshalKnownValue(m pref.Message) error {
 	var nerr errors.NonFatal
 	knownFields := m.KnownFields()
diff --git a/encoding/testprotos/pb2/test.pb.go b/encoding/testprotos/pb2/test.pb.go
index 5a1b5b2..0ad7a9a 100644
--- a/encoding/testprotos/pb2/test.pb.go
+++ b/encoding/testprotos/pb2/test.pb.go
@@ -990,6 +990,7 @@
 	OptStruct            *known.Struct      `protobuf:"bytes,25,opt,name=opt_struct,json=optStruct" json:"opt_struct,omitempty"`
 	OptList              *known.ListValue   `protobuf:"bytes,26,opt,name=opt_list,json=optList" json:"opt_list,omitempty"`
 	OptValue             *known.Value       `protobuf:"bytes,27,opt,name=opt_value,json=optValue" json:"opt_value,omitempty"`
+	OptNull              *known.NullValue   `protobuf:"varint,28,opt,name=opt_null,json=optNull,enum=google.protobuf.NullValue" json:"opt_null,omitempty"`
 	OptEmpty             *known.Empty       `protobuf:"bytes,30,opt,name=opt_empty,json=optEmpty" json:"opt_empty,omitempty"`
 	OptAny               *known.Any         `protobuf:"bytes,32,opt,name=opt_any,json=optAny" json:"opt_any,omitempty"`
 	OptFieldmask         *known.FieldMask   `protobuf:"bytes,40,opt,name=opt_fieldmask,json=optFieldmask" json:"opt_fieldmask,omitempty"`
@@ -1115,6 +1116,13 @@
 	return nil
 }
 
+func (x *KnownTypes) GetOptNull() known.NullValue {
+	if x != nil && x.OptNull != nil {
+		return *x.OptNull
+	}
+	return known.NullValue_NULL_VALUE
+}
+
 func (x *KnownTypes) GetOptEmpty() *known.Empty {
 	if x != nil {
 		return x.OptEmpty
@@ -1691,7 +1699,7 @@
 	0x18, 0x0a, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1c, 0x2e, 0x70, 0x62, 0x32, 0x2e, 0x46, 0x61, 0x6b,
 	0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x53, 0x65, 0x74, 0x45, 0x78, 0x74, 0x65, 0x6e,
 	0x73, 0x69, 0x6f, 0x6e, 0x52, 0x13, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x53, 0x65, 0x74,
-	0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x22, 0xe7, 0x07, 0x0a, 0x0a, 0x4b, 0x6e,
+	0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x22, 0x9e, 0x08, 0x0a, 0x0a, 0x4b, 0x6e,
 	0x6f, 0x77, 0x6e, 0x54, 0x79, 0x70, 0x65, 0x73, 0x12, 0x35, 0x0a, 0x08, 0x6f, 0x70, 0x74, 0x5f,
 	0x62, 0x6f, 0x6f, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f,
 	0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x42, 0x6f, 0x6f,
@@ -1743,56 +1751,59 @@
 	0x52, 0x07, 0x6f, 0x70, 0x74, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x33, 0x0a, 0x09, 0x6f, 0x70, 0x74,
 	0x5f, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x1b, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x67,
 	0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x56,
-	0x61, 0x6c, 0x75, 0x65, 0x52, 0x08, 0x6f, 0x70, 0x74, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x33,
-	0x0a, 0x09, 0x6f, 0x70, 0x74, 0x5f, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x18, 0x1e, 0x20, 0x01, 0x28,
-	0x0b, 0x32, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
-	0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x52, 0x08, 0x6f, 0x70, 0x74, 0x45, 0x6d,
-	0x70, 0x74, 0x79, 0x12, 0x2d, 0x0a, 0x07, 0x6f, 0x70, 0x74, 0x5f, 0x61, 0x6e, 0x79, 0x18, 0x20,
-	0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72,
-	0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x41, 0x6e, 0x79, 0x52, 0x06, 0x6f, 0x70, 0x74, 0x41,
-	0x6e, 0x79, 0x12, 0x3f, 0x0a, 0x0d, 0x6f, 0x70, 0x74, 0x5f, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x6d,
-	0x61, 0x73, 0x6b, 0x18, 0x28, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67,
-	0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c,
-	0x64, 0x4d, 0x61, 0x73, 0x6b, 0x52, 0x0c, 0x6f, 0x70, 0x74, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x6d,
-	0x61, 0x73, 0x6b, 0x2a, 0x21, 0x0a, 0x04, 0x45, 0x6e, 0x75, 0x6d, 0x12, 0x07, 0x0a, 0x03, 0x4f,
-	0x4e, 0x45, 0x10, 0x01, 0x12, 0x07, 0x0a, 0x03, 0x54, 0x57, 0x4f, 0x10, 0x02, 0x12, 0x07, 0x0a,
-	0x03, 0x54, 0x45, 0x4e, 0x10, 0x0a, 0x3a, 0x31, 0x0a, 0x0c, 0x6f, 0x70, 0x74, 0x5f, 0x65, 0x78,
-	0x74, 0x5f, 0x62, 0x6f, 0x6f, 0x6c, 0x12, 0x0f, 0x2e, 0x70, 0x62, 0x32, 0x2e, 0x45, 0x78, 0x74,
-	0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x15, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0a, 0x6f,
-	0x70, 0x74, 0x45, 0x78, 0x74, 0x42, 0x6f, 0x6f, 0x6c, 0x3a, 0x35, 0x0a, 0x0e, 0x6f, 0x70, 0x74,
-	0x5f, 0x65, 0x78, 0x74, 0x5f, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x12, 0x0f, 0x2e, 0x70, 0x62,
-	0x32, 0x2e, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x16, 0x20, 0x01,
-	0x28, 0x09, 0x52, 0x0c, 0x6f, 0x70, 0x74, 0x45, 0x78, 0x74, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67,
-	0x3a, 0x3c, 0x0a, 0x0c, 0x6f, 0x70, 0x74, 0x5f, 0x65, 0x78, 0x74, 0x5f, 0x65, 0x6e, 0x75, 0x6d,
-	0x12, 0x0f, 0x2e, 0x70, 0x62, 0x32, 0x2e, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e,
-	0x73, 0x18, 0x17, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x09, 0x2e, 0x70, 0x62, 0x32, 0x2e, 0x45, 0x6e,
-	0x75, 0x6d, 0x52, 0x0a, 0x6f, 0x70, 0x74, 0x45, 0x78, 0x74, 0x45, 0x6e, 0x75, 0x6d, 0x3a, 0x42,
-	0x0a, 0x0e, 0x6f, 0x70, 0x74, 0x5f, 0x65, 0x78, 0x74, 0x5f, 0x6e, 0x65, 0x73, 0x74, 0x65, 0x64,
-	0x12, 0x0f, 0x2e, 0x70, 0x62, 0x32, 0x2e, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e,
-	0x73, 0x18, 0x18, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x70, 0x62, 0x32, 0x2e, 0x4e, 0x65,
-	0x73, 0x74, 0x65, 0x64, 0x52, 0x0c, 0x6f, 0x70, 0x74, 0x45, 0x78, 0x74, 0x4e, 0x65, 0x73, 0x74,
-	0x65, 0x64, 0x3a, 0x37, 0x0a, 0x0f, 0x72, 0x70, 0x74, 0x5f, 0x65, 0x78, 0x74, 0x5f, 0x66, 0x69,
-	0x78, 0x65, 0x64, 0x33, 0x32, 0x12, 0x0f, 0x2e, 0x70, 0x62, 0x32, 0x2e, 0x45, 0x78, 0x74, 0x65,
-	0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x1f, 0x20, 0x03, 0x28, 0x07, 0x52, 0x0d, 0x72, 0x70,
-	0x74, 0x45, 0x78, 0x74, 0x46, 0x69, 0x78, 0x65, 0x64, 0x33, 0x32, 0x3a, 0x3c, 0x0a, 0x0c, 0x72,
-	0x70, 0x74, 0x5f, 0x65, 0x78, 0x74, 0x5f, 0x65, 0x6e, 0x75, 0x6d, 0x12, 0x0f, 0x2e, 0x70, 0x62,
-	0x32, 0x2e, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x20, 0x20, 0x03,
-	0x28, 0x0e, 0x32, 0x09, 0x2e, 0x70, 0x62, 0x32, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x52, 0x0a, 0x72,
-	0x70, 0x74, 0x45, 0x78, 0x74, 0x45, 0x6e, 0x75, 0x6d, 0x3a, 0x42, 0x0a, 0x0e, 0x72, 0x70, 0x74,
-	0x5f, 0x65, 0x78, 0x74, 0x5f, 0x6e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x12, 0x0f, 0x2e, 0x70, 0x62,
-	0x32, 0x2e, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x21, 0x20, 0x03,
-	0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x70, 0x62, 0x32, 0x2e, 0x4e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x52,
-	0x0c, 0x72, 0x70, 0x74, 0x45, 0x78, 0x74, 0x4e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x3a, 0x61, 0x0a,
-	0x15, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x5f, 0x73, 0x65, 0x74, 0x5f, 0x65, 0x78, 0x74,
-	0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x0f, 0x2e, 0x70, 0x62, 0x32, 0x2e, 0x4d, 0x65, 0x73,
-	0x73, 0x61, 0x67, 0x65, 0x53, 0x65, 0x74, 0x18, 0x32, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1c, 0x2e,
-	0x70, 0x62, 0x32, 0x2e, 0x46, 0x61, 0x6b, 0x65, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x53,
-	0x65, 0x74, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x13, 0x6d, 0x65, 0x73,
-	0x73, 0x61, 0x67, 0x65, 0x53, 0x65, 0x74, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e,
-	0x42, 0x37, 0x5a, 0x35, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67,
-	0x6f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x76,
-	0x32, 0x2f, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x70,
-	0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2f, 0x70, 0x62, 0x32,
+	0x61, 0x6c, 0x75, 0x65, 0x52, 0x08, 0x6f, 0x70, 0x74, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x35,
+	0x0a, 0x08, 0x6f, 0x70, 0x74, 0x5f, 0x6e, 0x75, 0x6c, 0x6c, 0x18, 0x1c, 0x20, 0x01, 0x28, 0x0e,
+	0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62,
+	0x75, 0x66, 0x2e, 0x4e, 0x75, 0x6c, 0x6c, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x52, 0x07, 0x6f, 0x70,
+	0x74, 0x4e, 0x75, 0x6c, 0x6c, 0x12, 0x33, 0x0a, 0x09, 0x6f, 0x70, 0x74, 0x5f, 0x65, 0x6d, 0x70,
+	0x74, 0x79, 0x18, 0x1e, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c,
+	0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79,
+	0x52, 0x08, 0x6f, 0x70, 0x74, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x12, 0x2d, 0x0a, 0x07, 0x6f, 0x70,
+	0x74, 0x5f, 0x61, 0x6e, 0x79, 0x18, 0x20, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x67, 0x6f,
+	0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x41, 0x6e,
+	0x79, 0x52, 0x06, 0x6f, 0x70, 0x74, 0x41, 0x6e, 0x79, 0x12, 0x3f, 0x0a, 0x0d, 0x6f, 0x70, 0x74,
+	0x5f, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x6d, 0x61, 0x73, 0x6b, 0x18, 0x28, 0x20, 0x01, 0x28, 0x0b,
+	0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62,
+	0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x4d, 0x61, 0x73, 0x6b, 0x52, 0x0c, 0x6f, 0x70,
+	0x74, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x6d, 0x61, 0x73, 0x6b, 0x2a, 0x21, 0x0a, 0x04, 0x45, 0x6e,
+	0x75, 0x6d, 0x12, 0x07, 0x0a, 0x03, 0x4f, 0x4e, 0x45, 0x10, 0x01, 0x12, 0x07, 0x0a, 0x03, 0x54,
+	0x57, 0x4f, 0x10, 0x02, 0x12, 0x07, 0x0a, 0x03, 0x54, 0x45, 0x4e, 0x10, 0x0a, 0x3a, 0x31, 0x0a,
+	0x0c, 0x6f, 0x70, 0x74, 0x5f, 0x65, 0x78, 0x74, 0x5f, 0x62, 0x6f, 0x6f, 0x6c, 0x12, 0x0f, 0x2e,
+	0x70, 0x62, 0x32, 0x2e, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x15,
+	0x20, 0x01, 0x28, 0x08, 0x52, 0x0a, 0x6f, 0x70, 0x74, 0x45, 0x78, 0x74, 0x42, 0x6f, 0x6f, 0x6c,
+	0x3a, 0x35, 0x0a, 0x0e, 0x6f, 0x70, 0x74, 0x5f, 0x65, 0x78, 0x74, 0x5f, 0x73, 0x74, 0x72, 0x69,
+	0x6e, 0x67, 0x12, 0x0f, 0x2e, 0x70, 0x62, 0x32, 0x2e, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69,
+	0x6f, 0x6e, 0x73, 0x18, 0x16, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x6f, 0x70, 0x74, 0x45, 0x78,
+	0x74, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x3a, 0x3c, 0x0a, 0x0c, 0x6f, 0x70, 0x74, 0x5f, 0x65,
+	0x78, 0x74, 0x5f, 0x65, 0x6e, 0x75, 0x6d, 0x12, 0x0f, 0x2e, 0x70, 0x62, 0x32, 0x2e, 0x45, 0x78,
+	0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x17, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x09,
+	0x2e, 0x70, 0x62, 0x32, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x52, 0x0a, 0x6f, 0x70, 0x74, 0x45, 0x78,
+	0x74, 0x45, 0x6e, 0x75, 0x6d, 0x3a, 0x42, 0x0a, 0x0e, 0x6f, 0x70, 0x74, 0x5f, 0x65, 0x78, 0x74,
+	0x5f, 0x6e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x12, 0x0f, 0x2e, 0x70, 0x62, 0x32, 0x2e, 0x45, 0x78,
+	0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x18, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b,
+	0x2e, 0x70, 0x62, 0x32, 0x2e, 0x4e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x52, 0x0c, 0x6f, 0x70, 0x74,
+	0x45, 0x78, 0x74, 0x4e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x3a, 0x37, 0x0a, 0x0f, 0x72, 0x70, 0x74,
+	0x5f, 0x65, 0x78, 0x74, 0x5f, 0x66, 0x69, 0x78, 0x65, 0x64, 0x33, 0x32, 0x12, 0x0f, 0x2e, 0x70,
+	0x62, 0x32, 0x2e, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x1f, 0x20,
+	0x03, 0x28, 0x07, 0x52, 0x0d, 0x72, 0x70, 0x74, 0x45, 0x78, 0x74, 0x46, 0x69, 0x78, 0x65, 0x64,
+	0x33, 0x32, 0x3a, 0x3c, 0x0a, 0x0c, 0x72, 0x70, 0x74, 0x5f, 0x65, 0x78, 0x74, 0x5f, 0x65, 0x6e,
+	0x75, 0x6d, 0x12, 0x0f, 0x2e, 0x70, 0x62, 0x32, 0x2e, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69,
+	0x6f, 0x6e, 0x73, 0x18, 0x20, 0x20, 0x03, 0x28, 0x0e, 0x32, 0x09, 0x2e, 0x70, 0x62, 0x32, 0x2e,
+	0x45, 0x6e, 0x75, 0x6d, 0x52, 0x0a, 0x72, 0x70, 0x74, 0x45, 0x78, 0x74, 0x45, 0x6e, 0x75, 0x6d,
+	0x3a, 0x42, 0x0a, 0x0e, 0x72, 0x70, 0x74, 0x5f, 0x65, 0x78, 0x74, 0x5f, 0x6e, 0x65, 0x73, 0x74,
+	0x65, 0x64, 0x12, 0x0f, 0x2e, 0x70, 0x62, 0x32, 0x2e, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69,
+	0x6f, 0x6e, 0x73, 0x18, 0x21, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x70, 0x62, 0x32, 0x2e,
+	0x4e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x52, 0x0c, 0x72, 0x70, 0x74, 0x45, 0x78, 0x74, 0x4e, 0x65,
+	0x73, 0x74, 0x65, 0x64, 0x3a, 0x61, 0x0a, 0x15, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x5f,
+	0x73, 0x65, 0x74, 0x5f, 0x65, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x0f, 0x2e,
+	0x70, 0x62, 0x32, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x53, 0x65, 0x74, 0x18, 0x32,
+	0x20, 0x01, 0x28, 0x0b, 0x32, 0x1c, 0x2e, 0x70, 0x62, 0x32, 0x2e, 0x46, 0x61, 0x6b, 0x65, 0x4d,
+	0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x53, 0x65, 0x74, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69,
+	0x6f, 0x6e, 0x52, 0x13, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x53, 0x65, 0x74, 0x45, 0x78,
+	0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x42, 0x37, 0x5a, 0x35, 0x67, 0x69, 0x74, 0x68, 0x75,
+	0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x6f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x70, 0x72, 0x6f,
+	0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x76, 0x32, 0x2f, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e,
+	0x67, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x2f, 0x70, 0x62, 0x32,
 }
 
 var (
@@ -1846,9 +1857,10 @@
 	(*known.Struct)(nil),                  // 33: google.protobuf.Struct
 	(*known.ListValue)(nil),               // 34: google.protobuf.ListValue
 	(*known.Value)(nil),                   // 35: google.protobuf.Value
-	(*known.Empty)(nil),                   // 36: google.protobuf.Empty
-	(*known.Any)(nil),                     // 37: google.protobuf.Any
-	(*known.FieldMask)(nil),               // 38: google.protobuf.FieldMask
+	(known.NullValue)(0),                  // 36: google.protobuf.NullValue
+	(*known.Empty)(nil),                   // 37: google.protobuf.Empty
+	(*known.Any)(nil),                     // 38: google.protobuf.Any
+	(*known.FieldMask)(nil),               // 39: google.protobuf.FieldMask
 }
 var xxx_File_pb2_test_proto_depIdxs = []int32{
 	11, // pb2.opt_ext_bool:extendee -> pb2.Extensions
@@ -1899,9 +1911,10 @@
 	33, // pb2.KnownTypes.opt_struct:type_name -> google.protobuf.Struct
 	34, // pb2.KnownTypes.opt_list:type_name -> google.protobuf.ListValue
 	35, // pb2.KnownTypes.opt_value:type_name -> google.protobuf.Value
-	36, // pb2.KnownTypes.opt_empty:type_name -> google.protobuf.Empty
-	37, // pb2.KnownTypes.opt_any:type_name -> google.protobuf.Any
-	38, // pb2.KnownTypes.opt_fieldmask:type_name -> google.protobuf.FieldMask
+	36, // pb2.KnownTypes.opt_null:type_name -> google.protobuf.NullValue
+	37, // pb2.KnownTypes.opt_empty:type_name -> google.protobuf.Empty
+	38, // pb2.KnownTypes.opt_any:type_name -> google.protobuf.Any
+	39, // pb2.KnownTypes.opt_fieldmask:type_name -> google.protobuf.FieldMask
 	5,  // pb2.Nests.OptGroup.opt_nested:type_name -> pb2.Nested
 	20, // pb2.Nests.OptGroup.optnestedgroup:type_name -> pb2.Nests.OptGroup.OptNestedGroup
 	9,  // pb2.IndirectRequired.StrToNestedEntry.value:type_name -> pb2.NestedWithRequired
diff --git a/encoding/testprotos/pb2/test.proto b/encoding/testprotos/pb2/test.proto
index 1b4f8d0..3aa9f41 100644
--- a/encoding/testprotos/pb2/test.proto
+++ b/encoding/testprotos/pb2/test.proto
@@ -213,9 +213,12 @@
 
   optional google.protobuf.Duration opt_duration = 20;
   optional google.protobuf.Timestamp opt_timestamp = 21;
+
   optional google.protobuf.Struct opt_struct = 25;
   optional google.protobuf.ListValue opt_list = 26;
   optional google.protobuf.Value opt_value = 27;
+  optional google.protobuf.NullValue opt_null = 28;
+
   optional google.protobuf.Empty opt_empty = 30;
   optional google.protobuf.Any opt_any = 32;