[REQ] Flag to Ignore Reserved Keywords
See original GitHub issueIs your feature request related to a problem? Please describe.
The JSON Web Signatures RFC [1] says that a signature can have a property called protected
.
The typescript-axios
generator however renames properties like protected
into _protected
because it’s a reserved keyword in Typescript. The issue is that though a reserved keyword in the language, that doesn’t mean that an object cannot have a property declared with that name (protected
).
So I’m stuck because I need work with the standard, but the generator makes this impossible even though the language (Typescript) would be just fine because it’s not reserved when it comes to naming class/interface members.
[1] https://tools.ietf.org/html/rfc7515#section-7.2
Describe the solution you’d like
Whatever the maintainers think is best that resolves the problem above. One idea I had is to add a flag that turns off the whole reserved keyword hand-holding feature of the generator and it’s up to the programmer to define the API specs in such a way that the generated code will compile.
This seemed like the simplest and quickest way to get me to where I want to be but I’ll take whatever else that gets the job done.
Describe alternatives you’ve considered
Just remove the protected
keyword from the list of reserved keywords in the generator altogether because it is reserved only in certain cases and using protected
as a class/interface member is definitely not one of them.
Edit on 2020-12-17:
Solution Found:
For those who come here looking for a solution to the same problem I had here’s how you can specify to have the protected
reserved keyword of Typescript to not get mangled in your model property names as _protected
:
java -jar openapi-generator-cli.jar generate --reserved-words-mappings protected=protected --input-spec=./openapi-spec.json -g typescript-axios
The generated model properties will not break the compilation with this because protected
is a valid property name, you just can’t use it in the code itself when let’s say declaring a variable.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top GitHub Comments
@macjohnny Finally got around to take a look at this, started with the implementation and quickly realized that the solution was under my nose the whole time in the form of the reserved keywords mapping feature that is already in the generator and can be used to just map reserved keywords back onto themselves as a neat trick.
So with all that said, I would like to withdraw my case here because I don’t think there should be yet another CLI arg to “ignore reserved keywords” when it’s perfectly fine (I wouldn’t even call it a workaround) to just map the reserved keyword onto itself and be done with it.
Thank you for all the support you’ve provided me in discovering this issue, it was especially helpful to receive the exact locations of the important lines of code in your previous comment, that’s where I started and it took me about 5 minutes to realize what I had to do after that.
For those who come here looking for a solution to the same problem I had here’s how you can specify to have the
protected
reserved keyword of Typescript to not get mangled in your model property names as_protected
:The generated model properties will not break the compilation with this because
protected
is a valid property name, you just can’t use it in the code itself when let’s say declaring a variable.The reserved words are defined here: https://github.com/OpenAPITools/openapi-generator/blob/336521b464d9edf42002a89d956499a4f73986e4/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java#L111
But we cannot simply change the list, it is more about adjusting when to escape and when not to escape. https://github.com/OpenAPITools/openapi-generator/blob/336521b464d9edf42002a89d956499a4f73986e4/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java#L303 https://github.com/OpenAPITools/openapi-generator/blob/336521b464d9edf42002a89d956499a4f73986e4/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java#L343