openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][typescript-axios] Body is sent as {}(empty object) after serializing when body is actually undefined

Open rdx-rockstar opened this issue 3 years ago • 1 comments

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] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
Description

While using a typescript-axios generator generated client the body is sent as {}(empty object) after serializing when the body is undefined. In common.ts file at line 117, while serializing the body:

export const serializeDataIfNeeded = function (value: any, requestOptions: any, configuration?: Configuration) {
    const nonString = typeof value !== 'string';
    const needsSerialization = nonString && configuration && configuration.isJsonMime
        ? configuration.isJsonMime(requestOptions.headers['Content-Type'])
        : nonString;
    return needsSerialization
        ? JSON.stringify(value !== undefined ? value : {})
        : (value || "");
}

here in the last third line, we can see that it makes the body as {} when it's undefined. In Typescript-Fetch this issue is fixed it sends body as undefined if it's undefined and body as null if it's null.

openapi-generator version

version 6.0.1 and below

Suggest a fix

It can be fixed by replacing line number 117 of common.ts file from ? JSON.stringify(value !== undefined ? value : {}) to ? JSON.stringify(value)

rdx-rockstar avatar Jul 27 '22 07:07 rdx-rockstar

I have the same issue

ahl-trifork avatar Sep 15 '22 06:09 ahl-trifork

you can skip serialization, set Content-Type in header, but it doesn't work in real request , still empty

kongsiyu avatar Dec 12 '22 08:12 kongsiyu

you can skip serialization, set Content-Type in header, but it doesn't work in real request , still empty

OK, 'Content-Type' + 'FormData' , it's working now:

  1. set Content-Type' { headers: { 'Content-Type': ContentTypeEnum.FORM_DATA } }

2.append file param to formData: const formData = new FormData(); formData.append('file', rawFile);

kongsiyu avatar Dec 12 '22 09:12 kongsiyu