Commit 50342e48 authored by Peter Sobot's avatar Peter Sobot Committed by Jie Luo

Allowed json_format.ParseDict to parse extensions with scalar types. (#5609)

parent 8532804b
......@@ -202,6 +202,17 @@ class JsonFormatTest(JsonFormatBase):
json_format.ParseDict(message_dict, parsed_message)
self.assertEqual(message, parsed_message)
def testExtensionToDictAndBackWithScalar(self):
message = unittest_pb2.TestAllExtensions()
ext1 = unittest_pb2.TestNestedExtension.test
message.Extensions[ext1] = 'data'
message_dict = json_format.MessageToDict(
message
)
parsed_message = unittest_pb2.TestAllExtensions()
json_format.ParseDict(message_dict, parsed_message)
self.assertEqual(message, parsed_message)
def testJsonParseDictToAnyDoesNotAlterInput(self):
orig_dict = {
'int32Value': 20,
......
......@@ -564,7 +564,10 @@ class _Parser(object):
sub_message.SetInParent()
self.ConvertMessage(value, sub_message)
else:
setattr(message, field.name, _ConvertScalarFieldValue(value, field))
if field.is_extension:
message.Extensions[field] = _ConvertScalarFieldValue(value, field)
else:
setattr(message, field.name, _ConvertScalarFieldValue(value, field))
except ParseError as e:
if field and field.containing_oneof is None:
raise ParseError('Failed to parse {0} field: {1}'.format(name, e))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment