From b8295afa272dd22a2f5a0fb53c37c710bcc8b5e6 Mon Sep 17 00:00:00 2001 From: unintended Date: Tue, 6 Aug 2019 13:56:43 +0300 Subject: [PATCH] Expand path templates via resttemplate's uriTemplateHandler (#3500) * Expand path templates via resttemplate's uriTemplateHandler * fix tests and compilation on lower jdk version * fix typo * re-generate samples --- .../libraries/resttemplate/ApiClient.mustache | 10 +++++++ .../Java/libraries/resttemplate/api.mustache | 3 ++- .../org/openapitools/client/ApiClient.java | 10 +++++++ .../client/api/AnotherFakeApi.java | 3 ++- .../org/openapitools/client/api/FakeApi.java | 27 ++++++++++--------- .../client/api/FakeClassnameTags123Api.java | 3 ++- .../org/openapitools/client/api/PetApi.java | 19 ++++++------- .../org/openapitools/client/api/StoreApi.java | 9 ++++--- .../org/openapitools/client/api/UserApi.java | 17 ++++++------ .../org/openapitools/client/ApiClient.java | 10 +++++++ .../client/api/AnotherFakeApi.java | 3 ++- .../org/openapitools/client/api/FakeApi.java | 27 ++++++++++--------- .../client/api/FakeClassnameTags123Api.java | 3 ++- .../org/openapitools/client/api/PetApi.java | 19 ++++++------- .../org/openapitools/client/api/StoreApi.java | 9 ++++--- .../org/openapitools/client/api/UserApi.java | 17 ++++++------ 16 files changed, 116 insertions(+), 73 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache index 4deaacb19ee..52e5f96e994 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache @@ -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 variables) { + return restTemplate.getUriTemplateHandler().expand(pathTemplate, variables).toString(); + } + /** * Invoke API by sending HTTP request with the given options. * diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api.mustache index 040d776efe9..79cdcd51ac1 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api.mustache @@ -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; @@ -72,7 +73,7 @@ public class {{classname}} { // create path and map variables final Map uriVariables = new HashMap();{{#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.emptyMap(){{/hasPathParams}}); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java index 6a61b3961c8..6ccafb53cc2 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java @@ -543,6 +543,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 variables) { + return restTemplate.getUriTemplateHandler().expand(pathTemplate, variables).toString(); + } + /** * Invoke API by sending HTTP request with the given options. * diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/AnotherFakeApi.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/AnotherFakeApi.java index b8f6e65d3e3..ffb5afa7d04 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/AnotherFakeApi.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/AnotherFakeApi.java @@ -5,6 +5,7 @@ import org.openapitools.client.ApiClient; 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; @@ -62,7 +63,7 @@ public class AnotherFakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeApi.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeApi.java index 9d607296978..4981d7ea3a8 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeApi.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeApi.java @@ -13,6 +13,7 @@ import org.openapitools.client.model.User; 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; @@ -69,7 +70,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -98,7 +99,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -127,7 +128,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -156,7 +157,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -185,7 +186,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -218,7 +219,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -257,7 +258,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -293,7 +294,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -357,7 +358,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -422,7 +423,7 @@ public class FakeApi { public void testEnumParameters(List enumHeaderStringArray, String enumHeaderString, List enumQueryStringArray, String enumQueryString, Integer enumQueryInteger, Double enumQueryDouble, List enumFormStringArray, String enumFormString) throws RestClientException { Object postBody = null; - String path = UriComponentsBuilder.fromPath("/fake").build().toUriString(); + String path = apiClient.expandPath("/fake", Collections.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -485,7 +486,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -526,7 +527,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -565,7 +566,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java index c0e2aef1afe..b619b51c261 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java @@ -5,6 +5,7 @@ import org.openapitools.client.ApiClient; 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; @@ -62,7 +63,7 @@ public class FakeClassnameTags123Api { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/PetApi.java index 4adb3a4e2a4..d012578f0fa 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/PetApi.java @@ -7,6 +7,7 @@ import org.openapitools.client.model.ModelApiResponse; 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; @@ -64,7 +65,7 @@ public class PetApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -102,7 +103,7 @@ public class PetApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("petId", petId); - String path = UriComponentsBuilder.fromPath("/pet/{petId}").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/pet/{petId}", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -138,7 +139,7 @@ public class PetApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -175,7 +176,7 @@ public class PetApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -216,7 +217,7 @@ public class PetApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("petId", petId); - String path = UriComponentsBuilder.fromPath("/pet/{petId}").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/pet/{petId}", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -252,7 +253,7 @@ public class PetApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -290,7 +291,7 @@ public class PetApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("petId", petId); - String path = UriComponentsBuilder.fromPath("/pet/{petId}").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/pet/{petId}", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -334,7 +335,7 @@ public class PetApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("petId", petId); - String path = UriComponentsBuilder.fromPath("/pet/{petId}/uploadImage").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/pet/{petId}/uploadImage", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -385,7 +386,7 @@ public class PetApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("petId", petId); - String path = UriComponentsBuilder.fromPath("/fake/{petId}/uploadImageWithRequiredFile").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/fake/{petId}/uploadImageWithRequiredFile", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/StoreApi.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/StoreApi.java index 0bf4604d28d..4488bd0ae0a 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/StoreApi.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/StoreApi.java @@ -5,6 +5,7 @@ import org.openapitools.client.ApiClient; import org.openapitools.client.model.Order; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -65,7 +66,7 @@ public class StoreApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("order_id", orderId); - String path = UriComponentsBuilder.fromPath("/store/order/{order_id}").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/store/order/{order_id}", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -91,7 +92,7 @@ public class StoreApi { public Map getInventory() throws RestClientException { Object postBody = null; - String path = UriComponentsBuilder.fromPath("/store/inventory").build().toUriString(); + String path = apiClient.expandPath("/store/inventory", Collections.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -130,7 +131,7 @@ public class StoreApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("order_id", orderId); - String path = UriComponentsBuilder.fromPath("/store/order/{order_id}").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/store/order/{order_id}", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -165,7 +166,7 @@ public class StoreApi { throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling placeOrder"); } - String path = UriComponentsBuilder.fromPath("/store/order").build().toUriString(); + String path = apiClient.expandPath("/store/order", Collections.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/UserApi.java index 586e2320a80..1d2d59616b6 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/UserApi.java @@ -5,6 +5,7 @@ import org.openapitools.client.ApiClient; import org.openapitools.client.model.User; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -61,7 +62,7 @@ public class UserApi { throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling createUser"); } - String path = UriComponentsBuilder.fromPath("/user").build().toUriString(); + String path = apiClient.expandPath("/user", Collections.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -92,7 +93,7 @@ public class UserApi { throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling createUsersWithArrayInput"); } - String path = UriComponentsBuilder.fromPath("/user/createWithArray").build().toUriString(); + String path = apiClient.expandPath("/user/createWithArray", Collections.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -123,7 +124,7 @@ public class UserApi { throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling createUsersWithListInput"); } - String path = UriComponentsBuilder.fromPath("/user/createWithList").build().toUriString(); + String path = apiClient.expandPath("/user/createWithList", Collections.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -158,7 +159,7 @@ public class UserApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("username", username); - String path = UriComponentsBuilder.fromPath("/user/{username}").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/user/{username}", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -195,7 +196,7 @@ public class UserApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("username", username); - String path = UriComponentsBuilder.fromPath("/user/{username}").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/user/{username}", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -236,7 +237,7 @@ public class UserApi { throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'password' when calling loginUser"); } - String path = UriComponentsBuilder.fromPath("/user/login").build().toUriString(); + String path = apiClient.expandPath("/user/login", Collections.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -266,7 +267,7 @@ public class UserApi { public void logoutUser() throws RestClientException { Object postBody = null; - String path = UriComponentsBuilder.fromPath("/user/logout").build().toUriString(); + String path = apiClient.expandPath("/user/logout", Collections.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -307,7 +308,7 @@ public class UserApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("username", username); - String path = UriComponentsBuilder.fromPath("/user/{username}").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/user/{username}", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java index ad850d363d7..c49d73bcfb0 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java @@ -538,6 +538,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 variables) { + return restTemplate.getUriTemplateHandler().expand(pathTemplate, variables).toString(); + } + /** * Invoke API by sending HTTP request with the given options. * diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/AnotherFakeApi.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/AnotherFakeApi.java index b8f6e65d3e3..ffb5afa7d04 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/AnotherFakeApi.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/AnotherFakeApi.java @@ -5,6 +5,7 @@ import org.openapitools.client.ApiClient; 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; @@ -62,7 +63,7 @@ public class AnotherFakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeApi.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeApi.java index 9d607296978..4981d7ea3a8 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeApi.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeApi.java @@ -13,6 +13,7 @@ import org.openapitools.client.model.User; 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; @@ -69,7 +70,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -98,7 +99,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -127,7 +128,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -156,7 +157,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -185,7 +186,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -218,7 +219,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -257,7 +258,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -293,7 +294,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -357,7 +358,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -422,7 +423,7 @@ public class FakeApi { public void testEnumParameters(List enumHeaderStringArray, String enumHeaderString, List enumQueryStringArray, String enumQueryString, Integer enumQueryInteger, Double enumQueryDouble, List enumFormStringArray, String enumFormString) throws RestClientException { Object postBody = null; - String path = UriComponentsBuilder.fromPath("/fake").build().toUriString(); + String path = apiClient.expandPath("/fake", Collections.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -485,7 +486,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -526,7 +527,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -565,7 +566,7 @@ public class FakeApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java index c0e2aef1afe..b619b51c261 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java @@ -5,6 +5,7 @@ import org.openapitools.client.ApiClient; 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; @@ -62,7 +63,7 @@ public class FakeClassnameTags123Api { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/PetApi.java index 4adb3a4e2a4..d012578f0fa 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/PetApi.java @@ -7,6 +7,7 @@ import org.openapitools.client.model.ModelApiResponse; 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; @@ -64,7 +65,7 @@ public class PetApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -102,7 +103,7 @@ public class PetApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("petId", petId); - String path = UriComponentsBuilder.fromPath("/pet/{petId}").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/pet/{petId}", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -138,7 +139,7 @@ public class PetApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -175,7 +176,7 @@ public class PetApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -216,7 +217,7 @@ public class PetApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("petId", petId); - String path = UriComponentsBuilder.fromPath("/pet/{petId}").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/pet/{petId}", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -252,7 +253,7 @@ public class PetApi { 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.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -290,7 +291,7 @@ public class PetApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("petId", petId); - String path = UriComponentsBuilder.fromPath("/pet/{petId}").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/pet/{petId}", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -334,7 +335,7 @@ public class PetApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("petId", petId); - String path = UriComponentsBuilder.fromPath("/pet/{petId}/uploadImage").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/pet/{petId}/uploadImage", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -385,7 +386,7 @@ public class PetApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("petId", petId); - String path = UriComponentsBuilder.fromPath("/fake/{petId}/uploadImageWithRequiredFile").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/fake/{petId}/uploadImageWithRequiredFile", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/StoreApi.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/StoreApi.java index 0bf4604d28d..4488bd0ae0a 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/StoreApi.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/StoreApi.java @@ -5,6 +5,7 @@ import org.openapitools.client.ApiClient; import org.openapitools.client.model.Order; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -65,7 +66,7 @@ public class StoreApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("order_id", orderId); - String path = UriComponentsBuilder.fromPath("/store/order/{order_id}").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/store/order/{order_id}", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -91,7 +92,7 @@ public class StoreApi { public Map getInventory() throws RestClientException { Object postBody = null; - String path = UriComponentsBuilder.fromPath("/store/inventory").build().toUriString(); + String path = apiClient.expandPath("/store/inventory", Collections.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -130,7 +131,7 @@ public class StoreApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("order_id", orderId); - String path = UriComponentsBuilder.fromPath("/store/order/{order_id}").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/store/order/{order_id}", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -165,7 +166,7 @@ public class StoreApi { throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling placeOrder"); } - String path = UriComponentsBuilder.fromPath("/store/order").build().toUriString(); + String path = apiClient.expandPath("/store/order", Collections.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/UserApi.java index 586e2320a80..1d2d59616b6 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/UserApi.java @@ -5,6 +5,7 @@ import org.openapitools.client.ApiClient; import org.openapitools.client.model.User; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -61,7 +62,7 @@ public class UserApi { throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling createUser"); } - String path = UriComponentsBuilder.fromPath("/user").build().toUriString(); + String path = apiClient.expandPath("/user", Collections.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -92,7 +93,7 @@ public class UserApi { throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling createUsersWithArrayInput"); } - String path = UriComponentsBuilder.fromPath("/user/createWithArray").build().toUriString(); + String path = apiClient.expandPath("/user/createWithArray", Collections.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -123,7 +124,7 @@ public class UserApi { throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling createUsersWithListInput"); } - String path = UriComponentsBuilder.fromPath("/user/createWithList").build().toUriString(); + String path = apiClient.expandPath("/user/createWithList", Collections.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -158,7 +159,7 @@ public class UserApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("username", username); - String path = UriComponentsBuilder.fromPath("/user/{username}").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/user/{username}", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -195,7 +196,7 @@ public class UserApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("username", username); - String path = UriComponentsBuilder.fromPath("/user/{username}").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/user/{username}", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -236,7 +237,7 @@ public class UserApi { throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'password' when calling loginUser"); } - String path = UriComponentsBuilder.fromPath("/user/login").build().toUriString(); + String path = apiClient.expandPath("/user/login", Collections.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -266,7 +267,7 @@ public class UserApi { public void logoutUser() throws RestClientException { Object postBody = null; - String path = UriComponentsBuilder.fromPath("/user/logout").build().toUriString(); + String path = apiClient.expandPath("/user/logout", Collections.emptyMap()); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders(); @@ -307,7 +308,7 @@ public class UserApi { // create path and map variables final Map uriVariables = new HashMap(); uriVariables.put("username", username); - String path = UriComponentsBuilder.fromPath("/user/{username}").buildAndExpand(uriVariables).toUriString(); + String path = apiClient.expandPath("/user/{username}", uriVariables); final MultiValueMap queryParams = new LinkedMultiValueMap(); final HttpHeaders headerParams = new HttpHeaders();