Skip to content

Commit b0c7578

Browse files
bclozelquaff
authored andcommitted
Improve query params in uri KeyValue with HTTP interface client
Prior to this commit, the HTTP interface client would create URI templates and name query params like so: "?{queryParam0}={queryParam0[0]}". While technically correct, the URI template is further used in observations as a KeyValue. This means that several service methods could result in having the exact same URI template even if they produce a different set of query params. This commit improves the naming of query params in the generated URI templates for better observability integration. Closes spring-projectsgh-34176
1 parent 69b7624 commit b0c7578

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestValues.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -464,16 +464,14 @@ private String appendQueryParams(
464464
String uriTemplate, Map<String, String> uriVars, MultiValueMap<String, String> requestParams) {
465465

466466
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uriTemplate);
467-
int i = 0;
468467
for (Map.Entry<String, List<String>> entry : requestParams.entrySet()) {
469-
String nameVar = "queryParam" + i;
468+
String nameVar = entry.getKey();
470469
uriVars.put(nameVar, entry.getKey());
471470
for (int j = 0; j < entry.getValue().size(); j++) {
472471
String valueVar = nameVar + "[" + j + "]";
473472
uriVars.put(valueVar, entry.getValue().get(j));
474473
uriComponentsBuilder.queryParam("{" + nameVar + "}", "{" + valueVar + "}");
475474
}
476-
i++;
477475
}
478476
return uriComponentsBuilder.build().toUriString();
479477
}

spring-web/src/test/java/org/springframework/web/service/invoker/HttpRequestValuesTests.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -79,17 +79,17 @@ void queryParamsWithUriTemplate() {
7979

8080
assertThat(uriTemplate)
8181
.isEqualTo("/path?" +
82-
"{queryParam0}={queryParam0[0]}&" +
83-
"{queryParam1}={queryParam1[0]}&" +
84-
"{queryParam1}={queryParam1[1]}");
82+
"{param1}={param1[0]}&" +
83+
"{param2}={param2[0]}&" +
84+
"{param2}={param2[1]}");
8585

8686
assertThat(requestValues.getUriVariables())
87-
.containsOnlyKeys("queryParam0", "queryParam1", "queryParam0[0]", "queryParam1[0]", "queryParam1[1]")
88-
.containsEntry("queryParam0", "param1")
89-
.containsEntry("queryParam1", "param2")
90-
.containsEntry("queryParam0[0]", "1st value")
91-
.containsEntry("queryParam1[0]", "2nd value A")
92-
.containsEntry("queryParam1[1]", "2nd value B");
87+
.containsOnlyKeys("param1", "param2", "param1[0]", "param2[0]", "param2[1]")
88+
.containsEntry("param1", "param1")
89+
.containsEntry("param2", "param2")
90+
.containsEntry("param1[0]", "1st value")
91+
.containsEntry("param2[0]", "2nd value A")
92+
.containsEntry("param2[1]", "2nd value B");
9393

9494
URI uri = UriComponentsBuilder.fromUriString(uriTemplate)
9595
.encode()
@@ -144,7 +144,13 @@ void requestPartAndRequestParam() {
144144
String uriTemplate = requestValues.getUriTemplate();
145145
assertThat(uriTemplate).isNotNull();
146146

147-
assertThat(uriTemplate).isEqualTo("/path?{queryParam0}={queryParam0[0]}");
147+
assertThat(uriTemplate).isEqualTo("/path?{query param}={query param[0]}");
148+
149+
URI uri = UriComponentsBuilder.fromUriString(uriTemplate)
150+
.encode()
151+
.build(requestValues.getUriVariables());
152+
assertThat(uri.toString())
153+
.isEqualTo("/path?query%20param=query%20value");
148154

149155
@SuppressWarnings("unchecked")
150156
MultiValueMap<String, Object> map = (MultiValueMap<String, Object>) requestValues.getBodyValue();

0 commit comments

Comments
 (0)