OpenAPITools / OpenAPITools/openapi-generator

Request body string is placed in quotation marks

Open
#10,362 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

When transferring data in the requestBody with content-Type != 'Application / x-www-form-urlencoded' and 'multipart / form-data', each string is placed in quotation marks and all quotation marks inside the string are escaped with ".

The problem arises in the function ApiClient.serialize where the string is put in quotation marks for almost all content types. This means that no plain text, XML, etc. transmission is possible.

openapi-generator version

5.2, master

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: My-API
  description: Post XML-String
  version: 1.0.0
tags:
  - name: products


servers:
  - url: /api
        
paths:
  /products:
    post:
      operationId: importProduct
      parameters:
        - name: referenceId
          required: false
          in: query
          schema:
            type: string
      requestBody:
        required: true
        content:
          text/plain; charset=utf-8:
            schema:
              type: string
      responses:
        '201':
          description: Returns the created Product
          content:
            text/plain; charset=utf-8:
              schema:
                type: string
      tags:
        - products

Command line used for generation

generate -i api.yaml -g java --library jersey2 -o

Related issues/PRs

#6956
#6954

Suggest a fix/enhancement

Modification of the file https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache so that this code is created:

public Entity<?> serialize(Object obj, Map<String, Object> formParams, String contentType, boolean isBodyNullable) throws ApiException {
  if (contentType.startsWith("multipart/form-data")) {
  [...]
  } else if (isJsonMime(contentType)) {
      String nullValue = isBodyNullable ? "null" : "";
      if (obj instanceof String) {
        entity = Entity.entity(obj == null ? nullValue : "\"" + ((String)obj).replaceAll("\"", Matcher.quoteReplacement("\\\"")) + "\"", contentType);
      } else {
        entity = Entity.entity(obj == null ? "null" : obj, contentType);
      }
    } else {
        entity = Entity.entity(obj == null ? "" : obj, contentType);
    }
    return entity;
  }

So the changes made in #6956 only applies to json and not to other contenttypes like plaintext, xml etc.

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/Java/libraries/jersey2/ApiClient.mustache and inspect the serialize method alongside related issues #6956 and #6954. Verify how string bodies are handled for JSON versus other content types; done means plain-text and XML request bodies are transmitted without added quotation marks while JSON behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
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.