OpenAPITools / OpenAPITools/openapi-generator

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

Open
#7,083 5 comments 3 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)?
  • 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?

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 templates/typescript-fetch/runtime.mustache and reproduce the generated client using the provided OpenAPI YAML and command. Verify that a text/plain string POST body is sent without added quotes while JSON bodies retain their expected encoding. The issue does not name a test path.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.