Skip to content

Commit

Permalink
Fix a python bug when assign an empty Struct at creation.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 689211445
  • Loading branch information
anandolee authored and copybara-github committed Oct 24, 2024
1 parent d2194ef commit 47613cf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python/google/protobuf/internal/python_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ def init(self, **kwargs):
)
)

if new_val:
if new_val != None:
try:
field_copy.MergeFrom(new_val)
except TypeError:
Expand Down
31 changes: 31 additions & 0 deletions python/google/protobuf/internal/well_known_types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import datetime
import unittest

from google.protobuf import json_format
from google.protobuf import text_format
from google.protobuf.internal import more_messages_pb2
from google.protobuf.internal import well_known_types
Expand Down Expand Up @@ -1040,6 +1041,36 @@ def testPackDeterministic(self):
b'\x0e\x1a\x05\n\x018\x10\x10\x1a\x05\n\x019\x10\x12')
self.assertEqual(golden, serialized)

def testJsonStruct(self):
value = struct_pb2.Value(struct_value=struct_pb2.Struct())
value_dict = json_format.MessageToDict(
value,
always_print_fields_with_no_presence=True,
preserving_proto_field_name=True,
use_integers_for_enums=True,
)
self.assertDictEqual(value_dict, {})

s = struct_pb2.Struct(
fields={
'a': struct_pb2.Value(struct_value=struct_pb2.Struct()),
},
)

sdict = json_format.MessageToDict(
s,
always_print_fields_with_no_presence=True,
preserving_proto_field_name=True,
use_integers_for_enums=True,
)

self.assertDictEqual(
sdict,
{
'a': {},
},
)


if __name__ == '__main__':
unittest.main()

0 comments on commit 47613cf

Please sign in to comment.