Skip to content

Commit 9ceabc7

Browse files
atsharpwing328
authored andcommitted
[Java] Refactor webClient generator to use URI templates to capture correct metrics (#4314)
* Fixing URI templating for webclients to support Spring metric collection * Revert accidental change to test
1 parent 94b5863 commit 9ceabc7

11 files changed

Lines changed: 133 additions & 113 deletions

File tree

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ public class ApiClient {
513513
* @param <T> the return type to use
514514
* @param path The sub-path of the HTTP URL
515515
* @param method The request method
516+
* @param pathParams The path parameters
516517
* @param queryParams The query parameters
517518
* @param body The request body object
518519
* @param headerParams The header parameters
@@ -523,8 +524,8 @@ public class ApiClient {
523524
* @param returnType The return type into which to deserialize the response
524525
* @return The response body in chosen type
525526
*/
526-
public <T> Mono<T> invokeAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
527-
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames);
527+
public <T> Mono<T> invokeAPI(String path, HttpMethod method, Map<String, Object> pathParams, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
528+
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, pathParams, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames);
528529
return requestBuilder.retrieve().bodyToMono(returnType);
529530
}
530531

@@ -534,6 +535,7 @@ public class ApiClient {
534535
* @param <T> the return type to use
535536
* @param path The sub-path of the HTTP URL
536537
* @param method The request method
538+
* @param pathParams The path parameters
537539
* @param queryParams The query parameters
538540
* @param body The request body object
539541
* @param headerParams The header parameters
@@ -544,12 +546,12 @@ public class ApiClient {
544546
* @param returnType The return type into which to deserialize the response
545547
* @return The response body in chosen type
546548
*/
547-
public <T> Flux<T> invokeFluxAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
548-
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames);
549+
public <T> Flux<T> invokeFluxAPI(String path, HttpMethod method, Map<String, Object> pathParams, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
550+
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, pathParams, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames);
549551
return requestBuilder.retrieve().bodyToFlux(returnType);
550552
}
551553

552-
private WebClient.RequestBodySpec prepareRequest(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames) {
554+
private WebClient.RequestBodySpec prepareRequest(String path, HttpMethod method, Map<String, Object> pathParams, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames) {
553555
updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);
554556
555557
final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
@@ -569,7 +571,7 @@ public class ApiClient {
569571
builder.queryParams(queryParams);
570572
}
571573

572-
final WebClient.RequestBodySpec requestBuilder = webClient.method(method).uri(builder.build(true).toUri());
574+
final WebClient.RequestBodySpec requestBuilder = webClient.method(method).uri(builder.encode().toUriString(), pathParams);
573575
if(accept != null) {
574576
requestBuilder.accept(accept.toArray(new MediaType[accept.size()]));
575577
}

modules/openapi-generator/src/main/resources/Java/libraries/webclient/api.mustache

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,10 @@ public class {{classname}} {
6767
if ({{paramName}} == null) {
6868
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter '{{paramName}}' when calling {{operationId}}");
6969
}
70-
{{/required}}{{/allParams}}{{#hasPathParams}}
70+
{{/required}}{{/allParams}}
7171
// create path and map variables
72-
final Map<String, Object> uriVariables = new HashMap<String, Object>();{{#pathParams}}
73-
uriVariables.put("{{baseName}}", {{{paramName}}});{{/pathParams}}{{/hasPathParams}}
74-
String path = UriComponentsBuilder.fromPath("{{{path}}}"){{#hasPathParams}}.buildAndExpand(uriVariables){{/hasPathParams}}{{^hasPathParams}}.build(){{/hasPathParams}}.toUriString();
72+
final Map<String, Object> pathParams = new HashMap<String, Object>();{{#hasPathParams}}{{#pathParams}}
73+
pathParams.put("{{baseName}}", {{{paramName}}});{{/pathParams}}{{/hasPathParams}}
7574

7675
final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
7776
final HttpHeaders headerParams = new HttpHeaders();
@@ -104,7 +103,7 @@ public class {{classname}} {
104103
String[] authNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };
105104

106105
{{#returnType}}ParameterizedTypeReference<{{#isListContainer}}{{{returnBaseType}}}{{/isListContainer}}{{^isListContainer}}{{{returnType}}}{{/isListContainer}}> returnType = new ParameterizedTypeReference<{{#isListContainer}}{{{returnBaseType}}}{{/isListContainer}}{{^isListContainer}}{{{returnType}}}{{/isListContainer}}>() {};{{/returnType}}{{^returnType}}ParameterizedTypeReference<Void> returnType = new ParameterizedTypeReference<Void>() {};{{/returnType}}
107-
return apiClient.{{#isListContainer}}invokeFluxAPI{{/isListContainer}}{{^isListContainer}}invokeAPI{{/isListContainer}}(path, HttpMethod.{{httpMethod}}, queryParams, postBody, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType);
106+
return apiClient.{{#isListContainer}}invokeFluxAPI{{/isListContainer}}{{^isListContainer}}invokeAPI{{/isListContainer}}("{{{path}}}", HttpMethod.{{httpMethod}}, pathParams, queryParams, postBody, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType);
108107
}
109108
{{/operation}}
110109
}

modules/openapi-generator/src/main/resources/Java/libraries/webclient/pom.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
<properties>
143143
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
144144
<swagger-annotations-version>1.5.22</swagger-annotations-version>
145-
<spring-web-version>5.0.7.RELEASE</spring-web-version>
145+
<spring-web-version>5.0.8.RELEASE</spring-web-version>
146146
<jackson-version>2.9.10</jackson-version>
147147
<jackson-databind-version>2.9.10.1</jackson-databind-version>
148148
<jackson-databind-nullable-version>0.2.0</jackson-databind-nullable-version>

samples/client/petstore/java/webclient/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
<properties>
122122
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
123123
<swagger-annotations-version>1.5.22</swagger-annotations-version>
124-
<spring-web-version>5.0.7.RELEASE</spring-web-version>
124+
<spring-web-version>5.0.8.RELEASE</spring-web-version>
125125
<jackson-version>2.9.10</jackson-version>
126126
<jackson-databind-version>2.9.10.1</jackson-databind-version>
127127
<jackson-databind-nullable-version>0.2.0</jackson-databind-nullable-version>

samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ public MediaType selectHeaderContentType(String[] contentTypes) {
509509
* @param <T> the return type to use
510510
* @param path The sub-path of the HTTP URL
511511
* @param method The request method
512+
* @param pathParams The path parameters
512513
* @param queryParams The query parameters
513514
* @param body The request body object
514515
* @param headerParams The header parameters
@@ -519,8 +520,8 @@ public MediaType selectHeaderContentType(String[] contentTypes) {
519520
* @param returnType The return type into which to deserialize the response
520521
* @return The response body in chosen type
521522
*/
522-
public <T> Mono<T> invokeAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
523-
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames);
523+
public <T> Mono<T> invokeAPI(String path, HttpMethod method, Map<String, Object> pathParams, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
524+
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, pathParams, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames);
524525
return requestBuilder.retrieve().bodyToMono(returnType);
525526
}
526527

@@ -530,6 +531,7 @@ public <T> Mono<T> invokeAPI(String path, HttpMethod method, MultiValueMap<Strin
530531
* @param <T> the return type to use
531532
* @param path The sub-path of the HTTP URL
532533
* @param method The request method
534+
* @param pathParams The path parameters
533535
* @param queryParams The query parameters
534536
* @param body The request body object
535537
* @param headerParams The header parameters
@@ -540,12 +542,12 @@ public <T> Mono<T> invokeAPI(String path, HttpMethod method, MultiValueMap<Strin
540542
* @param returnType The return type into which to deserialize the response
541543
* @return The response body in chosen type
542544
*/
543-
public <T> Flux<T> invokeFluxAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
544-
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames);
545+
public <T> Flux<T> invokeFluxAPI(String path, HttpMethod method, Map<String, Object> pathParams, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
546+
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, pathParams, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames);
545547
return requestBuilder.retrieve().bodyToFlux(returnType);
546548
}
547549

548-
private WebClient.RequestBodySpec prepareRequest(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames) {
550+
private WebClient.RequestBodySpec prepareRequest(String path, HttpMethod method, Map<String, Object> pathParams, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, String> cookieParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames) {
549551
updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);
550552

551553
final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
@@ -565,7 +567,7 @@ private WebClient.RequestBodySpec prepareRequest(String path, HttpMethod method,
565567
builder.queryParams(queryParams);
566568
}
567569

568-
final WebClient.RequestBodySpec requestBuilder = webClient.method(method).uri(builder.build(true).toUri());
570+
final WebClient.RequestBodySpec requestBuilder = webClient.method(method).uri(builder.encode().toUriString(), pathParams);
569571
if(accept != null) {
570572
requestBuilder.accept(accept.toArray(new MediaType[accept.size()]));
571573
}

samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/api/AnotherFakeApi.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public Mono<Client> call123testSpecialTags(Client body) throws RestClientExcepti
6161
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling call123testSpecialTags");
6262
}
6363

64-
String path = UriComponentsBuilder.fromPath("/another-fake/dummy").build().toUriString();
64+
// create path and map variables
65+
final Map<String, Object> pathParams = new HashMap<String, Object>();
6566

6667
final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
6768
final HttpHeaders headerParams = new HttpHeaders();
@@ -80,6 +81,6 @@ public Mono<Client> call123testSpecialTags(Client body) throws RestClientExcepti
8081
String[] authNames = new String[] { };
8182

8283
ParameterizedTypeReference<Client> returnType = new ParameterizedTypeReference<Client>() {};
83-
return apiClient.invokeAPI(path, HttpMethod.PATCH, queryParams, postBody, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType);
84+
return apiClient.invokeAPI("/another-fake/dummy", HttpMethod.PATCH, pathParams, queryParams, postBody, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType);
8485
}
8586
}

0 commit comments

Comments
 (0)