pulsar-client-cpp icon indicating copy to clipboard operation
pulsar-client-cpp copied to clipboard

[Bug] Build fails with Protobuf 30 from type changes

Open cho-m opened this issue 10 months ago • 1 comments

Search before asking

  • [x] I searched in the issues and found nothing similar.

Version

OS: macOS 15 Client: v3.7.0

Minimal reproduce step

Should be reproducible with any build attempt using Protobuf 30.0 (C++ 6.30.0) or later.

However, won't be able to use Vcpkg build yet as they only have Protobuf 29.3 (C++ 5.29.3).

What did you expect to see?

Successful build

What did you see instead?

lib/ProtobufNativeSchema.cc:42:23: error: no viable conversion from 'internal::DescriptorStringView' (aka 'basic_string_view<char>') to 'const std::string' (aka 'const basic_string<char>')
   42 |     const std::string rootMessageTypeName = descriptor->full_name();
      |                       ^                     ~~~~~~~~~~~~~~~~~~~~~~~

Anything else?

Caused by breaking change in https://github.com/protocolbuffers/protobuf/commit/d1990d968a54176eb9f4229abe7f7c97ece50cec

Should be possible to use explicit constructor for std::string instead and work for both Protobuf 30 and older compatibility. I think move constructor/assignment should avoid unnecessary copies for older Protobuf.

Are you willing to submit a PR?

  • [x] I'm willing to submit a PR!

cho-m avatar Mar 14 '25 15:03 cho-m

A simple fix could be something like:

diff --git a/lib/ProtobufNativeSchema.cc b/lib/ProtobufNativeSchema.cc
index 632943d..cbcda43 100644
--- a/lib/ProtobufNativeSchema.cc
+++ b/lib/ProtobufNativeSchema.cc
@@ -39,8 +39,13 @@ SchemaInfo createProtobufNativeSchema(const google::protobuf::Descriptor* descri
     }
 
     const auto fileDescriptor = descriptor->file();
+#if GOOGLE_PROTOBUF_VERSION >= 6030000
+    const std::string& rootMessageTypeName = std::string(descriptor->full_name());
+    const std::string& rootFileDescriptorName = std::string(fileDescriptor->name());
+#else
     const std::string& rootMessageTypeName = descriptor->full_name();
     const std::string& rootFileDescriptorName = fileDescriptor->name();
+#endif
 
     FileDescriptorSet fileDescriptorSet;
     internalCollectFileDescriptors(fileDescriptor, fileDescriptorSet);

Perhaps more efficient would be to pass absl::string_view to absl::StrCat^1

diff --git a/lib/ProtobufNativeSchema.cc b/lib/ProtobufNativeSchema.cc
index 632943d..45c46ab 100644
--- a/lib/ProtobufNativeSchema.cc
+++ b/lib/ProtobufNativeSchema.cc
@@ -19,6 +19,9 @@
 #include "pulsar/ProtobufNativeSchema.h"
 
 #include <google/protobuf/descriptor.pb.h>
+#if GOOGLE_PROTOBUF_VERSION >= 6030000
+#include <absl/strings/str_cat.h>
+#endif
 
 #include <stdexcept>
 #include <vector>
@@ -39,8 +42,8 @@ SchemaInfo createProtobufNativeSchema(const google::protobuf::Descriptor* descri
     }
 
     const auto fileDescriptor = descriptor->file();
-    const std::string& rootMessageTypeName = descriptor->full_name();
-    const std::string& rootFileDescriptorName = fileDescriptor->name();
+    const auto& rootMessageTypeName = descriptor->full_name();
+    const auto& rootFileDescriptorName = fileDescriptor->name();
 
     FileDescriptorSet fileDescriptorSet;
     internalCollectFileDescriptors(fileDescriptor, fileDescriptorSet);
@@ -48,9 +51,15 @@ SchemaInfo createProtobufNativeSchema(const google::protobuf::Descriptor* descri
     std::vector<char> bytes(fileDescriptorSet.ByteSizeLong());
     fileDescriptorSet.SerializeToArray(bytes.data(), bytes.size());
 
+#if GOOGLE_PROTOBUF_VERSION >= 6030000
+    const std::string schemaJson = absl::StrCat(R"({"fileDescriptorSet":")", base64::encode(bytes),
+                                                R"(","rootMessageTypeName":")", rootMessageTypeName,
+                                                R"(","rootFileDescriptorName":")", rootFileDescriptorName, R"("})");
+#else
     const std::string schemaJson = R"({"fileDescriptorSet":")" + base64::encode(bytes) +
                                    R"(","rootMessageTypeName":")" + rootMessageTypeName +
                                    R"(","rootFileDescriptorName":")" + rootFileDescriptorName + R"("})";
+#endif
 
     return SchemaInfo(SchemaType::PROTOBUF_NATIVE, "", schemaJson);
 }

cho-m avatar Aug 21 '25 15:08 cho-m