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

Option to force singular field naming (JS generator) #42

Closed
4nte opened this issue Oct 16, 2019 · 11 comments
Closed

Option to force singular field naming (JS generator) #42

4nte opened this issue Oct 16, 2019 · 11 comments

Comments

@4nte
Copy link

4nte commented Oct 16, 2019

What language does this apply to?
This is for proto3 Javascript code generation.

Describe the problem you are trying to solve.
A repeated field, when generated to javascript, will be appended with a List suffix. This behaviour introduces (forced) changes to the user-defined names which can sometimes be unwanted as we'd (generally) expect the field names to match the original proto file.

Describe the solution you'd like
Field annotation (a boolean) to force singular naming.

Describe alternatives you've considered

  1. Writing a custom mapper to rename the fields to their original proto name.
  2. Modifying generated JS code.

Additional context
https://stackoverflow.com/questions/51508553/why-does-the-protobuf-javascript-compiler-suffix-collections-with-list-map

@untra
Copy link

untra commented Jan 9, 2020

this also just bit us, and makes it hard to use shared protobuf fields with our generated typescript as well:
improbable-eng/ts-protoc-gen#86
😱

@majelbstoat
Copy link

majelbstoat commented Mar 21, 2020

I also have this problem. I can't use the generated types in a Typescript client consuming an API from grpc-gateway. grpc-gateway (correctly) sends my repeated field as questions. I can access response.questions in the client, but I can't type the result as QuestionsResponse.AsObject because that says I have a field response.questionsList, which I don't.

It's pretty unfriendly to change the field name from the definition. If the goal is to avoid collisions with internal fields, you could do something like the go implementation does and put them all in a weirdly named field like xxx_internal.

@majelbstoat
Copy link

Well, instead of just complaining, I wrote a protoc plugin for the grpc-gateway use-case. Maybe it will help someone out. No warranty implied, use at your own risk etc.

https://gist.github.com/majelbstoat/5f73f58a057123c8d46db66ebbaae987

@thesayyn
Copy link

You could just use https://github.com/thesayyn/protoc-gen-ts. it works well with almost every grpc framework out there.

@majelbstoat
Copy link

That's cool, but it doesn't work with gprc-gateway

@weisssean
Copy link

weisssean commented Sep 17, 2020

I use both python and JS protocol buffers and the deserialize results are different. it makes it pretty unusable. Why do we even need the "List" suffix in the first place?
Any workarounds?
Any chance this feature will make it to a release any time soon?

@domino14
Copy link

Does anyone have any workarounds or suggestions?

@janjaap
Copy link

janjaap commented May 17, 2021

In the project I'm currently working on, I had to resort to converting the generated client files manually, like so

find src/**/*_pb.js | xargs sed -i 's/\(List\|Map\): jspb\.Message/: jspb.Message/g'

@acozzette acozzette transferred this issue from protocolbuffers/protobuf May 16, 2022
@SmashPlusUltra
Copy link

It'll be nice to see the opinion of the developers which maintain the project about this issue.

@dibenede
Copy link
Contributor

Sorry we have no plans to change the naming conventions or adding options to control the naming due to compatibility and maintenance issues, respectively.

@jpgundrum
Copy link

Hey, I just wanted to update this thread as I have just recently dealt with this very issue. There is a forked repository which can be found here that removes the auto addition of "List" when the "repeated" keyword is used in the proto document.

After building the binary (protoc-gen-js in this case) from the cloned repository you can run the cmd protoc --plugin=protoc-gen-js=./protoc-gen-js --js_out=import_style=commonjs,binary:. my_proto_file.proto which will generate a .js file that does not auto append "List".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests