Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular 4+ TypeScript coge generator with HttpClient is broken if post / put / patch don't have body #6649

Closed
pgrm opened this issue Oct 9, 2017 · 8 comments · Fixed by #6754

Comments

@pgrm
Copy link
Contributor

pgrm commented Oct 9, 2017

Description

Looking at the lines

{{#useHttpClient}}
return this.httpClient.{{httpMethod}}{{^isResponseFile}}<any>{{/isResponseFile}}(`${this.basePath}{{{path}}}`, {{#bodyParam}}{{paramName}}, {{/bodyParam}}{{#hasFormParams}}formParams, {{/hasFormParams}}{
{{#hasQueryParams}}
params: queryParameters,
{{/hasQueryParams}}
headers: headers,
{{#isResponseFile}}
responseType: "blob",
{{/isResponseFile}}
withCredentials: this.configuration.withCredentials,
});
{{/useHttpClient}}

If no body and form params are defined in put or post (which is possible), the generated code looks like this:

return this.httpClient.put<any>(`${this.basePath}/some-url/${encodeURIComponent(String(id))}`, {
            headers: headers,
            withCredentials: this.configuration.withCredentials
        });

which causes

{  
  headers: headers,
  withCredentials: this.configuration.withCredentials
}

to be treated and sent as the body. This means a body, which isn't expected by the server, is sent and headers, which are expected, are not sent.

Swagger-codegen version

Master branch, latest commit I have is bf74e33

Related issues/PRs

This is when the bug was introduced - #6295

Suggest a fix/enhancement

I'm not sure what would be the best way to fix it. For the template it would be easiest to just have always some default body param if necessary, which would be simply null, for the Java backend code it would be nicer to solve it simply on the template, but since you need a check if it's a PATCH, PUT or POST operation, I don't see how to do it in mustache. Maybe introducing a flag like expectsBody?

@macjohnny
Copy link
Contributor

@pgrm can you check whether this is still an issue with #6574?

@pgrm
Copy link
Contributor Author

pgrm commented Oct 10, 2017

@macjohnny I've just now used the latest master to generate my new client, and the issue is still there

@macjohnny
Copy link
Contributor

@pgrm I will try to create a fix and open a new PR

@pgrm
Copy link
Contributor Author

pgrm commented Oct 10, 2017

@macjohnny FYI, this is how I solved it for now - apaleo@57cf9b6 (I can see that I forgot to update the comment 😉 but it seems to be working)

@macjohnny
Copy link
Contributor

@pgrm glad to see you found a fix. could you please open a new PR with your changes and generate the samples? I guess you should also be aware of #6496, which will hopefully be merged soon.

@topce
Copy link
Contributor

topce commented Oct 19, 2017

Hi @pgrm @macjohnny
Any news on this one ,
IMO it is important bug fix

@pgrm
Copy link
Contributor Author

pgrm commented Oct 19, 2017

@topce I'll submit a PR later today

@macjohnny
Copy link
Contributor

@pgrm I already filed a PR with your changes and some indentation-improvements here: #6754

wing328 pushed a commit that referenced this issue Oct 24, 2017
* fix empty body issue

* #6649: add line break

* #6649: improve indentation

* #6649: improve indentation

* #6649: generate code

* #6649: generate samples

* #6649: fix method docs
@wing328 wing328 added this to the v2.3.0 milestone Oct 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants