mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-20 16:37:11 +00:00
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:
committed by
GitHub
parent
4b88cf8243
commit
4d9fd4df92
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user