OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.71k stars 6.32k forks source link

Request body string is placed in quotation marks #10362

Open Malte-Neu opened 2 years ago

Malte-Neu commented 2 years ago
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.

ChristianCiach commented 1 year ago

This also makes it impossible to just send a JSON-formatted String as-is to the server. The JSON-String now always gets double-encoded. Unfortunately there doesn't seem to be workaround other than to extend the ApiClient and overriding the serialize method with a saner implementation.

deki-ch commented 1 year ago

@wing328 This problem is really a blocker for us (if you don't consider workaround with ApiClient extend). Is there a possiblity to priorize this issue and solve it?

wing328 commented 1 year ago

@deki-ch would you like to file a PR to fix the issue or sponsor the fix?