mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-06 17:06:12 +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;
|
||||
@@ -438,6 +439,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
|
||||
|
||||
@@ -66,7 +66,6 @@ public class HeaderApi {
|
||||
final MultiValueMap<String, String> cookieParams = new LinkedMultiValueMap<>();
|
||||
final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<>();
|
||||
|
||||
|
||||
if (integerHeader != null)
|
||||
headerParams.add("integer_header", apiClient.parameterToString(integerHeader));
|
||||
if (booleanHeader != null)
|
||||
|
||||
@@ -71,7 +71,7 @@ public class QueryApi {
|
||||
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "enum_nonref_string_query", enumNonrefStringQuery));
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "enum_ref_string_query", enumRefStringQuery));
|
||||
|
||||
|
||||
final String[] localVarAccepts = {
|
||||
"text/plain"
|
||||
};
|
||||
@@ -149,7 +149,7 @@ public class QueryApi {
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "datetime_query", datetimeQuery));
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "date_query", dateQuery));
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "string_query", stringQuery));
|
||||
|
||||
|
||||
final String[] localVarAccepts = {
|
||||
"text/plain"
|
||||
};
|
||||
@@ -230,7 +230,7 @@ public class QueryApi {
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "integer_query", integerQuery));
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "boolean_query", booleanQuery));
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "string_query", stringQuery));
|
||||
|
||||
|
||||
final String[] localVarAccepts = {
|
||||
"text/plain"
|
||||
};
|
||||
@@ -312,7 +312,7 @@ public class QueryApi {
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "photoUrls", queryObject.getPhotoUrls()));
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "tags", queryObject.getTags()));
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "status", queryObject.getStatus()));
|
||||
|
||||
|
||||
final String[] localVarAccepts = {
|
||||
"text/plain"
|
||||
};
|
||||
@@ -383,7 +383,7 @@ public class QueryApi {
|
||||
final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<>();
|
||||
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "query_object", queryObject));
|
||||
|
||||
|
||||
final String[] localVarAccepts = {
|
||||
"text/plain"
|
||||
};
|
||||
@@ -454,7 +454,7 @@ public class QueryApi {
|
||||
final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<>();
|
||||
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "query_object", queryObject));
|
||||
|
||||
|
||||
final String[] localVarAccepts = {
|
||||
"text/plain"
|
||||
};
|
||||
@@ -525,7 +525,7 @@ public class QueryApi {
|
||||
final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<>();
|
||||
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "query_object", queryObject));
|
||||
|
||||
|
||||
final String[] localVarAccepts = {
|
||||
"text/plain"
|
||||
};
|
||||
@@ -596,7 +596,7 @@ public class QueryApi {
|
||||
final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<>();
|
||||
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "values", queryObject.getValues()));
|
||||
|
||||
|
||||
final String[] localVarAccepts = {
|
||||
"text/plain"
|
||||
};
|
||||
@@ -672,7 +672,7 @@ public class QueryApi {
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "photoUrls", queryObject.getPhotoUrls()));
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "tags", queryObject.getTags()));
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "status", queryObject.getStatus()));
|
||||
|
||||
|
||||
final String[] localVarAccepts = {
|
||||
"text/plain"
|
||||
};
|
||||
@@ -743,7 +743,7 @@ public class QueryApi {
|
||||
final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<>();
|
||||
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "query_object", queryObject));
|
||||
|
||||
|
||||
final String[] localVarAccepts = {
|
||||
"text/plain"
|
||||
};
|
||||
@@ -814,9 +814,9 @@ public class QueryApi {
|
||||
final MultiValueMap<String, String> cookieParams = new LinkedMultiValueMap<>();
|
||||
final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<>();
|
||||
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(null, "json_serialized_object_ref_string_query", jsonSerializedObjectRefStringQuery));
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMap(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "json_serialized_object_array_ref_string_query", jsonSerializedObjectArrayRefStringQuery));
|
||||
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMapJson(null, "json_serialized_object_ref_string_query", jsonSerializedObjectRefStringQuery));
|
||||
queryParams.putAll(apiClient.parameterToMultiValueMapJson(ApiClient.CollectionFormat.valueOf("csv".toUpperCase(Locale.ROOT)), "json_serialized_object_array_ref_string_query", jsonSerializedObjectArrayRefStringQuery));
|
||||
|
||||
final String[] localVarAccepts = {
|
||||
"text/plain"
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user