diff --git a/python/google/protobuf/internal/json_format_test.py b/python/google/protobuf/internal/json_format_test.py index db34c93e19..9112f5e240 100644 --- a/python/google/protobuf/internal/json_format_test.py +++ b/python/google/protobuf/internal/json_format_test.py @@ -200,6 +200,16 @@ class JsonFormatTest(JsonFormatBase): json_format.ParseDict(message_dict, parsed_message) self.assertEqual(message, parsed_message) + def testJsonParseDictToAnyDoesNotAlterInput(self): + orig_dict = { + "int32Value": 20, + "@type": "type.googleapis.com/proto3.TestMessage" + } + copied_dict = json.loads(json.dumps(orig_dict)) + parsed_message = any_pb2.Any() + json_format.ParseDict(copied_dict, parsed_message) + self.assertEqual(copied_dict, orig_dict) + def testExtensionSerializationDictMatchesProto3Spec(self): """See go/proto3-json-spec for spec. """ diff --git a/python/google/protobuf/json_format.py b/python/google/protobuf/json_format.py index e466f55612..3f2c1bfbe2 100644 --- a/python/google/protobuf/json_format.py +++ b/python/google/protobuf/json_format.py @@ -573,6 +573,7 @@ class _Parser(object): else: del value['@type'] self._ConvertFieldValuePair(value, sub_message) + value['@type'] = type_url # Sets Any message message.value = sub_message.SerializeToString() message.type_url = type_url