-
-
Notifications
You must be signed in to change notification settings - Fork 7k
[Draft] NestJS server codegen #21494
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
base: master
Are you sure you want to change the base?
[Draft] NestJS server codegen #21494
Conversation
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 this new generator!
import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER; | ||
import static org.openapitools.codegen.utils.StringUtils.*; | ||
|
||
public class TypeScriptNestjsServerCodegen extends AbstractTypeScriptClientCodegen { |
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.
@TiFu @bodograumann would it make sense to base this here on the TypeScriptClientCodegen?
so we could re-use a lot of the model/imports handling?
modules/openapi-generator/src/main/resources/typescript-nestjs-server/README.md
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/typescript-nestjs-server/api.module.mustache
Outdated
Show resolved
Hide resolved
modules/openapi-generator/src/main/resources/typescript-nestjs-server/package.mustache
Outdated
Show resolved
Hide resolved
@@ -0,0 +1,21 @@ | |||
{ | |||
"compilerOptions": { | |||
"module": "commonjs", |
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.
is this expected?
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 was created like this from the scaffolding from the Nest CLI. I'm not sure if it would work correctly for all projects if we change it to esNext
. I don't think there would be any downsides if we keep it as commonjs
. What do you think?
"test:e2e": "jest --config ./test/jest-e2e.json" | ||
}, | ||
"dependencies": { | ||
"@nestjs/common": "^11.0.1", |
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 use the nest version variable like in
openapi-generator/modules/openapi-generator/src/main/resources/typescript-nestjs/package.mustache
Line 32 in a5f638f
"@nestjs/common": "^{{nestVersion}}", |
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 cleaned up package.mustache
and parameterized all versions
modules/openapi-generator/src/main/resources/typescript-nestjs-server/variables.mustache
Outdated
Show resolved
Hide resolved
@@ -0,0 +1,13 @@ | |||
import { Injectable } from '@nestjs/common'; |
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 also add samples like in https://github.com/OpenAPITools/openapi-generator/blob/master/bin/configs/typescript-angular-v16-provided-in-root.yaml
and add unit-tests like in https://github.com/OpenAPITools/openapi-generator/tree/master/samples/client/petstore/typescript-angular-v16-provided-in-root/tests/default/src and register like in
openapi-generator/CI/circle_parallel.sh
Line 90 in a5f638f
(cd samples/client/petstore/typescript-angular-v16-provided-in-root && mvn integration-test) |
this.cliOptions.add(new CliOption(MODEL_FILE_SUFFIX, "The suffix of the file of the generated model (model<suffix>.ts).")); | ||
this.cliOptions.add(new CliOption(FILE_NAMING, "Naming convention for the output files: 'camelCase', 'kebab-case'.").defaultValue(this.fileNaming)); | ||
this.cliOptions.add(new CliOption(STRING_ENUMS, STRING_ENUMS_DESC).defaultValue(String.valueOf(this.stringEnums))); | ||
this.cliOptions.add(new CliOption(USE_SINGLE_REQUEST_PARAMETER, "Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.").defaultValue(Boolean.FALSE.toString())); |
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.
this isnt used right now, right?
i would suggest to set it to true by default, it will avoid clashes in the generated code with variables.
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.
No, it's not used indeed. Shall it really be kept instead of removed?
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.
inwould suggest to implement it ;)
export type ApiImplementations = { | ||
{{#apis}} | ||
{{#operations}} | ||
{{#lambda.camelcase}}{{classname}}{{/lambda.camelcase}}: Type<{{classname}}> |
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.
how is the implementer supposed to use this? can you clarify that in the PR description?
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 updated the description as well as the committed README
thanks for the PR suggestions:
|
Thanks a lot for the detailed feedback! I’m glad to see the comments coming in. Apologies for any messiness at this stage. I wanted to get early feedback before spending time polishing things. There are probably a few things left from the initial NestJS scaffolding or generator code that doesn't serve any purpose here. From what I gather, the overall approach seems acceptable. I’ll continue building on this and refining it based on your input. Let me know if anything fundamental needs to change! |
…-server/api.module.mustache Co-authored-by: Esteban Gehring <[email protected]>
proposed solution for #11785
This is a prototype of a server code generator for NestJS. The code probably needs improvement, I'm relatively new in this codebase and tried to adhere to existing generators. As of now, I only want to gather feedback on the solution the generated output provides, and whether it's worth continuing this approach.
Description a per the added README
OpenApi Generator typescript-nestjs-server
Usage: The generated output is intended to be its own module, that can be imported into your NestJS App Module. You do not need to change generated files, just import the module and implement the API
Example usage (with the openapi sample
petstore.yaml
):api-module/api
handlers/PetService.ts
:ApiModule
withApiModule.forRoot
and provide a instance ofApiImplementations
with a reference to your implementationapp.module.ts
You now can regenerate the API module as often as you want without overriding your implementation.
@TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo (2019/11) @amakhrov @davidgamero @mkusaka @joscha