C
C#5d ago
nikatark

Grpc.Tools generating camelCase classes

Hello, I am integrating a Java gRPC service in my .NET application but gRPC.Tools generates camelCase classes I boiled down the proto file to minumum for the example
syntax = "proto3";

package services;

option java_package = "services";
option java_multiple_files = true;

//file content

service ExampleService {
rpc example (exampleRequest) returns (exampleResponse);
}

message exampleRequest {
int32 user_id = 1;
}

message exampleResponse {
int32 status_code = 1;
string message = 2;
}
syntax = "proto3";

package services;

option java_package = "services";
option java_multiple_files = true;

//file content

service ExampleService {
rpc example (exampleRequest) returns (exampleResponse);
}

message exampleRequest {
int32 user_id = 1;
}

message exampleResponse {
int32 status_code = 1;
string message = 2;
}
after building, the exampleRequest class is generated as it was written in the .proto file, and since the file was generated by a java server, its camelCase instead of PascalCase
var channel = GrpcChannel.ForAddress("http://example.com");
var client = new ExampleService.ExampleServiceClient(channel);
var request = new exampleRequest { UserId = 1 };
var channel = GrpcChannel.ForAddress("http://example.com");
var client = new ExampleService.ExampleServiceClient(channel);
var request = new exampleRequest { UserId = 1 };
my .csproj file:
<ItemGroup>
<Protobuf Include="services\exampleService.proto" GrpcServices="Client" />
</ItemGroup>
<ItemGroup>
<Protobuf Include="services\exampleService.proto" GrpcServices="Client" />
</ItemGroup>
Is there any way to force PascalCase names in the generated classes?
4 Replies
ero
ero5d ago
not directly a solution, but you might want to know that you're violating the protobuf style guidelines by using camelCase for message names
ero
ero5d ago
Style Guide
Provides direction for how best to structure your proto definitions.
nikatark
nikatarkOP5d ago
Thank you, I was not aware of that naming convention. Since I was given the file by another department I will bring it up to them
canton7
canton74d ago
I think that probably is the solution: I suspect there's no code to do case transformation for C#, because the casing of messages should already be PascalCase Yeah, I don't see anything in the source to change the case of message names: https://github.com/protocolbuffers/protobuf/blob/fb78c09e1671eed5321bc41816e4656bf966cfa3/src/google/protobuf/compiler/csharp/names.cc#L73 (whereas there is conversion logic for fields: https://github.com/protocolbuffers/protobuf/blob/fb78c09e1671eed5321bc41816e4656bf966cfa3/src/google/protobuf/compiler/csharp/csharp_helpers.cc#L230) Types in Java should also be in PascalCase though, no?

Did you find this page helpful?