@@ -66,8 +66,9 @@ bool MessageMayContainExtensionsMemoized(const ::google::protobuf::Descriptor* d
6666}
6767
6868struct HasUnknownFields {
69- HasUnknownFields (const ::google::protobuf::Descriptor* root_descriptor)
70- : root_descriptor(root_descriptor) {}
69+ HasUnknownFields (const ::google::protobuf::python::PyProto_API* py_proto_api,
70+ const ::google::protobuf::Descriptor* root_descriptor)
71+ : py_proto_api(py_proto_api), root_descriptor(root_descriptor) {}
7172
7273 std::string FieldFQN () const { return absl::StrJoin (field_fqn_parts, " ." ); }
7374 std::string FieldFQNWithFieldNumber () const {
@@ -81,6 +82,7 @@ struct HasUnknownFields {
8182
8283 std::string BuildErrorMessage () const ;
8384
85+ const ::google::protobuf::python::PyProto_API* py_proto_api;
8486 const ::google::protobuf::Descriptor* root_descriptor = nullptr ;
8587 const ::google::protobuf::Descriptor* unknown_field_parent_descriptor = nullptr ;
8688 std::vector<std::string> field_fqn_parts;
@@ -97,9 +99,15 @@ bool HasUnknownFields::FindUnknownFieldsRecursive(
9799 reflection.GetUnknownFields (*sub_message);
98100 if (!unknown_field_set.empty ()) {
99101 unknown_field_parent_descriptor = sub_message->GetDescriptor ();
100- field_fqn_parts.resize (depth);
101102 unknown_field_number = unknown_field_set.field (0 ).number ();
102- return true ;
103+
104+ // Stop only if the extension is known by Python.
105+ if (py_proto_api->GetDefaultDescriptorPool ()->FindExtensionByNumber (
106+ unknown_field_parent_descriptor,
107+ unknown_field_number)) {
108+ field_fqn_parts.resize (depth);
109+ return true ;
110+ }
103111 }
104112
105113 // If this message does not include submessages which allow extensions,
@@ -174,9 +182,10 @@ void AllowUnknownFieldsFor(absl::string_view top_message_descriptor_full_name,
174182}
175183
176184std::optional<std::string> CheckAndBuildErrorMessageIfAny (
185+ const ::google::protobuf::python::PyProto_API* py_proto_api,
177186 const ::google::protobuf::Message* message) {
178187 const auto * root_descriptor = message->GetDescriptor ();
179- HasUnknownFields search{root_descriptor};
188+ HasUnknownFields search{py_proto_api, root_descriptor};
180189 if (!search.FindUnknownFieldsRecursive (message, 0u )) {
181190 return std::nullopt ;
182191 }
0 commit comments