Skip to content

Commit d927d64

Browse files
committed
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 gh-34176
1 parent 8544435 commit d927d64

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.
@@ -492,16 +492,14 @@ private String appendQueryParams(
492492
String uriTemplate, Map<String, String> uriVars, MultiValueMap<String, String> requestParams) {
493493

494494
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uriTemplate);
495-
int i = 0;
496495
for (Map.Entry<String, List<String>> entry : requestParams.entrySet()) {
497-
String nameVar = "queryParam" + i;
496+
String nameVar = entry.getKey();
498497
uriVars.put(nameVar, entry.getKey());
499498
for (int j = 0; j < entry.getValue().size(); j++) {
500499
String valueVar = nameVar + "[" + j + "]";
501500
uriVars.put(valueVar, entry.getValue().get(j));
502501
uriComponentsBuilder.queryParam("{" + nameVar + "}", "{" + valueVar + "}");
503502
}
504-
i++;
505503
}
506504
return uriComponentsBuilder.build().toUriString();
507505
}

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)