Fix parsing empty Struct Values from Json (#5211)
* Fix parsing empty Struct Values from Json
This fixes a bug. When parsing a struct from JSON like
struct = json_format.Parse('{"k": {}}', Struct())
then the struct's "k" value would end up not initialized, and accessing
the value would raise an error.
In[1]: struct['k']
ValueError: Value not set
That seems to be because the Struct field of the Value was not set.
In[2]: struct
Out[2]:
fields {
key: "k"
value {
}
}
This commit makes sure that the Value's Struct field is initialized even
if the Struct has no values itself.
This commit also extends a test to cover this case.
* Additionally test for empty list
diff --git a/python/google/protobuf/internal/json_format_test.py b/python/google/protobuf/internal/json_format_test.py
index 7f64940..bd85ce8 100644
--- a/python/google/protobuf/internal/json_format_test.py
+++ b/python/google/protobuf/internal/json_format_test.py
@@ -495,6 +495,8 @@
message.value['email'] = None
message.value.get_or_create_struct('address')['city'] = 'SFO'
message.value['address']['house_number'] = 1024
+ message.value.get_or_create_struct('empty_struct')
+ message.value.get_or_create_list('empty_list')
struct_list = message.value.get_or_create_list('list')
struct_list.extend([6, 'seven', True, False, None])
struct_list.add_struct()['subkey2'] = 9
@@ -509,6 +511,8 @@
' "city": "SFO", '
' "house_number": 1024'
' }, '
+ ' "empty_struct": {}, '
+ ' "empty_list": [], '
' "age": 10, '
' "name": "Jim", '
' "attend": true, '
@@ -519,6 +523,8 @@
'}'))
parsed_message = json_format_proto3_pb2.TestStruct()
self.CheckParseBack(message, parsed_message)
+ parsed_message.value['empty_struct'] # check for regression; this used to raise
+ parsed_message.value['empty_list']
def testValueMessage(self):
message = json_format_proto3_pb2.TestValue()
diff --git a/python/google/protobuf/json_format.py b/python/google/protobuf/json_format.py
index ce1db7d..e466f55 100644
--- a/python/google/protobuf/json_format.py
+++ b/python/google/protobuf/json_format.py
@@ -614,6 +614,9 @@
if not isinstance(value, dict):
raise ParseError(
'Struct must be in a dict which is {0}.'.format(value))
+ # Clear will mark the struct as modified so it will be created even if
+ # there are no values.
+ message.Clear()
for key in value:
self._ConvertValueMessage(value[key], message.fields[key])
return