Refactor URI building in PrestashopClient to simplify query parameter encoding

This commit is contained in:
Vincent Guillet
2025-12-02 16:54:49 +01:00
parent bceedc8620
commit 177eb2eb5c

View File

@@ -43,48 +43,36 @@ public class PrestashopClient {
*/ */
private String buildUri(String path, MultiValueMap<String, String> params) { private String buildUri(String path, MultiValueMap<String, String> params) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
// baseUrl (ex: https://shop.gameovergne.fr/api)
sb.append(baseUrl); sb.append(baseUrl);
// path (on gère proprement le /)
if (path != null && !path.isEmpty()) { if (path != null && !path.isEmpty()) {
if (path.charAt(0) == '/') { if (path.charAt(0) == '/') sb.append(path);
sb.append(path); else sb.append('/').append(path);
} else {
sb.append('/').append(path);
}
} }
// query params
if (params != null && !params.isEmpty()) { if (params != null && !params.isEmpty()) {
boolean first = true; boolean first = true;
for (Map.Entry<String, List<String>> entry : params.entrySet()) { for (Map.Entry<String, List<String>> entry : params.entrySet()) {
String key = entry.getKey(); String key = entry.getKey();
List<String> values = entry.getValue(); List<String> values = entry.getValue();
if (values == null || values.isEmpty()) continue;
if (values == null || values.isEmpty()) {
continue;
}
String encodedKey = URLEncoder.encode(key, StandardCharsets.UTF_8);
for (String rawValue : values) { for (String rawValue : values) {
if (first) { if (first) { sb.append('?'); first = false; }
sb.append('?'); else sb.append('&');
first = false;
} else {
sb.append('&');
}
String encodedValue = rawValue == null // encode la clé
? "" sb.append(URLEncoder.encode(key, StandardCharsets.UTF_8));
: URLEncoder.encode(rawValue, StandardCharsets.UTF_8); sb.append('=');
sb.append(encodedKey) // on encode seulement les parties sensibles, PAS les crochets ni virgules
.append('=') String safeValue = rawValue
.append(encodedValue); .replace("[", "%5B")
.replace("]", "%5D")
.replace(" ", "")
.replace(",", ","); // laisse la virgule brute
sb.append(safeValue);
} }
} }
} }