Skip to content

Commit 3ec7cc4

Browse files
committed
chore: regenerate Java jersey3 samples with errorEntity feature
- Regenerate samples to verify templates work correctly - ApiException now contains errorEntity and getErrorEntity() - API methods include localVarErrorTypes for error deserialization - Fixes #4777
1 parent be83c27 commit 3ec7cc4

File tree

9 files changed

+126
-47
lines changed

9 files changed

+126
-47
lines changed

samples/client/petstore/java/jersey3/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import java.util.Arrays;
6464
import java.util.ArrayList;
6565
import java.util.Date;
66+
import java.util.Locale;
6667
import java.util.stream.Collectors;
6768
import java.util.stream.Stream;
6869
import java.time.OffsetDateTime;
@@ -1197,6 +1198,7 @@ public File prepareDownloadFile(Response response) throws IOException {
11971198
* @param authNames The authentications to apply
11981199
* @param returnType The return type into which to deserialize the response
11991200
* @param isBodyNullable True if the body is nullable
1201+
* @param errorTypes Mapping of error codes to types into which to deserialize the response
12001202
* @return The response body in type of string
12011203
* @throws ApiException API exception
12021204
*/
@@ -1213,7 +1215,9 @@ public <T> ApiResponse<T> invokeAPI(
12131215
String contentType,
12141216
String[] authNames,
12151217
GenericType<T> returnType,
1216-
boolean isBodyNullable)
1218+
boolean isBodyNullable,
1219+
Map<String, GenericType> errorTypes
1220+
)
12171221
throws ApiException {
12181222

12191223
String targetURL;
@@ -1224,7 +1228,7 @@ public <T> ApiResponse<T> invokeAPI(
12241228
if (index < 0 || index >= serverConfigurations.size()) {
12251229
throw new ArrayIndexOutOfBoundsException(
12261230
String.format(
1227-
java.util.Locale.ROOT,
1231+
Locale.ROOT,
12281232
"Invalid index %d when selecting the host settings. Must be less than %d",
12291233
index, serverConfigurations.size()));
12301234
}
@@ -1327,14 +1331,16 @@ public <T> ApiResponse<T> invokeAPI(
13271331
String respBody = null;
13281332
if (response.hasEntity()) {
13291333
try {
1334+
// call bufferEntity, so that a subsequent call to `readEntity` in `deserialize` doesn't fail
1335+
response.bufferEntity();
13301336
respBody = String.valueOf(response.readEntity(String.class));
13311337
message = respBody;
13321338
} catch (RuntimeException e) {
13331339
// e.printStackTrace();
13341340
}
13351341
}
13361342
throw new ApiException(
1337-
response.getStatus(), message, buildResponseHeaders(response), respBody);
1343+
response.getStatus(), message, buildResponseHeaders(response), respBody, deserializeErrorEntity(errorTypes, response));
13381344
}
13391345
} finally {
13401346
try {
@@ -1345,6 +1351,21 @@ public <T> ApiResponse<T> invokeAPI(
13451351
}
13461352
}
13471353
}
1354+
1355+
private Object deserializeErrorEntity(Map<String, GenericType> errorTypes, Response response) {
1356+
if (errorTypes == null) {
1357+
return null;
1358+
}
1359+
GenericType errorType = errorTypes.get(String.valueOf(response.getStatus()));
1360+
if (errorType == null) {
1361+
errorType = errorTypes.get("0"); // "0" is the "default" response
1362+
}
1363+
try {
1364+
return deserialize(response, errorType);
1365+
} catch (Exception e) {
1366+
return String.format("Failed deserializing error entity: %s", e.toString());
1367+
}
1368+
}
13481369

13491370
protected Response sendRequest(String method, Invocation.Builder invocationBuilder, Entity<?> entity) {
13501371
Response response;
@@ -1371,7 +1392,7 @@ protected Response sendRequest(String method, Invocation.Builder invocationBuild
13711392
*/
13721393
@Deprecated
13731394
public <T> ApiResponse<T> invokeAPI(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames, GenericType<T> returnType, boolean isBodyNullable) throws ApiException {
1374-
return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType, isBodyNullable);
1395+
return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType, isBodyNullable, null/*TODO SME manage*/);
13751396
}
13761397

13771398
/**

samples/client/petstore/java/jersey3/src/main/java/org/openapitools/client/ApiException.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class ApiException extends Exception {
2626
private int code = 0;
2727
private Map<String, List<String>> responseHeaders = null;
2828
private String responseBody = null;
29+
private Object errorEntity = null;
2930

3031
public ApiException() {}
3132

@@ -67,6 +68,11 @@ public ApiException(int code, String message, Map<String, List<String>> response
6768
this.responseBody = responseBody;
6869
}
6970

71+
public ApiException(int code, String message, Map<String, List<String>> responseHeaders, String responseBody, Object errorEntity) {
72+
this(code, message, responseHeaders, responseBody);
73+
this.errorEntity = errorEntity;
74+
}
75+
7076
/**
7177
* Get the HTTP status code.
7278
*
@@ -93,4 +99,13 @@ public Map<String, List<String>> getResponseHeaders() {
9399
public String getResponseBody() {
94100
return responseBody;
95101
}
102+
103+
/**
104+
* Get the deserialized error entity (or null if this error doesn't have a model associated).
105+
*
106+
* @return Deserialized error entity
107+
*/
108+
public Object getErrorEntity() {
109+
return errorEntity;
110+
}
96111
}

samples/client/petstore/java/jersey3/src/main/java/org/openapitools/client/api/AnotherFakeApi.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ public ApiResponse<Client> call123testSpecialTagsWithHttpInfo(@jakarta.annotatio
8787

8888
String localVarAccept = apiClient.selectHeaderAccept("application/json");
8989
String localVarContentType = apiClient.selectHeaderContentType("application/json");
90+
final Map<String, GenericType> localVarErrorTypes = new HashMap<String, GenericType>();
9091
GenericType<Client> localVarReturnType = new GenericType<Client>() {};
9192
return apiClient.invokeAPI("AnotherFakeApi.call123testSpecialTags", "/another-fake/dummy", "PATCH", new ArrayList<>(), client,
9293
new LinkedHashMap<>(), new LinkedHashMap<>(), new LinkedHashMap<>(), localVarAccept, localVarContentType,
93-
null, localVarReturnType, false);
94+
null, localVarReturnType, false, localVarErrorTypes);
9495
}
9596
}

samples/client/petstore/java/jersey3/src/main/java/org/openapitools/client/api/DefaultApi.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ public FooGetDefaultResponse fooGet() throws ApiException {
8080
public ApiResponse<FooGetDefaultResponse> fooGetWithHttpInfo() throws ApiException {
8181
String localVarAccept = apiClient.selectHeaderAccept("application/json");
8282
String localVarContentType = apiClient.selectHeaderContentType();
83+
final Map<String, GenericType> localVarErrorTypes = new HashMap<String, GenericType>();
8384
GenericType<FooGetDefaultResponse> localVarReturnType = new GenericType<FooGetDefaultResponse>() {};
8485
return apiClient.invokeAPI("DefaultApi.fooGet", "/foo", "GET", new ArrayList<>(), null,
8586
new LinkedHashMap<>(), new LinkedHashMap<>(), new LinkedHashMap<>(), localVarAccept, localVarContentType,
86-
null, localVarReturnType, false);
87+
null, localVarReturnType, false, localVarErrorTypes);
8788
}
8889
}

0 commit comments

Comments
 (0)