Skip to content

Commit c43dea8

Browse files
committed
chore: regenerate jersey3-oneOf sample with errorEntity feature
1 parent 1acccd6 commit c43dea8

3 files changed

Lines changed: 42 additions & 5 deletions

File tree

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import java.util.Arrays;
6363
import java.util.ArrayList;
6464
import java.util.Date;
65+
import java.util.Locale;
6566
import java.util.stream.Collectors;
6667
import java.util.stream.Stream;
6768
import java.time.OffsetDateTime;
@@ -974,6 +975,7 @@ public File prepareDownloadFile(Response response) throws IOException {
974975
* @param authNames The authentications to apply
975976
* @param returnType The return type into which to deserialize the response
976977
* @param isBodyNullable True if the body is nullable
978+
* @param errorTypes Mapping of error codes to types into which to deserialize the response
977979
* @return The response body in type of string
978980
* @throws ApiException API exception
979981
*/
@@ -990,7 +992,9 @@ public <T> ApiResponse<T> invokeAPI(
990992
String contentType,
991993
String[] authNames,
992994
GenericType<T> returnType,
993-
boolean isBodyNullable)
995+
boolean isBodyNullable,
996+
Map<String, GenericType> errorTypes
997+
)
994998
throws ApiException {
995999

9961000
String targetURL;
@@ -1001,7 +1005,7 @@ public <T> ApiResponse<T> invokeAPI(
10011005
if (index < 0 || index >= serverConfigurations.size()) {
10021006
throw new ArrayIndexOutOfBoundsException(
10031007
String.format(
1004-
java.util.Locale.ROOT,
1008+
Locale.ROOT,
10051009
"Invalid index %d when selecting the host settings. Must be less than %d",
10061010
index, serverConfigurations.size()));
10071011
}
@@ -1088,14 +1092,16 @@ public <T> ApiResponse<T> invokeAPI(
10881092
String respBody = null;
10891093
if (response.hasEntity()) {
10901094
try {
1095+
// call bufferEntity, so that a subsequent call to `readEntity` in `deserialize` doesn't fail
1096+
response.bufferEntity();
10911097
respBody = String.valueOf(response.readEntity(String.class));
10921098
message = respBody;
10931099
} catch (RuntimeException e) {
10941100
// e.printStackTrace();
10951101
}
10961102
}
10971103
throw new ApiException(
1098-
response.getStatus(), message, buildResponseHeaders(response), respBody);
1104+
response.getStatus(), message, buildResponseHeaders(response), respBody, deserializeErrorEntity(errorTypes, response));
10991105
}
11001106
} finally {
11011107
try {
@@ -1106,6 +1112,21 @@ public <T> ApiResponse<T> invokeAPI(
11061112
}
11071113
}
11081114
}
1115+
1116+
private Object deserializeErrorEntity(Map<String, GenericType> errorTypes, Response response) {
1117+
if (errorTypes == null) {
1118+
return null;
1119+
}
1120+
GenericType errorType = errorTypes.get(String.valueOf(response.getStatus()));
1121+
if (errorType == null) {
1122+
errorType = errorTypes.get("0"); // "0" is the "default" response
1123+
}
1124+
try {
1125+
return deserialize(response, errorType);
1126+
} catch (Exception e) {
1127+
return null;
1128+
}
1129+
}
11091130

11101131
protected Response sendRequest(String method, Invocation.Builder invocationBuilder, Entity<?> entity) {
11111132
Response response;
@@ -1132,7 +1153,7 @@ protected Response sendRequest(String method, Invocation.Builder invocationBuild
11321153
*/
11331154
@Deprecated
11341155
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 {
1135-
return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType, isBodyNullable);
1156+
return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType, isBodyNullable, null/*TODO SME manage*/);
11361157
}
11371158

11381159
/**

samples/client/petstore/java/jersey3-oneOf/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-oneOf/src/main/java/org/openapitools/client/api/DefaultApi.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ public void rootPost(@jakarta.annotation.Nullable PostRequest postRequest) throw
7878
public ApiResponse<Void> rootPostWithHttpInfo(@jakarta.annotation.Nullable PostRequest postRequest) throws ApiException {
7979
String localVarAccept = apiClient.selectHeaderAccept();
8080
String localVarContentType = apiClient.selectHeaderContentType("application/json");
81+
final Map<String, GenericType> localVarErrorTypes = new HashMap<String, GenericType>();
8182
return apiClient.invokeAPI("DefaultApi.rootPost", "/", "POST", new ArrayList<>(), postRequest,
8283
new LinkedHashMap<>(), new LinkedHashMap<>(), new LinkedHashMap<>(), localVarAccept, localVarContentType,
83-
null, null, false);
84+
null, null, false, localVarErrorTypes);
8485
}
8586
}

0 commit comments

Comments
 (0)