diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/restclient/auth/OAuth.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/restclient/auth/OAuth.mustache index 1e1e6247c80c..1b7ad33a33f4 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/restclient/auth/OAuth.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/restclient/auth/OAuth.mustache @@ -2,25 +2,49 @@ package {{invokerPackage}}.auth; +import java.util.Optional; +import java.util.function.Supplier; import org.springframework.http.HttpHeaders; import org.springframework.util.MultiValueMap; +/** + * Provides support for RFC 6750 - Bearer Token usage for OAUTH 2.0 Authorization. + */ {{>generatedAnnotation}} public class OAuth implements Authentication { - private String accessToken; + private Supplier tokenSupplier; + /** + * Returns the bearer token used for Authorization. + * + * @return The bearer token + */ public String getAccessToken() { - return accessToken; + return tokenSupplier.get(); } + /** + * Sets the bearer access token used for Authorization. + * + * @param accessToken The bearer token to send in the Authorization header + */ public void setAccessToken(String accessToken) { - this.accessToken = accessToken; + setAccessToken(() -> accessToken); + } + + /** + * Sets the supplier of bearer tokens used for Authorization. + * + * @param tokenSupplier The supplier of bearer tokens to send in the Authorization header + */ + public void setAccessToken(Supplier tokenSupplier) { + this.tokenSupplier = tokenSupplier; } @Override public void applyToParams(MultiValueMap queryParams, HttpHeaders headerParams, MultiValueMap cookieParams) { - if (accessToken != null) { - headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken); - } + Optional.ofNullable(tokenSupplier).map(Supplier::get).ifPresent(accessToken -> + headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken) + ); } } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java index 1bbd795f6510..bd44e898ec82 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java @@ -2963,7 +2963,7 @@ void shouldGenerateCorrectJaxbAnnotations(Library library) { @Test - public void shouldGenerateOAuthTokenSuppliers() { + public void testRestTemplateWithGeneratedOAuthTokenSuppliers() { final Map files = generateFromContract( "src/test/resources/3_0/java/oauth.yaml", @@ -2994,6 +2994,27 @@ public void shouldGenerateOAuthTokenSuppliers() { } + @Test + public void testRestClientWithGeneratedOAuthTokenSuppliers() { + final Map files = generateFromContract( + "src/test/resources/3_0/java/oauth.yaml", + JavaClientCodegen.RESTCLIENT + ); + + final JavaFileAssert oAuth = JavaFileAssert.assertThat(files.get("OAuth.java")) + .printFileContent(); + oAuth + .assertMethod("setAccessToken", "String") + .bodyContainsLines("setAccessToken(() -> accessToken);"); + oAuth + .assertMethod("setAccessToken", "Supplier") + .bodyContainsLines("this.tokenSupplier = tokenSupplier;"); + oAuth + .assertMethod("applyToParams") + .bodyContainsLines("Optional.ofNullable(tokenSupplier).map(Supplier::get).ifPresent(accessToken ->") + .bodyContainsLines("headerParams.add(HttpHeaders.AUTHORIZATION, \"Bearer \" + accessToken)"); + } + @Test public void testRestClientWithXML_issue_19137() { final Path output = newTempFolder(); final CodegenConfigurator configurator = new CodegenConfigurator() diff --git a/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/auth/OAuth.java b/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/auth/OAuth.java index c8549d29b709..c4e47c4868fe 100644 --- a/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/auth/OAuth.java +++ b/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/auth/OAuth.java @@ -13,25 +13,49 @@ package org.openapitools.client.auth; +import java.util.Optional; +import java.util.function.Supplier; import org.springframework.http.HttpHeaders; import org.springframework.util.MultiValueMap; +/** + * Provides support for RFC 6750 - Bearer Token usage for OAUTH 2.0 Authorization. + */ @jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.10.0-SNAPSHOT") public class OAuth implements Authentication { - private String accessToken; + private Supplier tokenSupplier; + /** + * Returns the bearer token used for Authorization. + * + * @return The bearer token + */ public String getAccessToken() { - return accessToken; + return tokenSupplier.get(); } + /** + * Sets the bearer access token used for Authorization. + * + * @param accessToken The bearer token to send in the Authorization header + */ public void setAccessToken(String accessToken) { - this.accessToken = accessToken; + setAccessToken(() -> accessToken); + } + + /** + * Sets the supplier of bearer tokens used for Authorization. + * + * @param tokenSupplier The supplier of bearer tokens to send in the Authorization header + */ + public void setAccessToken(Supplier tokenSupplier) { + this.tokenSupplier = tokenSupplier; } @Override public void applyToParams(MultiValueMap queryParams, HttpHeaders headerParams, MultiValueMap cookieParams) { - if (accessToken != null) { - headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken); - } + Optional.ofNullable(tokenSupplier).map(Supplier::get).ifPresent(accessToken -> + headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken) + ); } } diff --git a/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/auth/OAuth.java b/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/auth/OAuth.java index c8549d29b709..c4e47c4868fe 100644 --- a/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/auth/OAuth.java +++ b/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/auth/OAuth.java @@ -13,25 +13,49 @@ package org.openapitools.client.auth; +import java.util.Optional; +import java.util.function.Supplier; import org.springframework.http.HttpHeaders; import org.springframework.util.MultiValueMap; +/** + * Provides support for RFC 6750 - Bearer Token usage for OAUTH 2.0 Authorization. + */ @jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.10.0-SNAPSHOT") public class OAuth implements Authentication { - private String accessToken; + private Supplier tokenSupplier; + /** + * Returns the bearer token used for Authorization. + * + * @return The bearer token + */ public String getAccessToken() { - return accessToken; + return tokenSupplier.get(); } + /** + * Sets the bearer access token used for Authorization. + * + * @param accessToken The bearer token to send in the Authorization header + */ public void setAccessToken(String accessToken) { - this.accessToken = accessToken; + setAccessToken(() -> accessToken); + } + + /** + * Sets the supplier of bearer tokens used for Authorization. + * + * @param tokenSupplier The supplier of bearer tokens to send in the Authorization header + */ + public void setAccessToken(Supplier tokenSupplier) { + this.tokenSupplier = tokenSupplier; } @Override public void applyToParams(MultiValueMap queryParams, HttpHeaders headerParams, MultiValueMap cookieParams) { - if (accessToken != null) { - headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken); - } + Optional.ofNullable(tokenSupplier).map(Supplier::get).ifPresent(accessToken -> + headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken) + ); } } diff --git a/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/auth/OAuth.java b/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/auth/OAuth.java index c8549d29b709..c4e47c4868fe 100644 --- a/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/auth/OAuth.java +++ b/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/auth/OAuth.java @@ -13,25 +13,49 @@ package org.openapitools.client.auth; +import java.util.Optional; +import java.util.function.Supplier; import org.springframework.http.HttpHeaders; import org.springframework.util.MultiValueMap; +/** + * Provides support for RFC 6750 - Bearer Token usage for OAUTH 2.0 Authorization. + */ @jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.10.0-SNAPSHOT") public class OAuth implements Authentication { - private String accessToken; + private Supplier tokenSupplier; + /** + * Returns the bearer token used for Authorization. + * + * @return The bearer token + */ public String getAccessToken() { - return accessToken; + return tokenSupplier.get(); } + /** + * Sets the bearer access token used for Authorization. + * + * @param accessToken The bearer token to send in the Authorization header + */ public void setAccessToken(String accessToken) { - this.accessToken = accessToken; + setAccessToken(() -> accessToken); + } + + /** + * Sets the supplier of bearer tokens used for Authorization. + * + * @param tokenSupplier The supplier of bearer tokens to send in the Authorization header + */ + public void setAccessToken(Supplier tokenSupplier) { + this.tokenSupplier = tokenSupplier; } @Override public void applyToParams(MultiValueMap queryParams, HttpHeaders headerParams, MultiValueMap cookieParams) { - if (accessToken != null) { - headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken); - } + Optional.ofNullable(tokenSupplier).map(Supplier::get).ifPresent(accessToken -> + headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken) + ); } }