Skip to content

Conversation

aryobenholzner
Copy link
Contributor

@aryobenholzner aryobenholzner commented Jun 29, 2025

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

  1. Invoke openapi-generator
    openapi-generator-cli.jar generate -i petstore.yaml -g typescript-nestjs-server -o api-module/
    
  2. implement the contracts from api-module/api
    handlers/PetService.ts:
    import { Pet, ApiResponse } from "models";
    import { Observable } from "rxjs";
    import { PetApi } from "../api";
    import { Inject, Injectable } from "@nestjs/common";
    
    @Injectable()
    export class PetService implements PetApi {
      addPet(pet: Pet, request: Request): Pet | Promise<Pet> | Observable<Pet> {
        throw new Error("Method not implemented.");
      }
    
      deletePet(petId: number, apiKey: string, request: Request): void | Promise<void> | Observable<void> {
        throw new Error("Method not implemented.");
      }
    
    ...
  3. Import the generated ApiModule with ApiModule.forRoot and provide a instance of ApiImplementations with a reference to your implementation
    app.module.ts
    import { Module } from "@nestjs/common";
    import { ApiModule, ApiImplementations } from "api-module";
    import { PetService } from "./handlers/PetService";
    import { UserService } from "./handlers/UserService";
    import { StoreService } from "./handlers/StoreService";
    
    const apiImplementations: ApiImplementations = {
      petApi: PetService,
      userApi: UserService,
      storeApi: StoreService,
    }
    
    @Module({
      imports: [
        ApiModule.forRoot(apiImplementations),
      ],
      controllers: [],
      providers: [],
    })
    export class AppModule {}

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

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

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?

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java

so we could re-use a lot of the model/imports handling?

@@ -0,0 +1,21 @@
{
"compilerOptions": {
"module": "commonjs",
Copy link
Member

Choose a reason for hiding this comment

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

is this expected?

Copy link
Contributor Author

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

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

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 cleaned up package.mustache and parameterized all versions

export type ApiImplementations = {
{{#apis}}
{{#operations}}
{{#lambda.camelcase}}{{classname}}{{/lambda.camelcase}}: Type<{{classname}}>
Copy link
Member

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?

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 updated the description as well as the committed README

@wing328
Copy link
Member

wing328 commented Jun 30, 2025

thanks for the PR

suggestions:

@aryobenholzner
Copy link
Contributor Author

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!

@aryobenholzner aryobenholzner force-pushed the nestjs-server-codegen branch from 3f8373d to dc89dcc Compare July 11, 2025 19:52
@aryobenholzner aryobenholzner changed the title [Draft] NestJS server codegen NestJS server codegen Jul 11, 2025
@aryobenholzner aryobenholzner marked this pull request as ready for review July 11, 2025 20:21
@aryobenholzner aryobenholzner force-pushed the nestjs-server-codegen branch from 364d6b0 to 2f05e15 Compare July 11, 2025 20:41
@macjohnny
Copy link
Member

@aryobenholzner is this ready for review?

@aryobenholzner aryobenholzner force-pushed the nestjs-server-codegen branch from 721e96b to d9903d1 Compare July 25, 2025 15:29
@aryobenholzner
Copy link
Contributor Author

@macjohnny I think it is. This is a working first version and I think it would be best to add more features later on.

@wing328
Copy link
Member

wing328 commented Jul 28, 2025

@aryobenholzner can you please review the CI failure when you've time?

@wing328 wing328 added this to the 7.15.0 milestone Jul 28, 2025
@aryobenholzner
Copy link
Contributor Author

@wing328 it should be good now

@wing328
Copy link
Member

wing328 commented Jul 31, 2025

ok but https://github.com/OpenAPITools/openapi-generator/actions/runs/16632831156/job/47097653123?pr=21494 still failed.

please take a look when you've time.

@aryobenholzner
Copy link
Contributor Author

@wing328 the node version in the workflows were too old for this version of NestJS, should be fixed now. (unfortunately I can't seem to trigger these workflows myself)

@@ -115,6 +115,7 @@ elif [ "$NODE_INDEX" = "3" ]; then
(cd samples/client/petstore/javascript-flowtyped && mvn integration-test)
(cd samples/client/petstore/javascript-es6 && mvn integration-test)
(cd samples/client/petstore/javascript-promise-es6 && mvn integration-test)
(cd samples/server/petstore/typescript-nestjs-server && mvn integration-test)
Copy link
Member

Choose a reason for hiding this comment

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

does that run the same test as the github action? if so, it could be removed (can be done in a subsequent PR)

@macjohnny macjohnny merged commit fcc83db into OpenAPITools:master Jul 31, 2025
17 checks passed
@aryobenholzner aryobenholzner deleted the nestjs-server-codegen branch July 31, 2025 13:11
Goopher pushed a commit to Goopher/openapi-generator that referenced this pull request Sep 9, 2025
* setup basic codegen for nestjs server

* set up generated file structure

* adapted moustache files

* fixed imports

* adapted templates

* added module bootstrap

* added model generation

* fixed error with generic type

* added README

* added usage clarification to README, introduced module index.ts

* Update modules/openapi-generator/src/main/resources/typescript-nestjs-server/api.module.mustache

Co-authored-by: Esteban Gehring <[email protected]>

* cleaned up package.mustache, added parameters for versions

* cleaned up unneeded boilerplate templates

* fixed indentations from templates

* implemented useSingleRequestParameter

* fixed parameter handling

* added samples with tests

* added docs

* fixed samples

* corrected docs

* updated docs after merge of master

* fixed samples build

* fixed workflow

---------

Co-authored-by: Esteban Gehring <[email protected]>
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.

3 participants