Skip to content

Commit b8295af

Browse files
unintendedmacjohnny
authored andcommitted
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
1 parent 3538c08 commit b8295af

16 files changed

Lines changed: 116 additions & 73 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,16 @@ public class ApiClient {
555555
return isForm ? formParams : obj;
556556
}
557557

558+
/**
559+
* Expand path template with variables
560+
* @param pathTemplate path template with placeholders
561+
* @param variables variables to replace
562+
* @return path with placeholders replaced by variables
563+
*/
564+
public String expandPath(String pathTemplate, Map<String, Object> variables) {
565+
return restTemplate.getUriTemplateHandler().expand(pathTemplate, variables).toString();
566+
}
567+
558568
/**
559569
* Invoke API by sending HTTP request with the given options.
560570
*

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {{invokerPackage}}.ApiClient;
66
{{/imports}}
77

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

7778
final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
7879
final HttpHeaders headerParams = new HttpHeaders();

samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,16 @@ protected Object selectBody(Object obj, MultiValueMap<String, Object> formParams
543543
return isForm ? formParams : obj;
544544
}
545545

546+
/**
547+
* Expand path template with variables
548+
* @param pathTemplate path template with placeholders
549+
* @param variables variables to replace
550+
* @return path with placeholders replaced by variables
551+
*/
552+
public String expandPath(String pathTemplate, Map<String, Object> variables) {
553+
return restTemplate.getUriTemplateHandler().expand(pathTemplate, variables).toString();
554+
}
555+
546556
/**
547557
* Invoke API by sending HTTP request with the given options.
548558
*

samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/AnotherFakeApi.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.openapitools.client.model.Client;
66

77
import java.util.ArrayList;
8+
import java.util.Collections;
89
import java.util.HashMap;
910
import java.util.List;
1011
import java.util.Locale;
@@ -62,7 +63,7 @@ public Client call123testSpecialTags(Client body) throws RestClientException {
6263
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling call123testSpecialTags");
6364
}
6465

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

6768
final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
6869
final HttpHeaders headerParams = new HttpHeaders();

samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeApi.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.openapitools.client.model.XmlItem;
1414

1515
import java.util.ArrayList;
16+
import java.util.Collections;
1617
import java.util.HashMap;
1718
import java.util.List;
1819
import java.util.Locale;
@@ -69,7 +70,7 @@ public void createXmlItem(XmlItem xmlItem) throws RestClientException {
6970
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'xmlItem' when calling createXmlItem");
7071
}
7172

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

570571
final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
571572
final HttpHeaders headerParams = new HttpHeaders();

samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.openapitools.client.model.Client;
66

77
import java.util.ArrayList;
8+
import java.util.Collections;
89
import java.util.HashMap;
910
import java.util.List;
1011
import java.util.Locale;
@@ -62,7 +63,7 @@ public Client testClassname(Client body) throws RestClientException {
6263
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling testClassname");
6364
}
6465

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

6768
final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
6869
final HttpHeaders headerParams = new HttpHeaders();

samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/PetApi.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.openapitools.client.model.Pet;
88

99
import java.util.ArrayList;
10+
import java.util.Collections;
1011
import java.util.HashMap;
1112
import java.util.List;
1213
import java.util.Locale;
@@ -64,7 +65,7 @@ public void addPet(Pet body) throws RestClientException {
6465
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'body' when calling addPet");
6566
}
6667

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

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

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

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

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

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

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

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

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

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

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

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

390391
final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
391392
final HttpHeaders headerParams = new HttpHeaders();

0 commit comments

Comments
 (0)