Skip to content

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

Open

Description

@benwiley4000

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");

Activity

  1. benwiley4000 commented on Jun 5, 2020

    @benwiley4000
    Author

    BTW Yes I tried explicitly setting content type in the spec file as an alternative, but for some reason this removed the body argument from my API, so I went back to */*.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions