Skip to content

Commit c70b078

Browse files
authored
Add support for a token supplier to OAuth based RestClient clients. (#19944)
Fixes #19943
1 parent acb1641 commit c70b078

File tree

5 files changed

+142
-25
lines changed
  • modules/openapi-generator/src
  • samples/client/petstore/java
    • restclient-swagger2/src/main/java/org/openapitools/client/auth
    • restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/auth
    • restclient/src/main/java/org/openapitools/client/auth

5 files changed

+142
-25
lines changed

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,49 @@
22

33
package {{invokerPackage}}.auth;
44

5+
import java.util.Optional;
6+
import java.util.function.Supplier;
57
import org.springframework.http.HttpHeaders;
68
import org.springframework.util.MultiValueMap;
79

10+
/**
11+
* Provides support for RFC 6750 - Bearer Token usage for OAUTH 2.0 Authorization.
12+
*/
813
{{>generatedAnnotation}}
914
public class OAuth implements Authentication {
10-
private String accessToken;
15+
private Supplier<String> tokenSupplier;
1116
17+
/**
18+
* Returns the bearer token used for Authorization.
19+
*
20+
* @return The bearer token
21+
*/
1222
public String getAccessToken() {
13-
return accessToken;
23+
return tokenSupplier.get();
1424
}
1525

26+
/**
27+
* Sets the bearer access token used for Authorization.
28+
*
29+
* @param accessToken The bearer token to send in the Authorization header
30+
*/
1631
public void setAccessToken(String accessToken) {
17-
this.accessToken = accessToken;
32+
setAccessToken(() -> accessToken);
33+
}
34+
35+
/**
36+
* Sets the supplier of bearer tokens used for Authorization.
37+
*
38+
* @param tokenSupplier The supplier of bearer tokens to send in the Authorization header
39+
*/
40+
public void setAccessToken(Supplier<String> tokenSupplier) {
41+
this.tokenSupplier = tokenSupplier;
1842
}
1943

2044
@Override
2145
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
22-
if (accessToken != null) {
23-
headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
24-
}
46+
Optional.ofNullable(tokenSupplier).map(Supplier::get).ifPresent(accessToken ->
47+
headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken)
48+
);
2549
}
2650
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2963,7 +2963,7 @@ void shouldGenerateCorrectJaxbAnnotations(Library library) {
29632963

29642964

29652965
@Test
2966-
public void shouldGenerateOAuthTokenSuppliers() {
2966+
public void testRestTemplateWithGeneratedOAuthTokenSuppliers() {
29672967

29682968
final Map<String, File> files = generateFromContract(
29692969
"src/test/resources/3_0/java/oauth.yaml",
@@ -2994,6 +2994,27 @@ public void shouldGenerateOAuthTokenSuppliers() {
29942994

29952995
}
29962996

2997+
@Test
2998+
public void testRestClientWithGeneratedOAuthTokenSuppliers() {
2999+
final Map<String, File> files = generateFromContract(
3000+
"src/test/resources/3_0/java/oauth.yaml",
3001+
JavaClientCodegen.RESTCLIENT
3002+
);
3003+
3004+
final JavaFileAssert oAuth = JavaFileAssert.assertThat(files.get("OAuth.java"))
3005+
.printFileContent();
3006+
oAuth
3007+
.assertMethod("setAccessToken", "String")
3008+
.bodyContainsLines("setAccessToken(() -> accessToken);");
3009+
oAuth
3010+
.assertMethod("setAccessToken", "Supplier<String>")
3011+
.bodyContainsLines("this.tokenSupplier = tokenSupplier;");
3012+
oAuth
3013+
.assertMethod("applyToParams")
3014+
.bodyContainsLines("Optional.ofNullable(tokenSupplier).map(Supplier::get).ifPresent(accessToken ->")
3015+
.bodyContainsLines("headerParams.add(HttpHeaders.AUTHORIZATION, \"Bearer \" + accessToken)");
3016+
}
3017+
29973018
@Test public void testRestClientWithXML_issue_19137() {
29983019
final Path output = newTempFolder();
29993020
final CodegenConfigurator configurator = new CodegenConfigurator()

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,49 @@
1313

1414
package org.openapitools.client.auth;
1515

16+
import java.util.Optional;
17+
import java.util.function.Supplier;
1618
import org.springframework.http.HttpHeaders;
1719
import org.springframework.util.MultiValueMap;
1820

21+
/**
22+
* Provides support for RFC 6750 - Bearer Token usage for OAUTH 2.0 Authorization.
23+
*/
1924
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.10.0-SNAPSHOT")
2025
public class OAuth implements Authentication {
21-
private String accessToken;
26+
private Supplier<String> tokenSupplier;
2227

28+
/**
29+
* Returns the bearer token used for Authorization.
30+
*
31+
* @return The bearer token
32+
*/
2333
public String getAccessToken() {
24-
return accessToken;
34+
return tokenSupplier.get();
2535
}
2636

37+
/**
38+
* Sets the bearer access token used for Authorization.
39+
*
40+
* @param accessToken The bearer token to send in the Authorization header
41+
*/
2742
public void setAccessToken(String accessToken) {
28-
this.accessToken = accessToken;
43+
setAccessToken(() -> accessToken);
44+
}
45+
46+
/**
47+
* Sets the supplier of bearer tokens used for Authorization.
48+
*
49+
* @param tokenSupplier The supplier of bearer tokens to send in the Authorization header
50+
*/
51+
public void setAccessToken(Supplier<String> tokenSupplier) {
52+
this.tokenSupplier = tokenSupplier;
2953
}
3054

3155
@Override
3256
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
33-
if (accessToken != null) {
34-
headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
35-
}
57+
Optional.ofNullable(tokenSupplier).map(Supplier::get).ifPresent(accessToken ->
58+
headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken)
59+
);
3660
}
3761
}

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,49 @@
1313

1414
package org.openapitools.client.auth;
1515

16+
import java.util.Optional;
17+
import java.util.function.Supplier;
1618
import org.springframework.http.HttpHeaders;
1719
import org.springframework.util.MultiValueMap;
1820

21+
/**
22+
* Provides support for RFC 6750 - Bearer Token usage for OAUTH 2.0 Authorization.
23+
*/
1924
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.10.0-SNAPSHOT")
2025
public class OAuth implements Authentication {
21-
private String accessToken;
26+
private Supplier<String> tokenSupplier;
2227

28+
/**
29+
* Returns the bearer token used for Authorization.
30+
*
31+
* @return The bearer token
32+
*/
2333
public String getAccessToken() {
24-
return accessToken;
34+
return tokenSupplier.get();
2535
}
2636

37+
/**
38+
* Sets the bearer access token used for Authorization.
39+
*
40+
* @param accessToken The bearer token to send in the Authorization header
41+
*/
2742
public void setAccessToken(String accessToken) {
28-
this.accessToken = accessToken;
43+
setAccessToken(() -> accessToken);
44+
}
45+
46+
/**
47+
* Sets the supplier of bearer tokens used for Authorization.
48+
*
49+
* @param tokenSupplier The supplier of bearer tokens to send in the Authorization header
50+
*/
51+
public void setAccessToken(Supplier<String> tokenSupplier) {
52+
this.tokenSupplier = tokenSupplier;
2953
}
3054

3155
@Override
3256
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
33-
if (accessToken != null) {
34-
headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
35-
}
57+
Optional.ofNullable(tokenSupplier).map(Supplier::get).ifPresent(accessToken ->
58+
headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken)
59+
);
3660
}
3761
}

samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/auth/OAuth.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,49 @@
1313

1414
package org.openapitools.client.auth;
1515

16+
import java.util.Optional;
17+
import java.util.function.Supplier;
1618
import org.springframework.http.HttpHeaders;
1719
import org.springframework.util.MultiValueMap;
1820

21+
/**
22+
* Provides support for RFC 6750 - Bearer Token usage for OAUTH 2.0 Authorization.
23+
*/
1924
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.10.0-SNAPSHOT")
2025
public class OAuth implements Authentication {
21-
private String accessToken;
26+
private Supplier<String> tokenSupplier;
2227

28+
/**
29+
* Returns the bearer token used for Authorization.
30+
*
31+
* @return The bearer token
32+
*/
2333
public String getAccessToken() {
24-
return accessToken;
34+
return tokenSupplier.get();
2535
}
2636

37+
/**
38+
* Sets the bearer access token used for Authorization.
39+
*
40+
* @param accessToken The bearer token to send in the Authorization header
41+
*/
2742
public void setAccessToken(String accessToken) {
28-
this.accessToken = accessToken;
43+
setAccessToken(() -> accessToken);
44+
}
45+
46+
/**
47+
* Sets the supplier of bearer tokens used for Authorization.
48+
*
49+
* @param tokenSupplier The supplier of bearer tokens to send in the Authorization header
50+
*/
51+
public void setAccessToken(Supplier<String> tokenSupplier) {
52+
this.tokenSupplier = tokenSupplier;
2953
}
3054

3155
@Override
3256
public void applyToParams(MultiValueMap<String, String> queryParams, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams) {
33-
if (accessToken != null) {
34-
headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
35-
}
57+
Optional.ofNullable(tokenSupplier).map(Supplier::get).ifPresent(accessToken ->
58+
headerParams.add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken)
59+
);
3660
}
3761
}

0 commit comments

Comments
 (0)