OpenAPITools / OpenAPITools/openapi-generator

[BUG] javascript-flowtype and typescript-axios detect JSON POST body using case-sensitive header checking

Open
#6,571 1 comment 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?
  • [N/A] Have you validated the input using an OpenAPI validator (example)?
  • [5.0.0-SNAPSHOT] What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

For the javascript-flowtype and typescript-axios generators, a POST request body is automatically passed into JSON.stringify() if the "Content-Type" header is "application/json." Sounds fine, but lets say you're overriding the content-type to something else like "multipart/formdata" by passing a generated header such as the one from the form-data package and it's all-lowercase - it doesn't match! The result is that your formdata (which is supposed to be plain text) gets JSON.stringified into a JSON string.

I don't think HTTP headers are supposed to be case-sensitive so while it might be technically a bit more expensive, I think the code ought to do a case-insensitive detection.

openapi-generator version

5.0.0-SNAPSHOT

OpenAPI declaration file content or url
  "paths": {
    "/some/post": {
      "post": {
        "operationId": "somePush",
        "requestBody": {
          "description": "post something",
          "content": {
            "*/*": {
              "schema": {
                "type": "string",
                "format": "binary"
              }
            }
          }
        }
    }
  }
  // in node.js
  const form = new FormData();
  await myApi.somePush(
    {
      body: form.getBuffer().toString()
    },
    {
      headers: form.getHeaders()
    }
  );
Command line used for generation
generate  -i swagger.json -g typescript-axios
Steps to reproduce
Related issues/PRs
Suggest a fix
- const needsSerialization = (typeof {{paramName}} !== "string") || localVarRequestOptions.headers['Content-Type'] === 'application/json';
+let needsSerialization =
+  typeof {{paramName}} !== "string" ||
+  Object.keys(localVarRequestOptions.headers)
+    .filter(headerKey => headerKey.toLowerCase() === "content-type")
+    .some(headerKey => localVarRequestOptions.headers[headerKey] === "application/json");

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 with modules/openapi-generator/src/main/resources/Javascript-Flowtyped/api.mustache and modules/openapi-generator/src/main/resources/typescript-axios/apiInner.mustache at the referenced request-body handling. Generate the client with the shown command and reproduce the form-data request using lowercase headers. Done means both generators recognize the Content-Type header regardless of its casing and preserve the non-JSON body.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
devtools, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.