Skip to content

Commit a967b51

Browse files
committed
feat(jersey3): add error entity deserialization support
- Add errorEntity field and getErrorEntity() method to ApiException - Add deserializeErrorEntity() method to ApiClient for error deserialization - Update API methods to pass errorTypes map for automatic error handling - Add unit tests for errorEntity feature
1 parent 5bc693d commit a967b51

101 files changed

Lines changed: 1681 additions & 921 deletions

File tree

Some content is hidden

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

samples/client/petstore/java/jersey3-oneOf/pom.xml

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -318,24 +318,6 @@
318318
<artifactId>jersey-apache-connector</artifactId>
319319
<version>${jersey-version}</version>
320320
</dependency>
321-
<dependency>
322-
<groupId>org.tomitribe</groupId>
323-
<artifactId>tomitribe-http-signatures</artifactId>
324-
<version>${http-signature-version}</version>
325-
</dependency>
326-
<!-- Bean Validation API support -->
327-
<dependency>
328-
<groupId>jakarta.validation</groupId>
329-
<artifactId>jakarta.validation-api</artifactId>
330-
<version>${beanvalidation-version}</version>
331-
<scope>provided</scope>
332-
</dependency>
333-
<!-- For equals and hashCode using reflection -->
334-
<dependency>
335-
<groupId>org.apache.commons</groupId>
336-
<artifactId>commons-lang3</artifactId>
337-
<version>${commons-lang3-version}</version>
338-
</dependency>
339321

340322
<!-- test dependencies -->
341323
<dependency>
@@ -354,8 +336,6 @@
354336
<jackson-databind-nullable-version>0.2.10</jackson-databind-nullable-version>
355337
<jakarta-annotation-version>2.1.1</jakarta-annotation-version>
356338
<beanvalidation-version>3.0.2</beanvalidation-version>
357-
<commons-lang3-version>3.12.0</commons-lang3-version>
358-
<http-signature-version>1.8</http-signature-version>
359339
<junit-version>5.10.0</junit-version>
360340
<spotless.version>2.21.0</spotless.version>
361341
</properties>

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

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
import java.util.Arrays;
6363
import java.util.ArrayList;
6464
import java.util.Date;
65-
import java.util.Locale;
6665
import java.util.stream.Collectors;
6766
import java.util.stream.Stream;
6867
import java.time.OffsetDateTime;
@@ -975,7 +974,6 @@ public File prepareDownloadFile(Response response) throws IOException {
975974
* @param authNames The authentications to apply
976975
* @param returnType The return type into which to deserialize the response
977976
* @param isBodyNullable True if the body is nullable
978-
* @param errorTypes Mapping of error codes to types into which to deserialize the response
979977
* @return The response body in type of string
980978
* @throws ApiException API exception
981979
*/
@@ -992,9 +990,7 @@ public <T> ApiResponse<T> invokeAPI(
992990
String contentType,
993991
String[] authNames,
994992
GenericType<T> returnType,
995-
boolean isBodyNullable,
996-
Map<String, GenericType> errorTypes
997-
)
993+
boolean isBodyNullable)
998994
throws ApiException {
999995

1000996
String targetURL;
@@ -1005,7 +1001,7 @@ public <T> ApiResponse<T> invokeAPI(
10051001
if (index < 0 || index >= serverConfigurations.size()) {
10061002
throw new ArrayIndexOutOfBoundsException(
10071003
String.format(
1008-
Locale.ROOT,
1004+
java.util.Locale.ROOT,
10091005
"Invalid index %d when selecting the host settings. Must be less than %d",
10101006
index, serverConfigurations.size()));
10111007
}
@@ -1092,16 +1088,14 @@ public <T> ApiResponse<T> invokeAPI(
10921088
String respBody = null;
10931089
if (response.hasEntity()) {
10941090
try {
1095-
// call bufferEntity, so that a subsequent call to `readEntity` in `deserialize` doesn't fail
1096-
response.bufferEntity();
10971091
respBody = String.valueOf(response.readEntity(String.class));
10981092
message = respBody;
10991093
} catch (RuntimeException e) {
11001094
// e.printStackTrace();
11011095
}
11021096
}
11031097
throw new ApiException(
1104-
response.getStatus(), message, buildResponseHeaders(response), respBody, deserializeErrorEntity(errorTypes, response));
1098+
response.getStatus(), message, buildResponseHeaders(response), respBody);
11051099
}
11061100
} finally {
11071101
try {
@@ -1112,30 +1106,6 @@ public <T> ApiResponse<T> invokeAPI(
11121106
}
11131107
}
11141108
}
1115-
1116-
/**
1117-
* Deserialize the response body into an error entity based on HTTP status code.
1118-
* Looks up the error type from the errorTypes map using the response status code,
1119-
* or falls back to the "default" error type if no match is found.
1120-
*
1121-
* @param errorTypes Map of status code strings to GenericType for deserialization
1122-
* @param response The HTTP response
1123-
* @return The deserialized error entity, or null if not found or deserialization fails
1124-
*/
1125-
private Object deserializeErrorEntity(Map<String, GenericType> errorTypes, Response response) {
1126-
if (errorTypes == null) {
1127-
return null;
1128-
}
1129-
GenericType errorType = errorTypes.get(String.valueOf(response.getStatus()));
1130-
if (errorType == null) {
1131-
errorType = errorTypes.get("0"); // "0" is the "default" response
1132-
}
1133-
try {
1134-
return deserialize(response, errorType);
1135-
} catch (Exception e) {
1136-
return null;
1137-
}
1138-
}
11391109

