Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #7083
Closed
Open
Issue created Jul 30, 2020 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

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

Created by: badsyntax

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?

Assignee
Assign to
Time tracking