Skip to content

Commit ded833e

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

File tree

37 files changed

+307
-110
lines changed

37 files changed

+307
-110
lines changed

.github/workflows/samples-java-client-jdk17.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ on:
77
- samples/client/petstore/java/webclient-jakarta/**
88
- samples/client/petstore/java/restclient-*/**
99
- samples/client/petstore/java/webclient-useSingleRequestParameter/**
10+
- samples/client/others/java/restclient-enum-in-multipart/**
1011
pull_request:
1112
paths:
1213
- samples/client/petstore/java/resttemplate-jakarta/**
1314
- samples/client/petstore/java/webclient-jakarta/**
1415
- samples/client/petstore/java/restclient-*/**
1516
- samples/client/petstore/java/webclient-useSingleRequestParameter/**
17+
- samples/client/others/java/restclient-enum-in-multipart/**
1618
jobs:
1719
build:
1820
name: Build Java Client JDK17
@@ -30,6 +32,7 @@ jobs:
3032
- samples/client/petstore/java/restclient-useSingleRequestParameter
3133
- samples/client/petstore/java/restclient-useSingleRequestParameter-static
3234
- samples/client/petstore/java/webclient-useSingleRequestParameter
35+
- samples/client/others/java/restclient-enum-in-multipart
3336
steps:
3437
- uses: actions/checkout@v4
3538
- uses: actions/setup-java@v4

modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,13 +1732,16 @@ public class ApiClient {
17321732
RequestBody requestBody;
17331733
if (obj instanceof String) {
17341734
requestBody = RequestBody.create((String) obj, MediaType.parse("text/plain"));
1735-
} else if (obj == null){
1736-
requestBody = RequestBody.create(obj, MediaType.parse("application/json"));
1737-
} else if (obj.getClass().getEnumConstants() != null) {
1738-
requestBody = RequestBody.create(obj.toString(), MediaType.parse("text/plain"));
17391735
} else {
1740-
requestBody = RequestBody.create(JSON.serialize(obj), MediaType.parse("application/json"));
1736+
String content;
1737+
if (obj != null) {
1738+
content = JSON.serialize(obj);
1739+
} else {
1740+
content = null;
1741+
}
1742+
requestBody = RequestBody.create(content, MediaType.parse("application/json"));
17411743
}
1744+
17421745
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"");
17431746
mpBuilder.addPart(partHeaders, requestBody);
17441747
}

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/restclient-enum-in-multipart/.openapi-generator/FILES

Lines changed: 0 additions & 5 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,3 @@ 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
39-
src/test/java/org/openapitools/client/model/DataChannelTest.java
40-
src/test/java/org/openapitools/client/model/DataDirectionTest.java
41-
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 com.fasterxml.jackson.annotation.JsonInclude;
17+
import com.fasterxml.jackson.annotation.JsonProperty;
18+
import com.fasterxml.jackson.annotation.JsonCreator;
19+
import com.fasterxml.jackson.annotation.JsonTypeName;
20+
import com.fasterxml.jackson.annotation.JsonValue;
21+
import java.util.UUID;
22+
import org.junit.jupiter.api.Assertions;
23+
import org.junit.jupiter.api.Disabled;
24+
import org.junit.jupiter.api.Test;
25+
26+
/**
27+
* Model tests for InlineObject
28+
*/
29+
class InlineObjectTest {
30+
private final InlineObject model = new InlineObject();
31+
32+
/**
33+
* Model tests for InlineObject
34+
*/
35+
@Test
36+
void testInlineObject() {
37+
// TODO: test InlineObject
38+
}
39+
40+
/**
41+
* Test the property 'messageId'
42+
*/
43+
@Test
44+
void messageIdTest() {
45+
// TODO: test messageId
46+
}
47+
48+
}

samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java

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

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

samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java

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

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

0 commit comments

Comments
 (0)