Skip to content

Commit b2b59ac

Browse files
committed
[Java] Fix content for enum with restclient (#19973)
1 parent fae1189 commit b2b59ac

File tree

50 files changed

+371
-222
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+371
-222
lines changed

samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,16 +1547,13 @@ protected void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String
15471547
RequestBody requestBody;
15481548
if (obj instanceof String) {
15491549
requestBody = RequestBody.create((String) obj, MediaType.parse("text/plain"));
1550+
} else if (obj == null){
1551+
requestBody = RequestBody.create(obj, MediaType.parse("application/json"));
1552+
} else if (obj.getClass().getEnumConstants() != null) {
1553+
requestBody = RequestBody.create(obj.toString(), MediaType.parse("text/plain"));
15501554
} else {
1551-
String content;
1552-
if (obj != null) {
1553-
content = JSON.serialize(obj);
1554-
} else {
1555-
content = null;
1556-
}
1557-
requestBody = RequestBody.create(content, MediaType.parse("application/json"));
1555+
requestBody = RequestBody.create(JSON.serialize(obj), MediaType.parse("application/json"));
15581556
}
1559-
15601557
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"");
15611558
mpBuilder.addPart(partHeaders, requestBody);
15621559
}

samples/client/echo_api/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,16 +1477,13 @@ protected void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String
14771477
RequestBody requestBody;
14781478
if (obj instanceof String) {
14791479
requestBody = RequestBody.create((String) obj, MediaType.parse("text/plain"));
1480+
} else if (obj == null){
1481+
requestBody = RequestBody.create(obj, MediaType.parse("application/json"));
1482+
} else if (obj.getClass().getEnumConstants() != null) {
1483+
requestBody = RequestBody.create(obj.toString(), MediaType.parse("text/plain"));
14801484
} else {
1481-
String content;
1482-
if (obj != null) {
1483-
content = JSON.serialize(obj);
1484-
} else {
1485-
content = null;
1486-
}
1487-
requestBody = RequestBody.create(content, MediaType.parse("application/json"));
1485+
requestBody = RequestBody.create(JSON.serialize(obj), MediaType.parse("application/json"));
14881486
}
1489-
14901487
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"");
14911488
mpBuilder.addPart(partHeaders, requestBody);
14921489
}

samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,18 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth
639639
addCookiesToRequest(cookieParams, requestBuilder);
640640
addCookiesToRequest(defaultCookies, requestBuilder);
641641

642+
if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) {
643+
formParams.forEach(
644+
(k, v) -> {
645+
if (v instanceof java.util.ArrayList) {
646+
Object o = v.get(0);
647+
if (o != null && o.getClass().getEnumConstants() != null) {
648+
v.set(0, o.toString());
649+
}
650+
}
651+
});
652+
}
653+
642654
var selectedBody = selectBody(body, formParams, contentType);
643655
if (selectedBody != null) {
644656
requestBuilder.body(selectedBody);

samples/client/others/java/okhttp-gson-oneOf-array/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,16 +1451,13 @@ protected void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String
14511451
RequestBody requestBody;
14521452
if (obj instanceof String) {
14531453
requestBody = RequestBody.create((String) obj, MediaType.parse("text/plain"));
1454+
} else if (obj == null){
1455+
requestBody = RequestBody.create(obj, MediaType.parse("application/json"));
1456+
} else if (obj.getClass().getEnumConstants() != null) {
1457+
requestBody = RequestBody.create(obj.toString(), MediaType.parse("text/plain"));
14541458
} else {
1455-
String content;
1456-
if (obj != null) {
1457-
content = JSON.serialize(obj);
1458-
} else {
1459-
content = null;
1460-
}
1461-
requestBody = RequestBody.create(content, MediaType.parse("application/json"));
1459+
requestBody = RequestBody.create(JSON.serialize(obj), MediaType.parse("application/json"));
14621460
}
1463-
14641461
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"");
14651462
mpBuilder.addPart(partHeaders, requestBody);
14661463
}

