Skip to content

Commit ce1e9e0

Browse files
authored
Jersey2/3: Fixes a bug in OAuth token renewal, where the previous HTTP 401 code is reused even after the retry succeeds (#22134)
* Jersey2/3: Fix a bug where, when using OAuth, the HTTP 401 status code persisted even if the second request after renewing the Bearer access token succeeded * Jersey2/3: Regenerate samples
1 parent fc12a1b commit ce1e9e0

File tree

8 files changed

+25
-25
lines changed

8 files changed

+25
-25
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,11 +1299,9 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
12991299
try {
13001300
response = sendRequest(method, invocationBuilder, entity);
13011301
1302-
final int statusCode = response.getStatusInfo().getStatusCode();
1303-
13041302
{{#hasOAuthMethods}}
13051303
// If OAuth is used and a status 401 is received, renew the access token and retry the request
1306-
if (authNames != null && statusCode == Status.UNAUTHORIZED.getStatusCode()) {
1304+
if (authNames != null && response.getStatusInfo().getStatusCode() == Status.UNAUTHORIZED.getStatusCode()) {
13071305
for (String authName : authNames) {
13081306
Authentication authentication = authentications.get(authName);
13091307
if (authentication instanceof OAuth) {
@@ -1317,8 +1315,10 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
13171315
}
13181316
}
13191317
}
1320-
1318+
13211319
{{/hasOAuthMethods}}
1320+
final int statusCode = response.getStatusInfo().getStatusCode();
1321+
13221322
Map<String, List<String>> responseHeaders = buildResponseHeaders(response);
13231323

13241324
if (statusCode == Status.NO_CONTENT.getStatusCode()) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,11 +1299,9 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
12991299
try {
13001300
response = sendRequest(method, invocationBuilder, entity);
13011301
1302-
final int statusCode = response.getStatusInfo().getStatusCode();
1303-
13041302
{{#hasOAuthMethods}}
13051303
// If OAuth is used and a status 401 is received, renew the access token and retry the request
1306-
if (authNames != null && statusCode == Status.UNAUTHORIZED.getStatusCode()) {
1304+
if (authNames != null && response.getStatusInfo().getStatusCode() == Status.UNAUTHORIZED.getStatusCode()) {
13071305
for (String authName : authNames) {
13081306
Authentication authentication = authentications.get(authName);
13091307
if (authentication instanceof OAuth) {
@@ -1319,6 +1317,8 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
13191317
}
13201318

13211319
{{/hasOAuthMethods}}
1320+
final int statusCode = response.getStatusInfo().getStatusCode();
1321+
13221322
Map<String, List<String>> responseHeaders = buildResponseHeaders(response);
13231323

13241324
if (statusCode == Status.NO_CONTENT.getStatusCode()) {

samples/client/petstore/java/jersey2-java8-localdatetime/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,10 +1213,8 @@ public <T> ApiResponse<T> invokeAPI(
12131213
try {
12141214
response = sendRequest(method, invocationBuilder, entity);
12151215

1216-
final int statusCode = response.getStatusInfo().getStatusCode();
1217-
12181216
// If OAuth is used and a status 401 is received, renew the access token and retry the request
1219-
if (authNames != null && statusCode == Status.UNAUTHORIZED.getStatusCode()) {
1217+
if (authNames != null && response.getStatusInfo().getStatusCode() == Status.UNAUTHORIZED.getStatusCode()) {
12201218
for (String authName : authNames) {
12211219
Authentication authentication = authentications.get(authName);
12221220
if (authentication instanceof OAuth) {
@@ -1230,6 +1228,8 @@ public <T> ApiResponse<T> invokeAPI(
12301228
}
12311229
}
12321230
}
1231+
1232+
final int statusCode = response.getStatusInfo().getStatusCode();
12331233

12341234
Map<String, List<String>> responseHeaders = buildResponseHeaders(response);
12351235

samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,10 +1213,8 @@ public <T> ApiResponse<T> invokeAPI(
12131213
try {
12141214
response = sendRequest(method, invocationBuilder, entity);
12151215

1216-
final int statusCode = response.getStatusInfo().getStatusCode();
1217-
12181216
// If OAuth is used and a status 401 is received, renew the access token and retry the request
1219-
if (authNames != null && statusCode == Status.UNAUTHORIZED.getStatusCode()) {
1217+
if (authNames != null && response.getStatusInfo().getStatusCode() == Status.UNAUTHORIZED.getStatusCode()) {
12201218
for (String authName : authNames) {
12211219
Authentication authentication = authentications.get(authName);
12221220
if (authentication instanceof OAuth) {
@@ -1230,6 +1228,8 @@ public <T> ApiResponse<T> invokeAPI(
12301228
}
12311229
}
12321230
}
1231+
1232+
final int statusCode = response.getStatusInfo().getStatusCode();
12331233

12341234
Map<String, List<String>> responseHeaders = buildResponseHeaders(response);
12351235

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,10 +1295,8 @@ public <T> ApiResponse<T> invokeAPI(
12951295
try {
12961296
response = sendRequest(method, invocationBuilder, entity);
12971297

1298-
final int statusCode = response.getStatusInfo().getStatusCode();
1299-
13001298
// If OAuth is used and a status 401 is received, renew the access token and retry the request
1301-
if (authNames != null && statusCode == Status.UNAUTHORIZED.getStatusCode()) {
1299+
if (authNames != null && response.getStatusInfo().getStatusCode() == Status.UNAUTHORIZED.getStatusCode()) {
13021300
for (String authName : authNames) {
13031301
Authentication authentication = authentications.get(authName);
13041302
if (authentication instanceof OAuth) {
@@ -1313,6 +1311,8 @@ public <T> ApiResponse<T> invokeAPI(
13131311
}
13141312
}
13151313

1314+
final int statusCode = response.getStatusInfo().getStatusCode();
1315+
13161316
Map<String, List<String>> responseHeaders = buildResponseHeaders(response);
13171317

13181318
if (statusCode == Status.NO_CONTENT.getStatusCode()) {

samples/openapi3/client/petstore/java/jersey2-java8-swagger1/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,10 +1197,8 @@ public <T> ApiResponse<T> invokeAPI(
11971197
try {
11981198
response = sendRequest(method, invocationBuilder, entity);
11991199

1200-
final int statusCode = response.getStatusInfo().getStatusCode();
1201-
12021200
// If OAuth is used and a status 401 is received, renew the access token and retry the request
1203-
if (authNames != null && statusCode == Status.UNAUTHORIZED.getStatusCode()) {
1201+
if (authNames != null && response.getStatusInfo().getStatusCode() == Status.UNAUTHORIZED.getStatusCode()) {
12041202
for (String authName : authNames) {
12051203
Authentication authentication = authentications.get(authName);
12061204
if (authentication instanceof OAuth) {
@@ -1214,6 +1212,8 @@ public <T> ApiResponse<T> invokeAPI(
12141212
}
12151213
}
12161214
}
1215+
1216+
final int statusCode = response.getStatusInfo().getStatusCode();
12171217

12181218
Map<String, List<String>> responseHeaders = buildResponseHeaders(response);
12191219

samples/openapi3/client/petstore/java/jersey2-java8-swagger2/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,10 +1197,8 @@ public <T> ApiResponse<T> invokeAPI(
11971197
try {
11981198
response = sendRequest(method, invocationBuilder, entity);
11991199

1200-
final int statusCode = response.getStatusInfo().getStatusCode();
1201-
12021200
// If OAuth is used and a status 401 is received, renew the access token and retry the request
1203-
if (authNames != null && statusCode == Status.UNAUTHORIZED.getStatusCode()) {
1201+
if (authNames != null && response.getStatusInfo().getStatusCode() == Status.UNAUTHORIZED.getStatusCode()) {
12041202
for (String authName : authNames) {
12051203
Authentication authentication = authentications.get(authName);
12061204
if (authentication instanceof OAuth) {
@@ -1214,6 +1212,8 @@ public <T> ApiResponse<T> invokeAPI(
12141212
}
12151213
}
12161214
}
1215+
1216+
final int statusCode = response.getStatusInfo().getStatusCode();
12171217

12181218
Map<String, List<String>> responseHeaders = buildResponseHeaders(response);
12191219

samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,10 +1295,8 @@ public <T> ApiResponse<T> invokeAPI(
12951295
try {
12961296
response = sendRequest(method, invocationBuilder, entity);
12971297

1298-
final int statusCode = response.getStatusInfo().getStatusCode();
1299-
13001298
// If OAuth is used and a status 401 is received, renew the access token and retry the request
1301-
if (authNames != null && statusCode == Status.UNAUTHORIZED.getStatusCode()) {
1299+
if (authNames != null && response.getStatusInfo().getStatusCode() == Status.UNAUTHORIZED.getStatusCode()) {
13021300
for (String authName : authNames) {
13031301
Authentication authentication = authentications.get(authName);
13041302
if (authentication instanceof OAuth) {
@@ -1312,6 +1310,8 @@ public <T> ApiResponse<T> invokeAPI(
13121310
}
13131311
}
13141312
}
1313+
1314+
final int statusCode = response.getStatusInfo().getStatusCode();
13151315

13161316
Map<String, List<String>> responseHeaders = buildResponseHeaders(response);
13171317

0 commit comments

Comments
 (0)