Skip to content

Conversation

@DavidGrath
Copy link
Contributor

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    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.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@joscha @mkusaka , please help review

@DavidGrath DavidGrath changed the title [typescript] Utility types now considered when generating schemas (Issue #21317) [typescript] ANTLR test enhancement. Utility types now considered when generating schemas (Issue #21317) Jun 14, 2025
@joscha
Copy link
Contributor

joscha commented Jun 14, 2025

refs #21317

@joscha
Copy link
Contributor

joscha commented Jun 14, 2025

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:
Can we use the TS parsing functionality itself - given this recently was ported to Go, could it be portable enough to be included here?
Alternatively, maybe one of the bigger AST generators like acorn has a WASM implementation that we can load?
Alternatively, is there a standard implementation of the lexer/parser part in this PR? Some widespread used Java package that has this functionality and is actively developed and we can lean on?
Alternatively, (this is already quite sad) is there a module that can load native JS in Java (Rhino comes to mind, but that is aeons old already) that we can use to load one of the AST generators and/or ts package itself inside java?
Alternatively, we can expect a dependency on node and call out to it at generation time to parse TS with its default tooling (this is probably the most future-proof but also the most overhead, unexpected solution)?

@DavidGrath
Copy link
Contributor Author

Good Day, @joscha , thank you for your feedback,
These are my responses:

Can we use the TS parsing functionality itself - given this recently was ported to Go, could it be portable enough to be included here?

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

Maybe one of the bigger AST generators like acorn has a WASM implementation that we can load?

I don't know how WASM works, so I can't really speak to this

Is there a standard implementation of the lexer/parser part in this PR? Some widespread used Java package that has this functionality and is actively developed and we can lean on?

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

Is there a module that can load native JS in Java (Rhino comes to mind, but that is aeons old already) that we can use to load one of the AST generators and/or ts package itself inside java?

I don't know much about Rhino, but from what I've looked up, I don't see anything about it supporting TypeScript

We can expect a dependency on node and call out to it at generation time to parse TS with its default tooling

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

@joscha
Copy link
Contributor

joscha commented Jun 15, 2025

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.
All I am saying is that I wish we could reuse the actual typescript parsing logic or a package which is less low level than the grammar for the language we're trying to support.
Given this is in tests and we're talking TS, maybe we can safely assume node and ecosystem to be there?
I know that the CLI package also has a nix profile, which alleviates a bunch of issues with dependency management - maybe that's an option here, too?
@macjohnny @wing328 WDYT?

}
}

@Test(description = "Issue #21317")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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 inputSpec to the path for issue_21317.yaml
  • 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

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?

Copy link
Member

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

@DavidGrath
Copy link
Contributor Author

@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;
Copy link
Member

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");
Copy link
Member

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
Copy link
Member

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

@macjohnny macjohnny changed the title [typescript] ANTLR test enhancement. Utility types now considered when generating schemas (Issue #21317) [typescript] Utility types now considered when generating schemas (Issue #21317) Jun 18, 2025
Comment on lines 4 to 6
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

leftovers?

Copy link
Contributor Author

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(
Copy link
Contributor

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?

Copy link
Contributor Author

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",
Copy link
Contributor

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?

Copy link
Contributor Author

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

@joscha
Copy link
Contributor

joscha commented Jun 18, 2025

@joscha , I've removed ANTLR by the advice of macjohnny. Do you think this fixes the main issue?

yes, i think so :)

@DavidGrath
Copy link
Contributor Author

Okay. Thank you both. I'll revert by the weekend or sooner

@DavidGrath
Copy link
Contributor Author

@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 meta-codegen

Copy link
Member

@macjohnny macjohnny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@macjohnny
Copy link
Member

@DavidGrath please update the docs and the samples and merge the latest master branch.

@DavidGrath
Copy link
Contributor Author

@macjohnny Done

@macjohnny
Copy link
Member

@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

@DavidGrath
Copy link
Contributor Author

@macjohnny , please can you confirm? I should have already merged the 7.14.0 release with 600ad25 before I generated the docs

@macjohnny
Copy link
Member

@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

@DavidGrath
Copy link
Contributor Author

@macjohnny , okay, I'll try and revert between now and Friday

@DavidGrath
Copy link
Contributor Author

@macjohnny , sorry for the delay, I've merged and regenerated the 7.15-snapshot samples

@macjohnny macjohnny merged commit 0e67c3a into OpenAPITools:master Jun 29, 2025
14 checks passed
@wing328 wing328 added this to the 7.15.0 milestone Aug 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants