Skip to content

Commit

Permalink
[Java][WebClient] better code format (#5433)
Browse files Browse the repository at this point in the history
* better code format for java webclient

* prefix local varaible with localVar
  • Loading branch information
wing328 authored Feb 27, 2020
1 parent ba0d673 commit 5f547b8
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 408 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ public class ApiClient {
updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);
final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
if (queryParams != null) {
if (queryParams != null) {
builder.queryParams(queryParams);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {{invokerPackage}}.ApiClient;

{{#imports}}import {{import}};
{{/imports}}
{{^fullJavaUtil}}

{{^fullJavaUtil}}import java.util.HashMap;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;{{/fullJavaUtil}}
import java.util.Map;
{{/fullJavaUtil}}

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
Expand Down Expand Up @@ -62,49 +64,68 @@ public class {{classname}} {
*/
public {{#returnType}}{{#isListContainer}}Flux<{{{returnBaseType}}}>{{/isListContainer}}{{^isListContainer}}Mono<{{{returnType}}}>{{/isListContainer}} {{/returnType}}{{^returnType}}Mono<Void> {{/returnType}}{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws RestClientException {
Object postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
{{#allParams}}{{#required}}
{{#allParams}}
{{#required}}
// verify the required parameter '{{paramName}}' is set
if ({{paramName}} == null) {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter '{{paramName}}' when calling {{operationId}}");
}
{{/required}}{{/allParams}}
{{/required}}
{{/allParams}}
// create path and map variables
final Map<String, Object> pathParams = new HashMap<String, Object>();{{#hasPathParams}}{{#pathParams}}
pathParams.put("{{baseName}}", {{#collectionFormat}}apiClient.collectionPathParameterToString(ApiClient.CollectionFormat.valueOf("csv".toUpperCase()), {{/collectionFormat}}{{{paramName}}}{{#collectionFormat}}){{/collectionFormat}});{{/pathParams}}{{/hasPathParams}}
final Map<String, Object> pathParams = new HashMap<String, Object>();
{{#hasPathParams}}

{{#pathParams}}
pathParams.put("{{baseName}}", {{#collectionFormat}}apiClient.collectionPathParameterToString(ApiClient.CollectionFormat.valueOf("csv".toUpperCase()), {{/collectionFormat}}{{{paramName}}}{{#collectionFormat}}){{/collectionFormat}});
{{/pathParams}}
{{/hasPathParams}}

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
final MultiValueMap<String, String> cookieParams = new LinkedMultiValueMap<String, String>();
final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();{{#hasQueryParams}}
final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();
{{#hasQueryParams}}

{{#queryParams}}queryParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{collectionFormat}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}}));{{#hasMore}}
{{/hasMore}}{{/queryParams}}{{/hasQueryParams}}{{#hasHeaderParams}}
{{#queryParams}}
queryParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{collectionFormat}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}}));
{{/queryParams}}
{{/hasQueryParams}}
{{#hasHeaderParams}}

{{#headerParams}}if ({{paramName}} != null)
{{#headerParams}}
if ({{paramName}} != null)
headerParams.add("{{baseName}}", apiClient.parameterToString({{paramName}}));{{#hasMore}}
{{/hasMore}}{{/headerParams}}{{/hasHeaderParams}}{{#hasCookieParams}}

{{#cookieParams}}cookieParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{collectionFormat}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}}));{{#hasMore}}
{{/hasMore}}{{/cookieParams}}{{/hasCookieParams}}{{#hasFormParams}}

{{#formParams}}if ({{paramName}} != null)
formParams.add{{#collectionFormat}}All{{/collectionFormat}}("{{baseName}}", {{#isFile}}new FileSystemResource({{paramName}}){{/isFile}}{{^isFile}}{{paramName}}{{/isFile}});{{#hasMore}}
{{/hasMore}}{{/formParams}}{{/hasFormParams}}

final String[] accepts = { {{#hasProduces}}
{{/hasMore}}
{{/headerParams}}
{{/hasHeaderParams}}
{{#hasCookieParams}}

{{#cookieParams}}
cookieParams.putAll(apiClient.parameterToMultiValueMap({{#collectionFormat}}ApiClient.CollectionFormat.valueOf("{{{collectionFormat}}}".toUpperCase(Locale.ROOT)){{/collectionFormat}}{{^collectionFormat}}null{{/collectionFormat}}, "{{baseName}}", {{paramName}}));
{{/cookieParams}}
{{/hasCookieParams}}
{{#hasFormParams}}

{{#formParams}}
if ({{paramName}} != null)
formParams.add{{#collectionFormat}}All{{/collectionFormat}}("{{baseName}}", {{#isFile}}new FileSystemResource({{paramName}}){{/isFile}}{{^isFile}}{{paramName}}{{/isFile}});
{{/formParams}}
{{/hasFormParams}}

final String[] localVarAccepts = { {{#hasProduces}}
{{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}}
{{/hasProduces}}};
final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = { {{#hasConsumes}}
final List<MediaType> localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
final String[] localVarContentTypes = { {{#hasConsumes}}
{{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}
{{/hasConsumes}}};
final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);
final MediaType localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);

String[] authNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };
String[] localVarAuthNames = new String[] { {{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}} };

{{#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}}
return apiClient.{{#isListContainer}}invokeFluxAPI{{/isListContainer}}{{^isListContainer}}invokeAPI{{/isListContainer}}("{{{path}}}", HttpMethod.{{httpMethod}}, pathParams, queryParams, postBody, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType);
{{#returnType}}ParameterizedTypeReference<{{#isListContainer}}{{{returnBaseType}}}{{/isListContainer}}{{^isListContainer}}{{{returnType}}}{{/isListContainer}}> localVarReturnType = new ParameterizedTypeReference<{{#isListContainer}}{{{returnBaseType}}}{{/isListContainer}}{{^isListContainer}}{{{returnType}}}{{/isListContainer}}>() {};{{/returnType}}{{^returnType}}ParameterizedTypeReference<Void> localVarReturnType = new ParameterizedTypeReference<Void>() {};{{/returnType}}
return apiClient.{{#isListContainer}}invokeFluxAPI{{/isListContainer}}{{^isListContainer}}invokeAPI{{/isListContainer}}("{{{path}}}", HttpMethod.{{httpMethod}}, pathParams, queryParams, postBody, headerParams, cookieParams, formParams, localVarAccept, localVarContentType, localVarAuthNames, localVarReturnType);
}
{{/operation}}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ private WebClient.RequestBodySpec prepareRequest(String path, HttpMethod method,
updateParamsForAuth(authNames, queryParams, headerParams, cookieParams);

final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
if (queryParams != null) {
if (queryParams != null) {
builder.queryParams(queryParams);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,30 @@ public void setApiClient(ApiClient apiClient) {
*/
public Mono<Client> call123testSpecialTags(Client body) throws RestClientException {
Object postBody = body;

// verify the required parameter 'body' is set
if (body == null) {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling call123testSpecialTags");
}

// create path and map variables
final Map<String, Object> pathParams = new HashMap<String, Object>();


final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
final MultiValueMap<String, String> cookieParams = new LinkedMultiValueMap<String, String>();
final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();

final String[] accepts = {
final String[] localVarAccepts = {
"application/json"
};
final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
final String[] contentTypes = {
final List<MediaType> localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
final String[] localVarContentTypes = {
"application/json"
};
final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);
final MediaType localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);

String[] authNames = new String[] { };
String[] localVarAuthNames = new String[] { };

ParameterizedTypeReference<Client> returnType = new ParameterizedTypeReference<Client>() {};
return apiClient.invokeAPI("/another-fake/dummy", HttpMethod.PATCH, pathParams, queryParams, postBody, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType);
ParameterizedTypeReference<Client> localVarReturnType = new ParameterizedTypeReference<Client>() {};
return apiClient.invokeAPI("/another-fake/dummy", HttpMethod.PATCH, pathParams, queryParams, postBody, headerParams, cookieParams, formParams, localVarAccept, localVarContentType, localVarAuthNames, localVarReturnType);
}
}
Loading

0 comments on commit 5f547b8

Please sign in to comment.