Skip to content

Commit 03676b3

Browse files
committed
Fix websocket server proto definitions to include top-level enums
The websocket server's "protos" request handler was using descriptor->DebugString() which only outputs message definitions, causing top-level enums (like PixelFormatType) to be omitted from the generated proto definitions sent to clients. This caused protobuf.js clients to fail parsing the definitions because messages referenced enum types that were never defined. Changes: - Extract and include all top-level enums from each proto file - Use fileDescriptor->enum_type_count() to iterate enums - Add deduplication sets for both files and enums - Keep using descriptor->DebugString() for messages to avoid including import statements that would break single-file parsing The generated proto now includes all necessary enum definitions while remaining compatible with protobuf.js single-file parsing. Fixes missing PixelFormatType, SphericalCoordinatesType, and other top-level enums in the websocket proto definitions. Signed-off-by: cmeng <[email protected]>
1 parent dab822c commit 03676b3

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/systems/websocket_server/WebsocketServer.cc

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,11 +715,15 @@ void WebsocketServer::OnMessage(int _socketId, const std::string _msg)
715715
gzdbg << "Protos request received\n";
716716

717717
std::string allProtos = "syntax = \"proto3\";\n";
718-
allProtos += "package gz.msgs;\n";
718+
allProtos += "package gz.msgs;\n\n";
719719

720720
std::vector<std::string> types;
721721
gz::msgs::Factory::Types(types);
722722

723+
// Track processed files and enums to avoid duplicates
724+
std::set<const google::protobuf::FileDescriptor*> processedFiles;
725+
std::set<const google::protobuf::EnumDescriptor*> processedEnums;
726+
723727
// Get all the messages, and build a single proto to send to the client.
724728
for (auto const &type : types)
725729
{
@@ -739,7 +743,28 @@ void WebsocketServer::OnMessage(int _socketId, const std::string _msg)
739743
continue;
740744

741745
if (descriptor)
746+
{
747+
auto fileDescriptor = descriptor->file();
748+
749+
// Only process each file once to extract top-level enums
750+
if (processedFiles.insert(fileDescriptor).second)
751+
{
752+
// Extract all top-level enums from this file
753+
for (int i = 0; i < fileDescriptor->enum_type_count(); ++i)
754+
{
755+
auto enumDescriptor = fileDescriptor->enum_type(i);
756+
if (processedEnums.insert(enumDescriptor).second)
757+
{
758+
allProtos += enumDescriptor->DebugString();
759+
allProtos += "\n";
760+
}
761+
}
762+
}
763+
764+
// Add the message definition (without file-level import statements)
742765
allProtos += descriptor->DebugString();
766+
allProtos += "\n";
767+
}
743768
else
744769
{
745770
gzerr << "Failed to get the descriptor for message["

0 commit comments

Comments
 (0)