Using the npm package "@openapitools/openapi-generator-cli": "^2.4.25",
I get an Json-Parse
-Error when my api returns status code 204 (without body).
Having a look into the generated code the return new runtime.JSONApiResponse(response, (jsonValue) => TaskFromJSON(jsonValue));
is always called for all responded status codes.
How can I fix this without changing the generated code?
async getTasksRaw(requestParameters: GetTaskByIdRequest, initOverrides?: RequestInit): Promise<runtime.ApiResponse<Task>> {
if (requestParameters.taskId === null || requestParameters.taskId === undefined) {
throw new runtime.RequiredError('taskId','Required parameter requestParameters.taskId was null or undefined when calling getTaskById.');
}
const queryParameters: any = {};
const headerParameters: runtime.HTTPHeaders = {};
const response = await this.request({
path: `/tasks/{taskId}`.replace(`{${"taskId"}}`, encodeURIComponent(String(requestParameters.taskId))),
method: 'GET',
headers: headerParameters,
query: queryParameters,
}, initOverrides);
return new runtime.JSONApiResponse(response, (jsonValue) => TaskFromJSON(jsonValue));
}
Ok, currently I fixed it by the following middleware:
import { HttpExceptionFromJSON, Middleware, ResponseContext } from './my-client';
/**
* Workaround
*/
export default class NoContentMiddleware implements Middleware {
public post?(context: ResponseContext): Promise<Response | void> {
if (context.response.status === 204) {
throw {
status: 204
};
}
return Promise.resolve(context.response);
}
}
Then I inject the middleware to the client:
const config = new Configuration({
basePath: apiGateway + '/api/myapi',
middleware: [new NoContentMiddleware()]
});
using the client:
public async test(){
try{
var myentity = await this.callAPI();
catch(e:any){
switch(e.status){
case 204:
console.log("no content");
break;
default:
console.log("unexpected error");
}
}
}