samples/client/others/java/okhttp-gson-oneOf/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,16 +1451,13 @@ protected void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String
14511451
RequestBody requestBody;
14521452
if (obj instanceof String) {
14531453
requestBody = RequestBody.create((String) obj, MediaType.parse("text/plain"));
1454+
} else if (obj == null){
1455+
requestBody = RequestBody.create(obj, MediaType.parse("application/json"));
1456+
} else if (obj.getClass().getEnumConstants() != null) {
1457+
requestBody = RequestBody.create(obj.toString(), MediaType.parse("text/plain"));
14541458
} else {
1455-
String content;
1456-
if (obj != null) {
1457-
content = JSON.serialize(obj);
1458-
} else {
1459-
content = null;
1460-
}
1461-
requestBody = RequestBody.create(content, MediaType.parse("application/json"));
1459+
requestBody = RequestBody.create(JSON.serialize(obj), MediaType.parse("application/json"));
14621460
}
1463-
14641461
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"");
14651462
mpBuilder.addPart(partHeaders, requestBody);
14661463
}

samples/client/others/java/okhttp-gson-streaming/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,16 +1474,13 @@ protected void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String
14741474
RequestBody requestBody;
14751475
if (obj instanceof String) {
14761476
requestBody = RequestBody.create((String) obj, MediaType.parse("text/plain"));
1477+
} else if (obj == null){
1478+
requestBody = RequestBody.create(obj, MediaType.parse("application/json"));
1479+
} else if (obj.getClass().getEnumConstants() != null) {
1480+
requestBody = RequestBody.create(obj.toString(), MediaType.parse("text/plain"));
14771481
} else {
1478-
String content;
1479-
if (obj != null) {
1480-
content = JSON.serialize(obj);
1481-
} else {
1482-
content = null;
1483-
}
1484-
requestBody = RequestBody.create(content, MediaType.parse("application/json"));
1482+
requestBody = RequestBody.create(JSON.serialize(obj), MediaType.parse("application/json"));
14851483
}
1486-
14871484
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"");
14881485
mpBuilder.addPart(partHeaders, requestBody);
14891486
}

samples/client/others/java/restclient-enum-in-multipart/.openapi-generator/FILES

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.github/workflows/maven.yml
22
.gitignore
3-
.openapi-generator-ignore
43
.travis.yml
54
README.md
65
api/openapi.yaml
@@ -35,7 +34,6 @@ src/main/java/org/openapitools/client/auth/HttpBearerAuth.java
3534
src/main/java/org/openapitools/client/model/DataChannel.java
3635
src/main/java/org/openapitools/client/model/DataDirection.java
3736
src/main/java/org/openapitools/client/model/InlineObject.java
38-
src/test/java/org/openapitools/client/api/BasApiTest.java
3937
src/test/java/org/openapitools/client/model/DataChannelTest.java
4038
src/test/java/org/openapitools/client/model/DataDirectionTest.java
4139
src/test/java/org/openapitools/client/model/InlineObjectTest.java

samples/client/others/java/restclient-enum-in-multipart/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ protected RestClient.RequestBodySpec prepareRequest(String path, HttpMethod meth
643643
(k, v) -> {
644644
if (v instanceof java.util.ArrayList) {
645645
Object o = v.get(0);
646-
if (o.getClass().getEnumConstants() != null) {
646+
if (o != null && o.getClass().getEnumConstants() != null) {
647647
v.set(0, o.toString());
648648
}
649649
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* API
3+
* API
4+
*
5+
* The version of the OpenAPI document: 1.0.0
6+
*
7+
*
8+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9+
* https://openapi-generator.tech
10+
* Do not edit the class manually.
11+
*/
12+
13+
14+
package org.openapitools.client.model;
15+
16+
import org.junit.jupiter.api.Assertions;
17+
import org.junit.jupiter.api.Disabled;
18+
import org.junit.jupiter.api.Test;
19+
20+
/**
21+
* Model tests for DataChannel
22+
*/
23+
class DataChannelTest {
24+
/**
25+
* Model tests for DataChannel
26+
*/
27+
@Test
28+
void testDataChannel() {
29+
// TODO: test DataChannel
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* API
3+
* API
4+
*
5+
* The version of the OpenAPI document: 1.0.0
6+
*
7+
*
8+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9+
* https://openapi-generator.tech
10+
* Do not edit the class manually.
11+
*/
12+
13+
14+
package org.openapitools.client.model;
15+
16+
import org.junit.jupiter.api.Assertions;
17+
import org.junit.jupiter.api.Disabled;
18+
import org.junit.jupiter.api.Test;
19+
20+
/**
21+
* Model tests for DataDirection
22+
*/
23+
class DataDirectionTest {
24+
/**
25+
* Model tests for DataDirection
26+
*/
27+
@Test
28+
void testDataDirection() {
29+
// TODO: test DataDirection
30+
}
31+
32+
}

0 commit comments

Comments
 (0)