From 03676b31f622ad1aae14c4b96e1c87b1ca352d1c Mon Sep 17 00:00:00 2001 From: cmeng Date: Fri, 17 Oct 2025 04:39:59 +0000 Subject: [PATCH] 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 --- .../websocket_server/WebsocketServer.cc | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/systems/websocket_server/WebsocketServer.cc b/src/systems/websocket_server/WebsocketServer.cc index 3d36b8327e..9eb634e0d9 100644 --- a/src/systems/websocket_server/WebsocketServer.cc +++ b/src/systems/websocket_server/WebsocketServer.cc @@ -715,11 +715,15 @@ void WebsocketServer::OnMessage(int _socketId, const std::string _msg) gzdbg << "Protos request received\n"; std::string allProtos = "syntax = \"proto3\";\n"; - allProtos += "package gz.msgs;\n"; + allProtos += "package gz.msgs;\n\n"; std::vector types; gz::msgs::Factory::Types(types); + // Track processed files and enums to avoid duplicates + std::set processedFiles; + std::set processedEnums; + // Get all the messages, and build a single proto to send to the client. for (auto const &type : types) { @@ -739,7 +743,28 @@ void WebsocketServer::OnMessage(int _socketId, const std::string _msg) continue; if (descriptor) + { + auto fileDescriptor = descriptor->file(); + + // Only process each file once to extract top-level enums + if (processedFiles.insert(fileDescriptor).second) + { + // Extract all top-level enums from this file + for (int i = 0; i < fileDescriptor->enum_type_count(); ++i) + { + auto enumDescriptor = fileDescriptor->enum_type(i); + if (processedEnums.insert(enumDescriptor).second) + { + allProtos += enumDescriptor->DebugString(); + allProtos += "\n"; + } + } + } + + // Add the message definition (without file-level import statements) allProtos += descriptor->DebugString(); + allProtos += "\n"; + } else { gzerr << "Failed to get the descriptor for message["