question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[REQ] Flag to Ignore Reserved Keywords

See original GitHub issue

Is 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:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
petermetzcommented, Dec 17, 2020

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

@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:

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript Reserved Words - W3Schools
JavaScript Reserved Words ; let*, long, native, new ; null, package, private, protected ; public, return, short, static.
Read more >
List of Reserved Words - Manual - PHP
These lists include keywords and predefined variables, constant, and class names. These lists are neither exhaustive nor complete.
Read more >
How can I escape a PowerShell reserved word?
I am using PowerShell to compose a command to execute OpenCover which has a flag -filter.
Read more >
Reserved Keywords (Transact-SQL) - Microsoft Learn
Transact-SQL reserved keywords can be used as identifiers or names of databases or database objects, such as tables, columns, views, and so on....
Read more >
Avoid special characters and reserved words
Do not use special characters such as ( ) - & @ * $ | % ~ except for underscore (_). Also do...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found