Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep '_' in property name in C# generated code #4120

Closed
VaronisDNMacClient opened this issue Jan 1, 2018 · 9 comments
Closed

Keep '_' in property name in C# generated code #4120

VaronisDNMacClient opened this issue Jan 1, 2018 · 9 comments

Comments

@VaronisDNMacClient
Copy link

Hi,

From trial and error, the style guide and source code I see that all recurrences of the character '_' are eliminated from property name when generating C# code.

Can this be overridden or avoided in some way? I need my properties with underscore (or any other char that can be perceived as a separator).

@kunalspathak
Copy link

In addition to that, is there a way to specify "Keep property names as is" for the generated C# code? If not, is there an easy way to get property name for corresponding field name inside .proto file?
I am generating .proto file on the fly, pass it through protoc to generate C# code and I generate some more C# code that populates the protobuf objects to drive further business logic. My problem is that I don't have a mapping of field names that I generated within .proto file and the corresponding property names that protoc generated.

@TeBoring TeBoring added this to To Do in Weekly Fixit via automation Apr 2, 2018
@anandolee
Copy link
Contributor

@anandolee anandolee moved this from To Do to In Progress in Weekly Fixit Apr 9, 2018
@kunalspathak
Copy link

kunalspathak commented Apr 10, 2018

Thanks @anandolee . I already tried that but it doesn't quite give what I want. Imagine protobuf message like this -

    message FooMulInputType {
        repeated sint32 param1 = 1;
        repeated sint32 Param2 = 2;
        repeated sint32 p_aram3 = 3;
        repeated sint32 p_aram_4 = 4;
    }

If i do something like this, I don't get desired ouput:

FieldDescriptor par1 = FooApisType.Types.FooMulInputType.Descriptor.FindDescriptor<FieldDescriptor>("p_aram3");
Console.WriteLine(par1.Name); // "p_aram3"

But this is not the property name that is generated in C#. See below screenshot that shows property names protoc.exe has generated and I want a way to get these property names instead. Let me know if there is a way to do that.

image

@anandolee
Copy link
Contributor

anandolee commented May 21, 2018

It is in the FieldDescriptor.propertyName: https://github.com/google/protobuf/blob/047575f20cbdee8fb2f88d78a317ffe6133b93e6/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs#L46

I think it is reasonable to make a public get API for the propertyName. Are you happy to make a PR for it?

@kunalspathak
Copy link

sure, will do it sometime.

@anandolee
Copy link
Contributor

Friendly ping. @kunalspathak do you get time for it?

@kunalspathak
Copy link

sorry, not yet. I might have some time starting 1st week of August.

@quintenp
Copy link

quintenp commented Nov 5, 2019

Has anyone found or implemented a way to keep the _ in field names instead of forcing camelCase?

@deannagarcia
Copy link
Member

Closing this due to the fix in #7642.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Weekly Fixit
  
In Progress
Development

No branches or pull requests

6 participants