From 579f4ab70dc5c37f075a0b3f186fe80dcdf8165d Mon Sep 17 00:00:00 2001 From: Eric Salo Date: Wed, 22 Mar 2023 07:25:40 -0700 Subject: [PATCH] fix Python bug with required fields https://github.com/protocolbuffers/upb/issues/1220 There were two bugs here: Python was incorrectly mandating that a required field be set during assignment, and it was also incorrectly assuming a non-NULL return from an internal function call. PiperOrigin-RevId: 518561818 --- python/google/protobuf/internal/message_test.py | 8 ++++++++ python/google/protobuf/internal/more_messages.proto | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/python/google/protobuf/internal/message_test.py b/python/google/protobuf/internal/message_test.py index cdcc5b09ed..dce675354b 100755 --- a/python/google/protobuf/internal/message_test.py +++ b/python/google/protobuf/internal/message_test.py @@ -53,6 +53,7 @@ cmp = lambda x, y: (x > y) - (x < y) from google.protobuf.internal import api_implementation # pylint: disable=g-import-not-at-top from google.protobuf.internal import encoder from google.protobuf.internal import more_extensions_pb2 +from google.protobuf.internal import more_messages_pb2 from google.protobuf.internal import packed_field_test_pb2 from google.protobuf.internal import test_proto3_optional_pb2 from google.protobuf.internal import test_util @@ -496,6 +497,13 @@ class MessageTest(unittest.TestCase): msg.repeated_nested_message.MergeFrom(other_msg.repeated_nested_message) self.assertEqual([1, 2, 3, 4], [m.bb for m in msg.repeated_nested_message]) + @unittest.skipIf( + api_implementation.Type() == 'upb', + "We can enable after protobuf updates to use the current upb repo") + def testInternalMergeWithMissingRequiredField(self, message_module): + req = more_messages_pb2.RequiredField() + more_messages_pb2.RequiredWrapper(request=req) + def testAddWrongRepeatedNestedField(self, message_module): msg = message_module.TestAllTypes() try: diff --git a/python/google/protobuf/internal/more_messages.proto b/python/google/protobuf/internal/more_messages.proto index 524dc701a9..3c5c971fae 100644 --- a/python/google/protobuf/internal/more_messages.proto +++ b/python/google/protobuf/internal/more_messages.proto @@ -358,3 +358,11 @@ message LotsNestedMessage { message B254 {} message B255 {} } + +message RequiredField { + required int32 id = 1; +} + +message RequiredWrapper { + optional RequiredField request = 1; +}