Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a python bug when assign an empty Struct at creation. #18977

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading