Skip to content

Commit 0486bce

Browse files
GregDThomasGoopher Maijenburg
authored andcommitted
Fix OpenAPITools#20370 by adding an OAuth access token supplier (OpenAPITools#21800)
1 parent 652a1a3 commit 0486bce

File tree

5 files changed

+60
-10
lines changed
  • modules/openapi-generator/src/main/resources/Java/libraries/restclient
  • samples/client/petstore/java
    • restclient-swagger2/src/main/java/org/openapitools/client
    • restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client
    • restclient-useSingleRequestParameter/src/main/java/org/openapitools/client
    • restclient/src/main/java/org/openapitools/client

5 files changed

+60
-10
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,20 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
389389
* @param accessToken the access token
390390
*/
391391
public void setAccessToken(String accessToken) {
392+
setAccessToken(() -> accessToken);
393+
}
394+
395+
/**
396+
* Helper method to set the supplier of access tokens for OAuth2 authentication.
397+
*
398+
* @param tokenSupplier The supplier of access tokens
399+
* @return ApiClient this client
400+
*/
401+
public ApiClient setAccessToken(Supplier<String> tokenSupplier) {
392402
for (Authentication auth : authentications.values()) {
393403
if (auth instanceof OAuth) {
394-
((OAuth) auth).setAccessToken(accessToken);
395-
return;
404+
((OAuth) auth).setAccessToken(tokenSupplier);
405+
return this;
396406
}
397407
}
398408
throw new RuntimeException("No OAuth2 authentication configured!");

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,20 @@ public void setApiKeyPrefix(String apiKeyPrefix) {
386386
* @param accessToken the access token
387387
*/
388388
public void setAccessToken(String accessToken) {
389+
setAccessToken(() -> accessToken);
390+
}
391+
392+
/**
393+
* Helper method to set the supplier of access tokens for OAuth2 authentication.
394+
*
395+
* @param tokenSupplier The supplier of access tokens
396+
* @return ApiClient this client
397+
*/
398+
public ApiClient setAccessToken(Supplier<String> tokenSupplier) {
389399
for (Authentication auth : authentications.values()) {
390400
if (auth instanceof OAuth) {
391-
((OAuth) auth).setAccessToken(accessToken);
392-
return;
401+
((OAuth) auth).setAccessToken(tokenSupplier);
402+
return this;
393403
}
394404
}
395405
throw new RuntimeException("No OAuth2 authentication configured!");

samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,20 @@ public void setApiKeyPrefix(String apiKeyPrefix) {
386386
* @param accessToken the access token
387387
*/
388388
public void setAccessToken(String accessToken) {
389+
setAccessToken(() -> accessToken);
390+
}
391+
392+
/**
393+
* Helper method to set the supplier of access tokens for OAuth2 authentication.
394+
*
395+
* @param tokenSupplier The supplier of access tokens
396+
* @return ApiClient this client
397+
*/
398+
public ApiClient setAccessToken(Supplier<String> tokenSupplier) {
389399
for (Authentication auth : authentications.values()) {
390400
if (auth instanceof OAuth) {
391-
((OAuth) auth).setAccessToken(accessToken);
392-
return;
401+
((OAuth) auth).setAccessToken(tokenSupplier);
402+
return this;
393403
}
394404
}
395405
throw new RuntimeException("No OAuth2 authentication configured!");

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,20 @@ public void setApiKeyPrefix(String apiKeyPrefix) {
386386
* @param accessToken the access token
387387
*/
388388
public void setAccessToken(String accessToken) {
389+
setAccessToken(() -> accessToken);
390+
}
391+
392+
/**
393+
* Helper method to set the supplier of access tokens for OAuth2 authentication.
394+
*
395+
* @param tokenSupplier The supplier of access tokens
396+
* @return ApiClient this client
397+
*/
398+
public ApiClient setAccessToken(Supplier<String> tokenSupplier) {
389399
for (Authentication auth : authentications.values()) {
390400
if (auth instanceof OAuth) {
391-
((OAuth) auth).setAccessToken(accessToken);
392-
return;
401+
((OAuth) auth).setAccessToken(tokenSupplier);
402+
return this;
393403
}
394404
}
395405
throw new RuntimeException("No OAuth2 authentication configured!");

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,20 @@ public void setApiKeyPrefix(String apiKeyPrefix) {
386386
* @param accessToken the access token
387387
*/
388388
public void setAccessToken(String accessToken) {
389+
setAccessToken(() -> accessToken);
390+
}
391+
392+
/**
393+
* Helper method to set the supplier of access tokens for OAuth2 authentication.
394+
*
395+
* @param tokenSupplier The supplier of access tokens
396+
* @return ApiClient this client
397+
*/
398+
public ApiClient setAccessToken(Supplier<String> tokenSupplier) {
389399
for (Authentication auth : authentications.values()) {
390400
if (auth instanceof OAuth) {
391-
((OAuth) auth).setAccessToken(accessToken);
392-
return;
401+
((OAuth) auth).setAccessToken(tokenSupplier);
402+
return this;
393403
}
394404
}
395405
throw new RuntimeException("No OAuth2 authentication configured!");

0 commit comments

Comments
 (0)