OpenAPITools / OpenAPITools/openapi-generator

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

Open
#13,028 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • 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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at common.ts line 117 in the typescript-axios generator and compare its serialization behavior with the TypeScript-Fetch implementation mentioned in the issue. Verify that an undefined body remains undefined and that null remains null, then run the relevant generator tests if available.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.