From cb6e0943d03ee9242890f3908ce5999f30e4d0a5 Mon Sep 17 00:00:00 2001 From: Greg Thomas Date: Fri, 22 Aug 2025 16:35:52 +0100 Subject: [PATCH] Fix #20370 by adding an OAuth access token supplier --- .../Java/libraries/restclient/ApiClient.mustache | 14 ++++++++++++-- .../java/org/openapitools/client/ApiClient.java | 14 ++++++++++++-- .../java/org/openapitools/client/ApiClient.java | 14 ++++++++++++-- .../java/org/openapitools/client/ApiClient.java | 14 ++++++++++++-- .../java/org/openapitools/client/ApiClient.java | 14 ++++++++++++-- 5 files changed, 60 insertions(+), 10 deletions(-) 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!");