diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache index 3dd26153003f..a69f86b4b9be 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache @@ -389,10 +389,20 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { * @param accessToken the access token */ public void setAccessToken(String accessToken) { + setAccessToken(() -> accessToken); + } + + /** + * Helper method to set the supplier of access tokens for OAuth2 authentication. + * + * @param tokenSupplier The supplier of access tokens + * @return ApiClient this client + */ + public ApiClient setAccessToken(Supplier tokenSupplier) { for (Authentication auth : authentications.values()) { if (auth instanceof OAuth) { - ((OAuth) auth).setAccessToken(accessToken); - return; + ((OAuth) auth).setAccessToken(tokenSupplier); + return this; } } throw new RuntimeException("No OAuth2 authentication configured!"); diff --git a/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java index bdad8856c14b..09e69f4c9c88 100644 --- a/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java @@ -386,10 +386,20 @@ public void setApiKeyPrefix(String apiKeyPrefix) { * @param accessToken the access token */ public void setAccessToken(String accessToken) { + setAccessToken(() -> accessToken); + } + + /** + * Helper method to set the supplier of access tokens for OAuth2 authentication. + * + * @param tokenSupplier The supplier of access tokens + * @return ApiClient this client + */ + public ApiClient setAccessToken(Supplier tokenSupplier) { for (Authentication auth : authentications.values()) { if (auth instanceof OAuth) { - ((OAuth) auth).setAccessToken(accessToken); - return; + ((OAuth) auth).setAccessToken(tokenSupplier); + return this; } } throw new RuntimeException("No OAuth2 authentication configured!"); diff --git a/samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java index bdad8856c14b..09e69f4c9c88 100644 --- a/samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java @@ -386,10 +386,20 @@ public void setApiKeyPrefix(String apiKeyPrefix) { * @param accessToken the access token */ public void setAccessToken(String accessToken) { + setAccessToken(() -> accessToken); + } + + /** + * Helper method to set the supplier of access tokens for OAuth2 authentication. + * + * @param tokenSupplier The supplier of access tokens + * @return ApiClient this client + */ + public ApiClient setAccessToken(Supplier tokenSupplier) { for (Authentication auth : authentications.values()) { if (auth instanceof OAuth) { - ((OAuth) auth).setAccessToken(accessToken); - return; + ((OAuth) auth).setAccessToken(tokenSupplier); + return this; } } throw new RuntimeException("No OAuth2 authentication configured!"); diff --git a/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java index bdad8856c14b..09e69f4c9c88 100644 --- a/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java @@ -386,10 +386,20 @@ public void setApiKeyPrefix(String apiKeyPrefix) { * @param accessToken the access token */ public void setAccessToken(String accessToken) { + setAccessToken(() -> accessToken); + } + + /** + * Helper method to set the supplier of access tokens for OAuth2 authentication. + * + * @param tokenSupplier The supplier of access tokens + * @return ApiClient this client + */ + public ApiClient setAccessToken(Supplier tokenSupplier) { for (Authentication auth : authentications.values()) { if (auth instanceof OAuth) { - ((OAuth) auth).setAccessToken(accessToken); - return; + ((OAuth) auth).setAccessToken(tokenSupplier); + return this; } } throw new RuntimeException("No OAuth2 authentication configured!"); diff --git a/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java index bdad8856c14b..09e69f4c9c88 100644 --- a/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java @@ -386,10 +386,20 @@ public void setApiKeyPrefix(String apiKeyPrefix) { * @param accessToken the access token */ public void setAccessToken(String accessToken) { + setAccessToken(() -> accessToken); + } + + /** + * Helper method to set the supplier of access tokens for OAuth2 authentication. + * + * @param tokenSupplier The supplier of access tokens + * @return ApiClient this client + */ + public ApiClient setAccessToken(Supplier tokenSupplier) { for (Authentication auth : authentications.values()) { if (auth instanceof OAuth) { - ((OAuth) auth).setAccessToken(accessToken); - return; + ((OAuth) auth).setAccessToken(tokenSupplier); + return this; } } throw new RuntimeException("No OAuth2 authentication configured!");