-
-
Notifications
You must be signed in to change notification settings - Fork 188
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
"name" property is a reserved keyword in Typescript generator #1053
Comments
|
Always make sure you use one of the serialization presets otherwise there is no guarantee that the serialized models matches the expected JSON 🙂 The helper functions that are then included in your models always ensure that the right property names are used regardless of whether it's changed because of the language. The same goes for additional properties (from JSON Schema) as it needs to be unwrapped within the enclosing object. The reserved keyword comes from JavaScript which is also ensured in TypeScript as often times the TypeScript models are transpiled to JS afterwards🙂 |
I'm using Is The reason why this jumped out as a bug to me is that I'm porting some OpenAPI schemas to AsyncAPI, and Here's an example of an interface for a model that exports a Are you saying there's no way to use a schema with a property/field called |
This is how I use the models: const model = ...
const jsonmodel : any = model.marshal();
const dataToSend = codec.encode(jsonmodel);
js.publish("mychannel", dataToSend);`; This is from the TS NATS template: https://github.com/asyncapi/ts-nats-template/blob/81cce86ea4a07f2b7054d09956d86932a22548b5/components/channel/jetstreamPublish.js#L19
Yea, that should preferably not happen of course 😄
It was taken from here: https://www.w3schools.com/js/js_reserved.asp I can't off the top of my head remember which exact situation it cant be used.
Not if you are transpiling the models to js, no. But it's entirely possible the JS keywords should only be seen as reserved given some new generator option, something like |
Thanks for lending your brainpower on this one! 🙇♀️ I think a boolean option like Zooming out a bit, what do you think about reserved keywords being a preset? I know your design goals for Modelina are oriented toward modularity + customization. Coming from the OpenAPI universe, a few generators (like Dart IIRC) allow you to pass a custom reserved word list. That way you can remove certain terms or even add your own reserved terms. |
I definitely think that would overload the presets, as they are not meant to be constraining the models even further, it should have already happened at the constraint phase, if not, then it's because we are missing some things in it's implementation. It also keeps it super clean as presets does one thing only, changing the generated model code 🙂 However, the thing about a custom reserved word list could definitely be an option to add! Feel free to raise this in a separate feature request! 🙇 |
Removing this from the v1 milestone as this is a simple feature change and potential breaking change for changing default behavior. |
We just stumbled across this with the property name "status" which was turned into "reservedStatus". We have customized the List now by providing the propertykey constariner, with a custom set of keywords. We stuck to the typescript keywords + this list for properties which seems to be quite a bit short than the standard list: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_words I think the current list might be a bit to long.
|
Any suggestion for specific improvements we can take @200Puls ? |
Hi @jonaslagoni, I did some more research and my suggestion would be to use the official reserved word list of the Ecmascript specification for the javascript generator: https://tc39.es/ecma262/#sec-keywords-and-reserved-words
and the future reserved words:
|
Just for info, we have also experienced this for properties named |
This is getting fixed by #1727 |
Describe the bug
I noticed one of my Typescript models is being generated with a
reserved_name
property instead of justname
- even thoughname
isn't a reserved keyword in Typescript.How to Reproduce
version:
1.0.0-next.36
reserved_name
is rendered as the property name.Here's my generator config, just in case that's relevant:
name
Expected behavior
The following Typescript interface should be generated, given the schema above:
The text was updated successfully, but these errors were encountered: