swagger-api / swagger-api/swagger-codegen
[typescript-angular] Swagger Angular client complains about missing HTTPClientModule even though it is being imported
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
I generated a TypeScript/Angular client for my backend's REST API using Swagger Codegen and am trying to use it in a completely new Angular project. Following the README file Swagger generated, I yarn linked the client's dist/ folder into my project's node_modules and updated my app.module.ts like so:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http'; // <-- Added this line
import { ApiModule } from '@my-project/rest-client'; // <-- and this one
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule, // <-- and these
ApiModule // <---------- two lines
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
The result:
Error: You need to import the HttpClientModule in your AppModule! See
also https://github.com/angular/angular/issues/20575
What gives?
Software versions
- Angular 5.2.10 (for both my project and the generated client)
- Angular CLI 1.7.4
- This snapshot of the upcoming Swagger Codegen 2.4.0
- TypeScript 2.5.3 (in my project) and 2.8.3 (in the generated client)
Swagger declaration file content or url
See this gist
Command line used for generation
SWAGGER_JAR=swagger-codegen-cli.jar
SWAGGER_JSON=swagger.json
SWAGGER_TARGET=rest-client
PACKAGE_NAME="@my-project/rest-client"
ANGULAR_VERSION="5.2.0"
java -jar $SWAGGER_JAR generate \
-i $SWAGGER_JSON \
-l typescript-angular \
-o $SWAGGER_TARGET \
--additional-properties npmName=$PACKAGE_NAME,snapshot=true,ngVersion=$ANGULAR_VERSION,npmRepository=SomeNonExistingNpmRepo
Steps to reproduce
- Use Swagger codegen to generate an npm package from the Swagger definition file linked above and build it.
- Create a new Angular project using
ng new - Use
yarn linkto add the Swagger client'sdist/folder as anode_moduleto said project. - Change the project's
app.module.tsas indicated above. - Run
ng servein the project's root folder.
Related issues/PRs
I couldn't find any.
Suggest a fix/enhancement
I wish I could come up with any suggestion. I already tried changing the TypeScript version of my Angular project from ~2.5.3 to ^2.5.3 (so that both the Swagger-generated client and my project are using the same TypeScript version) but, after a yarn install, this results in the following warning:
@angular/compiler-cli@5.2.10 requires typescript@'>=2.4.2 <2.7.0' but 2.8.3 was found instead. Using this version can result in undefined behaviour and difficult to debug problems.
Moreover, while ng serve does keep running despite the warning, the Swagger error message from above remains when pointing the browser to localhost:4200.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the problem using the linked Swagger declaration, the typescript-angular generation command, and a fresh Angular project, then inspect the generated README, dist/ package, and app.module.ts setup. Compare the generated client's Angular dependencies and module wiring with the reported Angular 5.2.10 error; done means identifying why HttpClientModule is reported missing and documenting a verified resolution.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- angular, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100