ソースを参照

fix json_format.ParseDict modifies input (#5267)

* fix json_format.ParseDict modifies input

* added unit test

* fix naming
giokara-oqton 6 年 前
コミット
f22be4ddb0

+ 10 - 0
python/google/protobuf/internal/json_format_test.py

@@ -200,6 +200,16 @@ class JsonFormatTest(JsonFormatBase):
     json_format.ParseDict(message_dict, parsed_message)
     json_format.ParseDict(message_dict, parsed_message)
     self.assertEqual(message, 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):
   def testExtensionSerializationDictMatchesProto3Spec(self):
     """See go/proto3-json-spec for spec.
     """See go/proto3-json-spec for spec.
     """
     """

+ 1 - 0
python/google/protobuf/json_format.py

@@ -573,6 +573,7 @@ class _Parser(object):
     else:
     else:
       del value['@type']
       del value['@type']
       self._ConvertFieldValuePair(value, sub_message)
       self._ConvertFieldValuePair(value, sub_message)
+      value['@type'] = type_url
     # Sets Any message
     # Sets Any message
     message.value = sub_message.SerializeToString()
     message.value = sub_message.SerializeToString()
     message.type_url = type_url
     message.type_url = type_url