-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Closed
Labels
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)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- 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-serverSteps 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.javasrc/main/openapi/openapi.yamlpom.xml
- Run
mvn clean compile - Set a breakpoint in
target/generated-sources/openapi/src/main/java/ch/example/ApiClient.java:1450private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, Object obj) {
- Run
src/main/java/org/example/Main.javain 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 ofJSON.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.