11401110
protected Response sendRequest(String method, Invocation.Builder invocationBuilder, Entity<?> entity) {
11411111
Response response;
@@ -1162,7 +1132,7 @@ protected Response sendRequest(String method, Invocation.Builder invocationBuild
11621132
*/
11631133
@Deprecated
11641134
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 {
1165-
return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType, isBodyNullable, null/*TODO SME manage*/);
1135+
return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType, isBodyNullable);
11661136
}
11671137

11681138
/**

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ 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;
3029

3130
public ApiException() {}
3231

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

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-
7670
/**
7771
* Get the HTTP status code.
7872
*
@@ -99,13 +93,4 @@ public Map<String, List<String>> getResponseHeaders() {
9993
public String getResponseBody() {
10094
return responseBody;
10195
}
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-
}
11196
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ 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>();
8281
return apiClient.invokeAPI("DefaultApi.rootPost", "/", "POST", new ArrayList<>(), postRequest,
8382
new LinkedHashMap<>(), new LinkedHashMap<>(), new LinkedHashMap<>(), localVarAccept, localVarContentType,
84-
null, null, false, localVarErrorTypes);
83+
null, null, false);
8584
}
8685
}

samples/client/petstore/java/jersey3/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# openapi-java-client
1+
# petstore-jersey3
22

33
OpenAPI Petstore
44

@@ -41,7 +41,7 @@ Add this dependency to your project's POM:
4141
```xml
4242
<dependency>
4343
<groupId>org.openapitools</groupId>
44-
<artifactId>openapi-java-client</artifactId>
44+
<artifactId>petstore-jersey3</artifactId>
4545
<version>1.0.0</version>
4646
<scope>compile</scope>
4747
</dependency>
@@ -53,12 +53,12 @@ Add this dependency to your project's build file:
5353

5454
```groovy
5555
repositories {
56-
mavenCentral() // Needed if the 'openapi-java-client' jar has been published to maven central.
57-
mavenLocal() // Needed if the 'openapi-java-client' jar has been published to the local maven repo.
56+
mavenCentral() // Needed if the 'petstore-jersey3' jar has been published to maven central.
57+
mavenLocal() // Needed if the 'petstore-jersey3' jar has been published to the local maven repo.
5858
}
5959
6060
dependencies {
61-
implementation "org.openapitools:openapi-java-client:1.0.0"
61+
implementation "org.openapitools:petstore-jersey3:1.0.0"
6262
}
6363
```
6464

@@ -72,7 +72,7 @@ mvn clean package
7272

7373
Then manually install the following JARs:
7474

75-
- `target/openapi-java-client-1.0.0.jar`
75+
- `target/petstore-jersey3-1.0.0.jar`
7676
- `target/lib/*.jar`
7777

7878
## Getting Started

samples/client/petstore/java/jersey3/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ if(hasProperty('target') && target == 'android') {
8484
publishing {
8585
publications {
8686
maven(MavenPublication) {
87-
artifactId = 'openapi-java-client'
87+
artifactId = 'petstore-jersey3'
8888

8989
from components.java
9090
}
@@ -104,10 +104,12 @@ ext {
104104
jackson_databind_version = "2.21.1"
105105
jackson_databind_nullable_version = "0.2.10"
106106
jakarta_annotation_version = "2.1.0"
107+
bean_validation_version = "3.0.2"
107108
jersey_version = "3.0.4"
108109
junit_version = "5.8.2"
109110
scribejava_apis_version = "8.3.1"
110111
tomitribe_http_signatures_version = "1.7"
112+
commons_lang3_version = "3.17.0"
111113
}
112114

113115
dependencies {
@@ -126,6 +128,8 @@ dependencies {
126128
implementation "com.github.scribejava:scribejava-apis:$scribejava_apis_version"
127129
implementation "org.tomitribe:tomitribe-http-signatures:$tomitribe_http_signatures_version"
128130
implementation "jakarta.annotation:jakarta.annotation-api:$jakarta_annotation_version"
131+
implementation "jakarta.validation:jakarta.validation-api:$bean_validation_version"
132+
implementation "org.apache.commons:commons-lang3:$commons_lang3_version"
129133
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit_version"
130134
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit_version"
131135
}

samples/client/petstore/java/jersey3/build.sbt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
lazy val root = (project in file(".")).
22
settings(
33
organization := "org.openapitools",
4-
name := "openapi-java-client",
4+
name := "petstore-jersey3",
55
version := "1.0.0",
66
scalaVersion := "2.11.12",
77
scalacOptions ++= Seq("-feature"),
@@ -24,6 +24,7 @@ lazy val root = (project in file(".")).
2424
"com.github.scribejava" % "scribejava-apis" % "8.3.1" % "compile",
2525
"org.tomitribe" % "tomitribe-http-signatures" % "1.7" % "compile",
2626
"jakarta.annotation" % "jakarta.annotation-api" % "2.1.0" % "compile",
27+
"org.apache.commons" % "commons-lang3" % "3.17.0" % "compile",
2728
"org.junit.jupiter" % "junit-jupiter-api" % "5.8.2" % "test"
2829
)
2930
)

samples/client/petstore/java/jersey3/docs/ArrayTest.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
|------------ | ------------- | ------------- | -------------|
1010
|**arrayOfString** | **List&lt;String&gt;** | | [optional] |
1111
|**arrayArrayOfInteger** | **List&lt;List&lt;Long&gt;&gt;** | | [optional] |
12-
|**arrayArrayOfModel** | **List&lt;List&lt;ReadOnlyFirst&gt;&gt;** | | [optional] |
12+
|**arrayArrayOfModel** | **List&lt;List&lt;@Valid ReadOnlyFirst&gt;&gt;** | | [optional] |
1313

1414

1515

samples/client/petstore/java/jersey3/docs/FakeApi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ public class Example {
427427
defaultClient.setBasePath("http://petstore.swagger.io:80/v2");
428428

429429
FakeApi apiInstance = new FakeApi(defaultClient);
430-
List<String> requestBody = Arrays.asList(); // List<String> |
430+
List<@Pattern(regexp = "[A-Z0-9]+")String> requestBody = Arrays.asList(); // List<@Pattern(regexp = "[A-Z0-9]+")String> |
431431
try {
432432
apiInstance.postArrayOfString(requestBody);
433433
} catch (ApiException e) {

samples/client/petstore/java/jersey3/docs/UserApi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public class Example {
103103
defaultClient.setBasePath("http://petstore.swagger.io:80/v2");
104104

105105
UserApi apiInstance = new UserApi(defaultClient);
106-
List<User> user = Arrays.asList(); // List<User> | List of user object
106+
List<@Valid User> user = Arrays.asList(); // List<@Valid User> | List of user object
107107
try {
108108
apiInstance.createUsersWithArrayInput(user);
109109
} catch (ApiException e) {
@@ -167,7 +167,7 @@ public class Example {
167167
defaultClient.setBasePath("http://petstore.swagger.io:80/v2");
168168

169169
UserApi apiInstance = new UserApi(defaultClient);
170-
List<User> user = Arrays.asList(); // List<User> | List of user object
170+
List<@Valid User> user = Arrays.asList(); // List<@Valid User> | List of user object
171171
try {
172172
apiInstance.createUsersWithListInput(user);
173173
} catch (ApiException e) {

0 commit comments

Comments
 (0)