openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][Java] Enum values get JSON encoded in multipart/form-data requests

Open Tiim opened this issue 1 year ago • 5 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [ ] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Enum values in multipart/form-data requests get wrapped in double quotes, instead of being included in the request as is. This seems due to the following code in the generated ApiClient.java file: The enum gets JSON encoded, which results in the string "MY_ENUM" instead of the raw enum value MY_ENUM.

    /**
     * Add a Content-Disposition Header for the given key and complex object to the MultipartBody Builder.
     *
     * @param mpBuilder MultipartBody.Builder
     * @param key The key of the Header element
     * @param obj The complex object to add to the Header
     */
    private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, Object obj) {
        RequestBody requestBody;
        if (obj instanceof String) {
            requestBody = RequestBody.create((String) obj, MediaType.parse("text/plain"));
        } else {
            String content;
            if (obj != null) {

// TODO: error is here. The object is an enum, which should NOT be JSON encoded!

                content = JSON.serialize(obj);
            } else {
                content = null;
            }
            requestBody = RequestBody.create(content, MediaType.parse("application/json"));
        }

        Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"");
        mpBuilder.addPart(partHeaders, requestBody);
    }

Example:

Generated POST request
POST /document HTTP/1.1
Accept: application/json
User-Agent: OpenAPI-Generator/2.16.0/java
Content-Type: multipart/form-data; boundary=79672ee3-fe3f-431d-802a-44a281963e66
Content-Length: 368
Host: localhost:3000
Connection: Keep-Alive
Accept-Encoding: gzip

--79672ee3-fe3f-431d-802a-44a281963e66
Content-Disposition: form-data; name="source_lang"
Content-Type: application/json; charset=utf-8
Content-Length: 4

"DE"
--79672ee3-fe3f-431d-802a-44a281963e66--
Expected POST request
POST /document HTTP/1.1
Accept: application/json
User-Agent: OpenAPI-Generator/2.16.0/java
Content-Type: multipart/form-data; boundary=79672ee3-fe3f-431d-802a-44a281963e66
Content-Length: 368
Host: localhost:3000
Connection: Keep-Alive
Accept-Encoding: gzip

--79672ee3-fe3f-431d-802a-44a281963e66
Content-Disposition: form-data; name="source_lang"
Content-Length: 2

DE
--79672ee3-fe3f-431d-802a-44a281963e66--
openapi-generator version

2.16.0

OpenAPI declaration file content or url

See Gist for a full example: https://gist.github.com/Tiim/65bdea69e5c381582fae6fd17dbdc80f

Generation Details
# codegen
mvn clean compile

# run debug server
npx http-echo-server
Steps to reproduce
  • Create a new empty folder
  • Copy the files from the Gist into the respective subfolders in this folder:
    • src/main/java/org/example/Main.java
    • src/main/openapi/openapi.yaml
    • pom.xml
  • Run mvn clean compile
  • Set a breakpoint in target/generated-sources/openapi/src/main/java/ch/example/ApiClient.java:1450
    • private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, Object obj) {
  • Run src/main/java/org/example/Main.java in debug mode
Related issues/PRs
Suggest a fix
  • Check if the Object is a generated enum. In this case, use obj.getValue() (which gets generated by the codegen) instead of JSON.serialize().

Temporary workaround

While creating the minimal example for this bug report, I found that when the enum is declared inline in the request, not in the schemas section, it is not turned into a java enum.

Tiim avatar Oct 25 '24 14:10 Tiim

I have the same issue.

jarryDk avatar May 19 '25 08:05 jarryDk

I have the same issue 👍

Springjunky avatar May 23 '25 06:05 Springjunky

Idem here :(

mike-963 avatar Jun 03 '25 11:06 mike-963

I have created this PR https://github.com/OpenAPITools/openapi-generator/pull/21373

@Tiim Thank for pointing me in the right direction.

jarryDk avatar Jun 04 '25 06:06 jarryDk

I have an idea of it being Spring to blame for error using the RestClient -> https://github.com/spring-projects/spring-framework/blob/main/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java#L508

jarryDk avatar Jun 10 '25 06:06 jarryDk