-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[typescript] Utility types now considered when generating schemas (Issue #21317) #21414
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
Conversation
|
refs #21317 |
|
I am not following 100% what's going on in this pull request, but it seems like there is a lexer/parser for the typescript grammar being generated in order to find types and their generics? Whilst I can appreciate the lean solution, I don't think this is a future-safe, sustainable way to do this. Whilst being non-standard, it wouldn't automatically extend to future typescript grammars without manual update. A few thoughts: |
|
Good Day, @joscha , thank you for your feedback,
I'm not fully sure what you mean by "portable", but I'm also not sure what that "ported to Go" comment from the g4 file meant either, so maybe that's where the misunderstanding starts
I don't know how WASM works, so I can't really speak to this
I'm not sure if I can call it "standard", but I got the files from the Grammars repository, and the TypeScriptParser got its last update 7 months ago, on November 4 2024
I don't know much about Rhino, but from what I've looked up, I don't see anything about it supporting TypeScript
I've thought about that, and then it also came to my mind that if we were to do similar for every language that this project supports, then it would be quite the amount of overhead Overall, I get where you're coming from. The fact that I couldn't even write all my unit tests within this PR without running into a parsing bug proves that parsing - as it is currently may - not be that much of an enhancement. I could try rolling back these assertion classes and use regular String comparisons instead. Let me know if that's okay |
|
Just a short note that I don't think the intent if this PR is bad, quite the opposite, I am just worried about the maintenance baggage that might come with it, especially because going forward we expect people making updates to the generator also to possibly deal with antlr, which can't be expected. |
| } | ||
| } | ||
|
|
||
| @Test(description = "Issue #21317") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of using a parser, i would suggest to just do sample file generation like https://github.com/OpenAPITools/openapi-generator/blob/master/bin/configs/typescript-fetch-allOf-nullable.yaml, resulting in https://github.com/OpenAPITools/openapi-generator/tree/master/samples/client/petstore/typescript-fetch/builds/allOf-nullable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, thank you. So to be clear, would I do something like this?:
- Create
typescript-fetch-utility-types.yaml - Set
inputSpecto the path forissue_21317.yaml - Build the sample and then test it from the
samplesdirectory by using.github/workflows/samples-typescript-typecheck.yamlas a guide, meaning I just run./bin/ts-typecheck-all.shin WSL
And also, should I remove everything that involves ANTLR+TypeScript for now - that is - the whole assertions package and the grammar files, or do I just remove their usage in the unit test specifically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Create typescript-fetch-utility-types.yaml
- Set inputSpec to the path for issue_21317.yaml
yes exactly
- Build the sample and then test it from the samples directory by using .github/workflows/samples-typescript-typecheck.yaml as a guide, meaning I just run ./bin/ts-typecheck-all.sh in WSL
that wouldnt be needed here, reviewers would usually just inspect the generated samples. although if specific things should be tested, a typescript unit-test can be written as well.
And also, should I remove everything that involves ANTLR+TypeScript for now - that is - the whole assertions package and the grammar files
yes please, since its not needed
|
@macjohnny , thank you for the review. Please do you think this properly addresses the original issue? @joscha , I've removed ANTLR by the advice of macjohnny. Do you think this fixes the main issue? |
| return openAPIType; | ||
| } else if (typeMapping.containsKey(openAPIType)) { | ||
| type = typeMapping.get(openAPIType); | ||
| String typeWithoutGeneric = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please extract the typeWithoutGeneric calculation into a separate method to deduplicate the code
| TestUtils.assertFileContains(userModel.toPath(), "Record<string,unknown>"); | ||
|
|
||
|
|
||
| File noRuntimeOutput = new File(output, "noruntime"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please split this into a separate test case
| @@ -0,0 +1,10 @@ | |||
| generatorName: typescript-fetch | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for adding the sample configs here, but after looking at the updated unit test, which covers the relevant code changes, i would argue the sample configs bin/configs/typescript-fetch-utility-types-without-runtime.yaml and bin/configs/typescript-fetch-utility-types.yaml and the corresponding samples should be removed, since:
- the diff to other samples is minimal
- the functionality is already covered in the unit test
- there is no big change in the template
| import org.antlr.v4.runtime.*; | ||
| import org.antlr.v4.runtime.tree.ParseTree; | ||
| import org.antlr.v4.runtime.tree.ParseTreeWalker; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| import org.antlr.v4.runtime.*; | |
| import org.antlr.v4.runtime.tree.ParseTree; | |
| import org.antlr.v4.runtime.tree.ParseTreeWalker; |
leftovers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yes. Sorry. Thank you for that
| // Typescript reserved words | ||
| "abstract", "await", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "debugger", "default", "delete", "do", "double", "else", "enum", "export", "extends", "false", "final", "finally", "float", "for", "function", "goto", "if", "implements", "import", "in", "instanceof", "int", "interface", "let", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this", "throw", "transient", "true", "try", "typeof", "var", "void", "volatile", "while", "with", "yield")); | ||
|
|
||
| defaultIncludes = new HashSet<>(Arrays.asList( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's really unfortunate we maintain this list :( Also, what happens if I have a custom type with the same name that shadows any of these? My assumption is that the generated code won't be correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe toTypescriptTypeName will append "Model" for those types
| "Set" | ||
| "Set", | ||
| //Utility types | ||
| "Awaited","Partial","Required","Readonly","Record","Pick","Omit","Exclude","Extract","NonNullable", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we at least reuse the list from above here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'll make a local variable utilityTypes and use addAll
yes, i think so :) |
|
Okay. Thank you both. I'll revert by the weekend or sooner |
|
@macjohnny @joscha , I've made the changes, but it seems I have to wait for some issues to be fixed from upstream as relates to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
|
@DavidGrath please update the docs and the samples and merge the latest master branch. |
|
@macjohnny Done |
|
@DavidGrath can you please merge the most recent master branch and re-generate the samples and docs? looks like 5000+ files were changed because of some version string |
|
@macjohnny , please can you confirm? I should have already merged the 7.14.0 release with 600ad25 before I generated the docs |
|
@DavidGrath yeah lets just try to merge the latest master, and re-generate the samples and docs, that should hopefully get rid of the unrelated version string changes |
|
@macjohnny , okay, I'll try and revert between now and Friday |
|
@macjohnny , sorry for the delay, I've merged and regenerated the 7.15-snapshot samples |
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)@joscha @mkusaka , please help review