Skip to content

Commit

Permalink
Expand path templates via resttemplate's uriTemplateHandler (#3500)
Browse files Browse the repository at this point in the history
* Expand path templates via resttemplate's uriTemplateHandler

* fix tests and compilation on lower jdk version

* fix typo

* re-generate samples
  • Loading branch information
unintended authored and macjohnny committed Aug 6, 2019
1 parent 3538c08 commit b8295af
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,16 @@ public class ApiClient {
return isForm ? formParams : obj;
}

/**
* Expand path template with variables
* @param pathTemplate path template with placeholders
* @param variables variables to replace
* @return path with placeholders replaced by variables
*/
public String expandPath(String pathTemplate, Map<String, Object> variables) {
return restTemplate.getUriTemplateHandler().expand(pathTemplate, variables).toString();
}

/**
* Invoke API by sending HTTP request with the given options.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {{invokerPackage}}.ApiClient;
{{/imports}}

{{^fullJavaUtil}}import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -72,7 +73,7 @@ public class {{classname}} {
// create path and map variables
final Map<String, Object> uriVariables = new HashMap<String, Object>();{{#pathParams}}
uriVariables.put("{{baseName}}", {{#collectionFormat}}apiClient.collectionPathParameterToString(ApiClient.CollectionFormat.valueOf("{{{collectionFormat}}}".toUpperCase()), {{{paramName}}}){{/collectionFormat}}{{^collectionFormat}}{{{paramName}}}{{/collectionFormat}});{{/pathParams}}{{/hasPathParams}}
String path = UriComponentsBuilder.fromPath("{{{path}}}"){{#hasPathParams}}.buildAndExpand(uriVariables){{/hasPathParams}}{{^hasPathParams}}.build(){{/hasPathParams}}.toUriString();
String path = apiClient.expandPath("{{{path}}}", {{#hasPathParams}}uriVariables{{/hasPathParams}}{{^hasPathParams}}Collections.<String, Object>emptyMap(){{/hasPathParams}});

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,16 @@ protected Object selectBody(Object obj, MultiValueMap<String, Object> formParams
return isForm ? formParams : obj;
}

/**
* Expand path template with variables
* @param pathTemplate path template with placeholders
* @param variables variables to replace
* @return path with placeholders replaced by variables
*/
public String expandPath(String pathTemplate, Map<String, Object> variables) {
return restTemplate.getUriTemplateHandler().expand(pathTemplate, variables).toString();
}

