Openapi-generator: [BUG][typescript-inversify] Type mismatch between service model and HttpClient requirement

Created on 12 Aug 2019  路  9Comments  路  Source: OpenAPITools/openapi-generator

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] What's the version of OpenAPI Generator used?
  • [x] Have you search for related issues/PRs?
  • [ ] What's the actual output vs expected output?
  • [ ] [Optional] Bounty to sponsor the fix (example)
Description

When using typescript in strict mode, the code won't compile if your spec has an optional requestBody because the HttpClient defines body as {} which means 'any except null or undefined'

openapi-generator version

4.1.0

Steps to reproduce

Generated service method:

    public apiAccountLoginPost(loginViewModel?: LoginViewModel, observe?: 'body', headers?: Headers): Promise<LoginViewModelResult>;
    public apiAccountLoginPost(loginViewModel?: LoginViewModel, observe?: 'response', headers?: Headers): Promise<HttpResponse<LoginViewModelResult>>;
    public apiAccountLoginPost(loginViewModel?: LoginViewModel, observe: any = 'body', headers: Headers = {}): Promise<any> {

HttpClient:

    post(url: string, body: {}|FormData, headers?: Headers): Observable<HttpResponse> {
        return this.performNetworkCall(url, "POST", this.getJsonBody(body), this.addJsonHeaders(headers));
    }

Error:

image

Related issues/PRs

3607

Suggest a fix

I believe changing the {} to any in the HttpClient will resolve this, however I don't know enough about typescript to understand the impact this may have

I'm happy to organise the PR for this if you agree with this change

TypeScript Bug

Most helpful comment

@deluksic I already fixed that in PR #3607 this issue has been opened as a discussion from that

All 9 comments

馃憤 Thanks for opening this issue!
馃彿 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@gualtierim what is your opinion?

For handling the undefined/null values should getJsonBody return null, empty string, or {} ?

For handling the undefined/null values should getJsonBody return null, empty string, or {} ?

good question, I guess undefined ?

I agree to change the signature of httpClient with Object|FormData. getJsonBody should return undefined, I will try it the next week

Also, addJsonHeaders's headers parameter should be optional.

@deluksic I already fixed that in PR #3607 this issue has been opened as a discussion from that

@gualtierim Did you manage to get around to trying the solution out yet?

Was this page helpful?
0 / 5 - 0 ratings