Reputation:
I am generating https://editor.swagger.io/ Codegen proxies. It is giving the following error in Angular 10. How can this be fixed?
Generic type 'ModuleWithProviders' requires 1 type argument(s).
export class ApiModule {
public static forRoot(configurationFactory: () => Configuration): ModuleWithProviders {
return {
ngModule: ApiModule,
providers: [ { provide: Configuration, useFactory: configurationFactory } ]
};
}
constructor( @Optional() @SkipSelf() parentModule: ApiModule,
@Optional() http: HttpClient) {
if (parentModule) {
throw new Error('ApiModule is already loaded. Import in your base AppModule only.');
}
if (!http) {
throw new Error('You need to import the HttpClientModule in your AppModule! \n' +
'See also https://github.com/angular/angular/issues/20575');
}
}
}
Upvotes: 9
Views: 3599
Reputation: 1312
io.swagger.swagger-codegen
fully supports Angular 10 since version 2.4.17. In detail you can se the fix which was provided in this pull request.
As djf mentioned use the parameter --additional-properties ngVersion=10
when calling generate
to tell io.swagger.swagger-codegen
to use Angular 10.
Upvotes: 3
Reputation: 6757
We stumbled over the same issue. Luckily you can provide custom *.mustache templates to the swagger-codegen-cli using the -t
switch (see the swagger doc).
Our adapted api.module.mustache looks like this:
import { NgModule, ModuleWithProviders, SkipSelf, Optional } from '@angular/core';
import { Configuration } from './configuration';
{{#useHttpClient}}import { HttpClient } from '@angular/common/http';{{/useHttpClient}}
{{^useHttpClient}}import { Http } from '@angular/http';{{/useHttpClient}}
{{#apiInfo}}
{{#apis}}
import { {{classname}} } from './{{importPath}}';
{{/apis}}
{{/apiInfo}}
@NgModule({
imports: [],
declarations: [],
exports: [],
providers: [
{{#apiInfo}}{{#apis}}{{classname}}{{#hasMore}},
{{/hasMore}}{{/apis}}{{/apiInfo}} ]
})
export class ApiModule {
public static forRoot(configurationFactory: () => Configuration): ModuleWithProviders<ApiModule> {
return {
ngModule: ApiModule,
providers: [ { provide: Configuration, useFactory: configurationFactory } ]
};
}
constructor( @Optional() @SkipSelf() parentModule: ApiModule,
@Optional() http: {{#useHttpClient}}HttpClient{{/useHttpClient}}{{^useHttpClient}}Http{{/useHttpClient}}) {
if (parentModule) {
throw new Error('ApiModule is already loaded. Import in your base AppModule only.');
}
if (!http) {
throw new Error('You need to import the {{#useHttpClient}}HttpClientModule{{/useHttpClient}}{{^useHttpClient}}HttpModule{{/useHttpClient}} in your AppModule! \n' +
'See also https://github.com/angular/angular/issues/20575');
}
}
}
The only change needed is to replace ModuleWithProviders
by ModuleWithProviders<ApiModule>
.
Afterwards the generated api.module.ts will work with Angular 10.
Upvotes: 2
Reputation: 885
this error tells you that, the ModuleWithProviders
class has 1 generic paramter. so you sould use it like ModuleWithProviders<T>
where T is a type.
EDIT:
The class is defined like this:
interface ModuleWithProviders<T> {
ngModule: Type<T>
providers?: Provider[]
}
.
export class ApiModule {
public static forRoot(configurationFactory: () => Configuration) : ModuleWithProviders<ApiModule> {
return {
ngModule: ApiModule,
providers: [ { provide: Configuration, useFactory: configurationFactory } ]
};
}
See Resource:
https://angular.io/guide/migration-module-with-providers
Upvotes: 3