Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/IceRpc.Protobuf.Generator/MessageDescriptorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@ internal static class MessageDescriptorExtensions
{
internal static string GetType(this MessageDescriptor messageDescriptor, string scope, bool streaming)
{
string csharpNamespace = messageDescriptor.File.GetCsharpNamespace();
string qualifiedName = messageDescriptor.GetQualifiedCsharpName();
string csharpType = scope == csharpNamespace ?
qualifiedName : $"global::{csharpNamespace}.{qualifiedName}";
string csharpType = messageDescriptor.GetQualifiedTypeName(scope);
if (streaming)
{
csharpType = $"global::System.Collections.Generic.IAsyncEnumerable<{csharpType}>";
}
return csharpType;
}

internal static string GetParserType(this MessageDescriptor messageDescriptor, string scope)
internal static string GetParserType(this MessageDescriptor messageDescriptor, string scope) =>
$"{messageDescriptor.GetQualifiedTypeName(scope)}.Parser";

private static string GetQualifiedTypeName(this MessageDescriptor messageDescriptor, string scope)
{
string csharpNamespace = messageDescriptor.File.GetCsharpNamespace();
string qualifiedName = messageDescriptor.GetQualifiedCsharpName();
string csharpType = scope == csharpNamespace ?
qualifiedName : $"global::{csharpNamespace}.{qualifiedName}";
return $"{csharpType}.Parser";
return scope == csharpNamespace ?
qualifiedName :
csharpNamespace.Length == 0 ?
$"global::{qualifiedName}" :
$"global::{csharpNamespace}.{qualifiedName}";
}

// Google's C# Protobuf generator emits nested message types inside a "Types" container class on each enclosing
Expand Down
7 changes: 6 additions & 1 deletion src/IceRpc.Protobuf.Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@
using IceRpc.Protobuf.RpcMethods;
");

codeBlock.AddBlock($"namespace {descriptor.GetCsharpNamespace()};");
string csharpNamespace = descriptor.GetCsharpNamespace();
if (csharpNamespace.Length > 0)
{
codeBlock.AddBlock($"namespace {csharpNamespace};");
}
// else generated types land in the global namespace, matching Google's protoc-gen-csharp behavior.

foreach (ServiceDescriptor service in descriptor.Services)
{
Expand Down
15 changes: 15 additions & 0 deletions tests/IceRpc.Protobuf.Tests/NoNamespaceTests.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) ZeroC, Inc.

// Intentionally has no `package` and no `option csharp_namespace`. The generator must emit valid C# in
// this case (no `namespace ;` declaration; cross-namespace references use `global::Type` rather than
// `global::.Type`).
Comment on lines +3 to +5
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regression .proto doesn’t currently exercise the cross-namespace reference scenario described in the header comment: nothing imports this file from a non-empty package/csharp_namespace, so the generator’s global::Type (vs global::.Type) path won’t be compiled/tested. Consider adding a second .proto with a non-empty package/csharp_namespace that imports this file and uses NoNamespaceMessage as an RPC input/output to ensure the generated C# contains global::NoNamespaceMessage and stays valid.

Copilot uses AI. Check for mistakes.

syntax = "proto3";

service NoNamespaceOperations {
rpc UnaryOp(NoNamespaceMessage) returns (NoNamespaceMessage);
}

message NoNamespaceMessage {
string value = 1;
}
Loading