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
This commit is contained in:
Brian Clozel 2025-01-02 20:00:38 +01:00
parent 8544435833
commit d927d64c40
2 changed files with 19 additions and 15 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -492,16 +492,14 @@ public class HttpRequestValues {
String uriTemplate, Map<String, String> uriVars, MultiValueMap<String, String> requestParams) {
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(uriTemplate);
int i = 0;
for (Map.Entry<String, List<String>> entry : requestParams.entrySet()) {
String nameVar = "queryParam" + i;
String nameVar = entry.getKey();
uriVars.put(nameVar, entry.getKey());
for (int j = 0; j < entry.getValue().size(); j++) {
String valueVar = nameVar + "[" + j + "]";
uriVars.put(valueVar, entry.getValue().get(j));
uriComponentsBuilder.queryParam("{" + nameVar + "}", "{" + valueVar + "}");
}
i++;
}
return uriComponentsBuilder.build().toUriString();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -79,17 +79,17 @@ class HttpRequestValuesTests {
assertThat(uriTemplate)
.isEqualTo("/path?" +
"{queryParam0}={queryParam0[0]}&" +
"{queryParam1}={queryParam1[0]}&" +
"{queryParam1}={queryParam1[1]}");
"{param1}={param1[0]}&" +
"{param2}={param2[0]}&" +
"{param2}={param2[1]}");
assertThat(requestValues.getUriVariables())
.containsOnlyKeys("queryParam0", "queryParam1", "queryParam0[0]", "queryParam1[0]", "queryParam1[1]")
.containsEntry("queryParam0", "param1")
.containsEntry("queryParam1", "param2")
.containsEntry("queryParam0[0]", "1st value")
.containsEntry("queryParam1[0]", "2nd value A")
.containsEntry("queryParam1[1]", "2nd value B");
.containsOnlyKeys("param1", "param2", "param1[0]", "param2[0]", "param2[1]")
.containsEntry("param1", "param1")
.containsEntry("param2", "param2")
.containsEntry("param1[0]", "1st value")
.containsEntry("param2[0]", "2nd value A")
.containsEntry("param2[1]", "2nd value B");
URI uri = UriComponentsBuilder.fromUriString(uriTemplate)
.encode()
@ -144,7 +144,13 @@ class HttpRequestValuesTests {
String uriTemplate = requestValues.getUriTemplate();
assertThat(uriTemplate).isNotNull();
assertThat(uriTemplate).isEqualTo("/path?{queryParam0}={queryParam0[0]}");
assertThat(uriTemplate).isEqualTo("/path?{query param}={query param[0]}");
URI uri = UriComponentsBuilder.fromUriString(uriTemplate)
.encode()
.build(requestValues.getUriVariables());
assertThat(uri.toString())
.isEqualTo("/path?query%20param=query%20value");
@SuppressWarnings("unchecked")
MultiValueMap<String, Object> map = (MultiValueMap<String, Object>) requestValues.getBodyValue();