Support Json-serialized query parameters in Spring client RestClient and WebClient (#21725)

* Add so that a query parameter can be serialized as Json in the Spring clients RestClient and WebClient

* Update samples

* Add clientCodeGen test
This commit is contained in:
Mattias Sehlstedt
2025-08-12 04:26:33 +02:00
committed by GitHub
parent 4b88cf8243
commit 4d9fd4df92
46 changed files with 740 additions and 178 deletions

View File

@@ -13,6 +13,7 @@
package org.openapitools.client;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
@@ -436,6 +437,36 @@ public class ApiClient extends JavaTimeFormatter {
}
}
/**
* Converts a parameter to a {@link MultiValueMap} containing Json-serialized values for use in REST requests
* @param collectionFormat The format to convert to
* @param name The name of the parameter
* @param value The parameter's value
* @return a Map containing the Json-serialized String value(s) of the input parameter
*/
public MultiValueMap<String, String> parameterToMultiValueMapJson(CollectionFormat collectionFormat, String name, Object value) {
Collection<?> valueCollection;
if (value instanceof Collection) {
valueCollection = (Collection<?>) value;
} else {
try {
return parameterToMultiValueMap(collectionFormat, name, objectMapper.writeValueAsString(value));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
List<String> values = new ArrayList<>();
for(Object o : valueCollection) {
try {
values.add(objectMapper.writeValueAsString(o));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]");
}
/**
* Converts a parameter to a {@link MultiValueMap} for use in REST requests
* @param collectionFormat The format to convert to