/**
* Invoke API by sending HTTP request with the given options.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.openapitools.client.model.Client;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -62,7 +63,7 @@ public Client call123testSpecialTags(Client body) throws RestClientException {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling call123testSpecialTags");
}

String path = UriComponentsBuilder.fromPath("/another-fake/dummy").build().toUriString();
String path = apiClient.expandPath("/another-fake/dummy", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.openapitools.client.model.XmlItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -69,7 +70,7 @@ public void createXmlItem(XmlItem xmlItem) throws RestClientException {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'xmlItem' when calling createXmlItem");
}

String path = UriComponentsBuilder.fromPath("/fake/create_xml_item").build().toUriString();
String path = apiClient.expandPath("/fake/create_xml_item", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -98,7 +99,7 @@ public void createXmlItem(XmlItem xmlItem) throws RestClientException {
public Boolean fakeOuterBooleanSerialize(Boolean body) throws RestClientException {
Object postBody = body;

String path = UriComponentsBuilder.fromPath("/fake/outer/boolean").build().toUriString();
String path = apiClient.expandPath("/fake/outer/boolean", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -127,7 +128,7 @@ public Boolean fakeOuterBooleanSerialize(Boolean body) throws RestClientExceptio
public OuterComposite fakeOuterCompositeSerialize(OuterComposite body) throws RestClientException {
Object postBody = body;

String path = UriComponentsBuilder.fromPath("/fake/outer/composite").build().toUriString();
String path = apiClient.expandPath("/fake/outer/composite", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -156,7 +157,7 @@ public OuterComposite fakeOuterCompositeSerialize(OuterComposite body) throws Re
public BigDecimal fakeOuterNumberSerialize(BigDecimal body) throws RestClientException {
Object postBody = body;

String path = UriComponentsBuilder.fromPath("/fake/outer/number").build().toUriString();
String path = apiClient.expandPath("/fake/outer/number", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -185,7 +186,7 @@ public BigDecimal fakeOuterNumberSerialize(BigDecimal body) throws RestClientExc
public String fakeOuterStringSerialize(String body) throws RestClientException {
Object postBody = body;

String path = UriComponentsBuilder.fromPath("/fake/outer/string").build().toUriString();
String path = apiClient.expandPath("/fake/outer/string", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -218,7 +219,7 @@ public void testBodyWithFileSchema(FileSchemaTestClass body) throws RestClientEx
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling testBodyWithFileSchema");
}

String path = UriComponentsBuilder.fromPath("/fake/body-with-file-schema").build().toUriString();
String path = apiClient.expandPath("/fake/body-with-file-schema", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -257,7 +258,7 @@ public void testBodyWithQueryParams(String query, User body) throws RestClientEx
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling testBodyWithQueryParams");
}

String path = UriComponentsBuilder.fromPath("/fake/body-with-query-params").build().toUriString();
String path = apiClient.expandPath("/fake/body-with-query-params", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -293,7 +294,7 @@ public Client testClientModel(Client body) throws RestClientException {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling testClientModel");
}

String path = UriComponentsBuilder.fromPath("/fake").build().toUriString();
String path = apiClient.expandPath("/fake", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -357,7 +358,7 @@ public void testEndpointParameters(BigDecimal number, Double _double, String pat
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter '_byte' when calling testEndpointParameters");
}

String path = UriComponentsBuilder.fromPath("/fake").build().toUriString();
String path = apiClient.expandPath("/fake", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -422,7 +423,7 @@ public void testEndpointParameters(BigDecimal number, Double _double, String pat
public void testEnumParameters(List<String> enumHeaderStringArray, String enumHeaderString, List<String> enumQueryStringArray, String enumQueryString, Integer enumQueryInteger, Double enumQueryDouble, List<String> enumFormStringArray, String enumFormString) throws RestClientException {
Object postBody = null;

String path = UriComponentsBuilder.fromPath("/fake").build().toUriString();
String path = apiClient.expandPath("/fake", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -485,7 +486,7 @@ public void testGroupParameters(Integer requiredStringGroup, Boolean requiredBoo
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'requiredInt64Group' when calling testGroupParameters");
}

String path = UriComponentsBuilder.fromPath("/fake").build().toUriString();
String path = apiClient.expandPath("/fake", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -526,7 +527,7 @@ public void testInlineAdditionalProperties(Map<String, String> param) throws Res
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'param' when calling testInlineAdditionalProperties");
}

String path = UriComponentsBuilder.fromPath("/fake/inline-additionalProperties").build().toUriString();
String path = apiClient.expandPath("/fake/inline-additionalProperties", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -565,7 +566,7 @@ public void testJsonFormData(String param, String param2) throws RestClientExcep
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'param2' when calling testJsonFormData");
}

String path = UriComponentsBuilder.fromPath("/fake/jsonFormData").build().toUriString();
String path = apiClient.expandPath("/fake/jsonFormData", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.openapitools.client.model.Client;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -62,7 +63,7 @@ public Client testClassname(Client body) throws RestClientException {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling testClassname");
}

String path = UriComponentsBuilder.fromPath("/fake_classname_test").build().toUriString();
String path = apiClient.expandPath("/fake_classname_test", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.openapitools.client.model.Pet;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -64,7 +65,7 @@ public void addPet(Pet body) throws RestClientException {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling addPet");
}

String path = UriComponentsBuilder.fromPath("/pet").build().toUriString();
String path = apiClient.expandPath("/pet", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -102,7 +103,7 @@ public void deletePet(Long petId, String apiKey) throws RestClientException {
// create path and map variables
final Map<String, Object> uriVariables = new HashMap<String, Object>();
uriVariables.put("petId", petId);
String path = UriComponentsBuilder.fromPath("/pet/{petId}").buildAndExpand(uriVariables).toUriString();
String path = apiClient.expandPath("/pet/{petId}", uriVariables);

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -138,7 +139,7 @@ public List<Pet> findPetsByStatus(List<String> status) throws RestClientExceptio
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'status' when calling findPetsByStatus");
}

String path = UriComponentsBuilder.fromPath("/pet/findByStatus").build().toUriString();
String path = apiClient.expandPath("/pet/findByStatus", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -175,7 +176,7 @@ public List<Pet> findPetsByTags(List<String> tags) throws RestClientException {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'tags' when calling findPetsByTags");
}

String path = UriComponentsBuilder.fromPath("/pet/findByTags").build().toUriString();
String path = apiClient.expandPath("/pet/findByTags", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -216,7 +217,7 @@ public Pet getPetById(Long petId) throws RestClientException {
// create path and map variables
final Map<String, Object> uriVariables = new HashMap<String, Object>();
uriVariables.put("petId", petId);
String path = UriComponentsBuilder.fromPath("/pet/{petId}").buildAndExpand(uriVariables).toUriString();
String path = apiClient.expandPath("/pet/{petId}", uriVariables);

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -252,7 +253,7 @@ public void updatePet(Pet body) throws RestClientException {
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling updatePet");
}

String path = UriComponentsBuilder.fromPath("/pet").build().toUriString();
String path = apiClient.expandPath("/pet", Collections.<String, Object>emptyMap());

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -290,7 +291,7 @@ public void updatePetWithForm(Long petId, String name, String status) throws Res
// create path and map variables
final Map<String, Object> uriVariables = new HashMap<String, Object>();
uriVariables.put("petId", petId);
String path = UriComponentsBuilder.fromPath("/pet/{petId}").buildAndExpand(uriVariables).toUriString();
String path = apiClient.expandPath("/pet/{petId}", uriVariables);

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -334,7 +335,7 @@ public ModelApiResponse uploadFile(Long petId, String additionalMetadata, File f
// create path and map variables
final Map<String, Object> uriVariables = new HashMap<String, Object>();
uriVariables.put("petId", petId);
String path = UriComponentsBuilder.fromPath("/pet/{petId}/uploadImage").buildAndExpand(uriVariables).toUriString();
String path = apiClient.expandPath("/pet/{petId}/uploadImage", uriVariables);

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down Expand Up @@ -385,7 +386,7 @@ public ModelApiResponse uploadFileWithRequiredFile(Long petId, File requiredFile
// create path and map variables
final Map<String, Object> uriVariables = new HashMap<String, Object>();
uriVariables.put("petId", petId);
String path = UriComponentsBuilder.fromPath("/fake/{petId}/uploadImageWithRequiredFile").buildAndExpand(uriVariables).toUriString();
String path = apiClient.expandPath("/fake/{petId}/uploadImageWithRequiredFile", uriVariables);

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
Expand Down
Loading

0 comments on commit b8295af

Please sign in to comment.