openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [typescript-nestjs-server] Cannot use Injectables in ApiImplementations

Open Pranen opened this issue 1 year ago • 3 comments

Description

If I want to implement a functionality in an ApiInterface i cannot inject services from the AppModule because they are not known to the ApiModule which is creating all those instances.

openapi-generator version

7.15

Steps to reproduce

Take the PetStore-Demo and add a TestService which is called from the UserService in the constructor:

@Injectable() export class TestService { hello(): string { return "Hello World"; } }

`@Injectable() export class UserService implements UserApi {

constructor(private readonly testService: TestService) {}`

If you do that, you'll receive an error in the startup:

Nest can't resolve dependencies of the UserService (?). Please make sure that the argument TestService at index [0] is available in the ApiModule context

Pranen avatar Aug 29 '25 12:08 Pranen

There are 3 possible solutions for fixing this problem:

  • @Global on the module where the service are that you want to use
  • Add in the Module-Template of your integration the import of the needed module
  • Use the moduleRef in the constructor and bind the service in the onModuleInit-Phase:
constructor(private readonly moduleRef: ModuleRef) {}

onModuleInit(): any {
        this.testService = this.moduleRef.get(TestService, {strict: false});
}

I used the moduleRef.

Pranen avatar Nov 08 '25 10:11 Pranen

cc @aryobenholzner

wing328 avatar Nov 08 '25 13:11 wing328

Thanks for the ping, I will look into this

aryobenholzner avatar Nov 19 '25 07:11 aryobenholzner