[BUG] [typescript-angular] Missing handling of text responses
Created by: Fredx87
Description
When the returned content type of an API is a text type (e.g. text/plain
), the Angular generated service doesn't set the responseType: text
option (as shown in Angular documentation: https://angular.io/guide/http#requesting-non-json-data)
openapi-generator version
3.3.4
OpenAPI declaration file content or url
openapi: 3.0.0
info:
title: MyAPI
description: MY API
version: 0.0.0
servers:
- url: http://localhost:8080/api/
paths:
/example:
get:
operationId: getExample
responses:
"200":
description: "Success"
content:
text/plain:
schema:
type: string
Command line used for generation
`docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v3.3.4 generate -i /local/swagger.yaml -g typescript-angular\ -o /local/src/app/api\ -DngVersion=6.0.0
Suggest a fix
This is the generated service:
public getExample(observe?: 'body', reportProgress?: boolean): Observable<string>;
public getExample(observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<string>>;
public getExample(observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<string>>;
public getExample(observe: any = 'body', reportProgress: boolean = false ): Observable<any> {
let headers = this.defaultHeaders;
// to determine the Accept header
let httpHeaderAccepts: string[] = [
'text/plain'
];
const httpHeaderAcceptSelected: string | undefined = this.configuration.selectHeaderAccept(httpHeaderAccepts);
if (httpHeaderAcceptSelected !== undefined) {
headers = headers.set('Accept', httpHeaderAcceptSelected);
}
// to determine the Content-Type header
const consumes: string[] = [
];
return this.httpClient.get<string>(`${this.configuration.basePath}/example`,
{
withCredentials: this.configuration.withCredentials,
headers: headers,
observe: observe,
reportProgress: reportProgress
}
);
}
Instead the generator should call the get method with this format:
return this.httpClient.get(`${this.configuration.basePath}/example`,
{
withCredentials: this.configuration.withCredentials,
headers: headers,
observe: observe,
reportProgress: reportProgress,
responseType: 'text'
}
);
Without the setting of this option. the http client will return an error instead of the text content of the called API