Skip to content

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

Closed
@pgrm

Description

@pgrm
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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions