OpenAPITools/openapi-generator

[BUG] [typescript-fetch] quotes added to text/plain POST body

badsyntax opened this issue · 5 comments

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • 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

I have an endpoint definition for a POST request that defines the requestBody as being text/plain.

typescript-fetch will JSON.stringify() the body thus adding quotes around the body value. This is a bug, my body value should have not have quotes around it.

openapi-generator version

openapi-generator-cli 4.3.1

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Example
  version: 1.0.0
paths:
  /example:
    post:
      operationId: postExample
      requestBody:
        required: true
        content:
          text/plain:
            schema:
              type: string
      responses:
        200:
          description: Successful operation
          content:
            text/html:
              schema:
                type: string
  
Command line used for generation
openapi-generator generate -i schema/myschema.yml  "-g" "typescript-fetch"  "-o" "clients/typescript-fetch" "--additional-properties=typescriptThreePlus=true,enablePostProcessFile=true" "--generate-alias-as-model"
Steps to reproduce

You'll need to generate the typescript-fetch files using the schema and cli command above.

Related issues/PRs

I couldn't find any similar issues.

Suggest a fix

I suggest reading the "Content-Type" from the request headers to determine when to JSON.stringify() the body:

--- a/templates/typescript-fetch/runtime.mustache
+++ b/templates/typescript-fetch/runtime.mustache
@@ -50,7 +50,7 @@ export class BaseAPI {
             // do not handle correctly sometimes.
             url += '?' + this.configuration.queryParamsStringify(context.query);
         }
-        const body = ((typeof FormData !== "undefined" && context.body instanceof FormData) || context.body instanceof URLSearchParams || isBlob(context.body))
+        const body = ((typeof FormData !== "undefined" && context.body instanceof FormData) || context.body instanceof URLSearchParams || isBlob(context.body)) || context.headers['Content-Type'] !== 'application/json'
            ? context.body
            : JSON.stringify(context.body);

If you are happy with the above change, I can send a PR. What do you think?

@badsyntax sounds good, go ahead

I ran into this issue today. It seems the mentioned PR did not arrive?

@LostMekka sorry I have not had time to land the PR. Do you want to send the PR using the suggested changes?

done :)

Is still opened, why?