Skip to content

Commit 7fc94ae

Browse files
committed
Fix #20370 by adding an OAuth access token supplier
1 parent 66c2a28 commit 7fc94ae

5 files changed

Lines changed: 65 additions & 15 deletions

File tree

  • 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

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,21 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
388388
* Helper method to set access token for the first OAuth2 authentication.
389389
* @param accessToken the access token
390390
*/
391-
public void setAccessToken(String accessToken) {
391+
public ApiClient setAccessToken(String accessToken) {
392+
return 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: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,21 @@ public void setApiKeyPrefix(String apiKeyPrefix) {
385385
* Helper method to set access token for the first OAuth2 authentication.
386386
* @param accessToken the access token
387387
*/
388-
public void setAccessToken(String accessToken) {
388+
public ApiClient setAccessToken(String accessToken) {
389+
return 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: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,21 @@ public void setApiKeyPrefix(String apiKeyPrefix) {
385385
* Helper method to set access token for the first OAuth2 authentication.
386386
* @param accessToken the access token
387387
*/
388-
public void setAccessToken(String accessToken) {
388+
public ApiClient setAccessToken(String accessToken) {
389+
return 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: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,21 @@ public void setApiKeyPrefix(String apiKeyPrefix) {
385385
* Helper method to set access token for the first OAuth2 authentication.
386386
* @param accessToken the access token
387387
*/
388-
public void setAccessToken(String accessToken) {
388+
public ApiClient setAccessToken(String accessToken) {
389+
return 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: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,21 @@ public void setApiKeyPrefix(String apiKeyPrefix) {
385385
* Helper method to set access token for the first OAuth2 authentication.
386386
* @param accessToken the access token
387387
*/
388-
public void setAccessToken(String accessToken) {
388+
public ApiClient setAccessToken(String accessToken) {
389+
return 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)