fix json_format.ParseDict modifies input (#5267)

* fix json_format.ParseDict modifies input

* added unit test

* fix naming
pull/5398/head
giokara-oqton 6 years ago committed by Jie Luo
parent 9313cdc442
commit f22be4ddb0
  1. 10
      python/google/protobuf/internal/json_format_test.py
  2. 1
      python/google/protobuf/json_format.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.
"""

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

Loading…
Cancel
Save