fix #6134 by considering the type List<File> (#11361)

The method buildRequestBodyMultipart in ApiClient.java now recognizes if
an input parameter is an instance of List.
This commit is contained in:
Lennart Schwahn
2022-02-15 03:35:46 +01:00
committed by GitHub
parent 7555018aa6
commit ba04720898
5 changed files with 95 additions and 15 deletions

View File

@@ -1522,9 +1522,12 @@ public class ApiClient {
for (Entry<String, Object> param : formParams.entrySet()) { for (Entry<String, Object> param : formParams.entrySet()) {
if (param.getValue() instanceof File) { if (param.getValue() instanceof File) {
File file = (File) param.getValue(); File file = (File) param.getValue();
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\"; filename=\"" + file.getName() + "\""); addPartToMultiPartBuilder(mpBuilder, param.getKey(), file);
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file)); } else if (param.getValue() instanceof List) {
mpBuilder.addPart(partHeaders, RequestBody.create(file, mediaType)); List<File> files = (List<File>) param.getValue();
for (File file : files) {
addPartToMultiPartBuilder(mpBuilder, param.getKey(), file);
}
} else { } else {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\""); Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\"");
mpBuilder.addPart(partHeaders, RequestBody.create(parameterToString(param.getValue()), null)); mpBuilder.addPart(partHeaders, RequestBody.create(parameterToString(param.getValue()), null));
@@ -1548,6 +1551,19 @@ public class ApiClient {
} }
} }
/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
mpBuilder.addPart(partHeaders, RequestBody.create(file, mediaType));
}
/** /**
* Get network interceptor to add it to the httpClient to track download progress for * Get network interceptor to add it to the httpClient to track download progress for
* async requests. * async requests.

View File

@@ -1317,9 +1317,12 @@ public class ApiClient {
for (Entry<String, Object> param : formParams.entrySet()) { for (Entry<String, Object> param : formParams.entrySet()) {
if (param.getValue() instanceof File) { if (param.getValue() instanceof File) {
File file = (File) param.getValue(); File file = (File) param.getValue();
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\"; filename=\"" + file.getName() + "\""); addPartToMultiPartBuilder(mpBuilder, param.getKey(), file);
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file)); } else if (param.getValue() instanceof List) {
mpBuilder.addPart(partHeaders, RequestBody.create(file, mediaType)); List<File> files = (List<File>) param.getValue();
for (File file : files) {
addPartToMultiPartBuilder(mpBuilder, param.getKey(), file);
}
} else { } else {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\""); Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\"");
mpBuilder.addPart(partHeaders, RequestBody.create(parameterToString(param.getValue()), null)); mpBuilder.addPart(partHeaders, RequestBody.create(parameterToString(param.getValue()), null));
@@ -1343,6 +1346,19 @@ public class ApiClient {
} }
} }
/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
mpBuilder.addPart(partHeaders, RequestBody.create(file, mediaType));
}
/** /**
* Get network interceptor to add it to the httpClient to track download progress for * Get network interceptor to add it to the httpClient to track download progress for
* async requests. * async requests.

View File

@@ -1395,9 +1395,12 @@ public class ApiClient {
for (Entry<String, Object> param : formParams.entrySet()) { for (Entry<String, Object> param : formParams.entrySet()) {
if (param.getValue() instanceof File) { if (param.getValue() instanceof File) {
File file = (File) param.getValue(); File file = (File) param.getValue();
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\"; filename=\"" + file.getName() + "\""); addPartToMultiPartBuilder(mpBuilder, param.getKey(), file);
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file)); } else if (param.getValue() instanceof List) {
mpBuilder.addPart(partHeaders, RequestBody.create(file, mediaType)); List<File> files = (List<File>) param.getValue();
for (File file : files) {
addPartToMultiPartBuilder(mpBuilder, param.getKey(), file);
}
} else { } else {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\""); Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\"");
mpBuilder.addPart(partHeaders, RequestBody.create(parameterToString(param.getValue()), null)); mpBuilder.addPart(partHeaders, RequestBody.create(parameterToString(param.getValue()), null));
@@ -1421,6 +1424,19 @@ public class ApiClient {
} }
} }
/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
mpBuilder.addPart(partHeaders, RequestBody.create(file, mediaType));
}
/** /**
* Get network interceptor to add it to the httpClient to track download progress for * Get network interceptor to add it to the httpClient to track download progress for
* async requests. * async requests.

View File

@@ -1396,9 +1396,12 @@ public class ApiClient {
for (Entry<String, Object> param : formParams.entrySet()) { for (Entry<String, Object> param : formParams.entrySet()) {
if (param.getValue() instanceof File) { if (param.getValue() instanceof File) {
File file = (File) param.getValue(); File file = (File) param.getValue();
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\"; filename=\"" + file.getName() + "\""); addPartToMultiPartBuilder(mpBuilder, param.getKey(), file);
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file)); } else if (param.getValue() instanceof List) {
mpBuilder.addPart(partHeaders, RequestBody.create(file, mediaType)); List<File> files = (List<File>) param.getValue();
for (File file : files) {
addPartToMultiPartBuilder(mpBuilder, param.getKey(), file);
}
} else { } else {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\""); Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\"");
mpBuilder.addPart(partHeaders, RequestBody.create(parameterToString(param.getValue()), null)); mpBuilder.addPart(partHeaders, RequestBody.create(parameterToString(param.getValue()), null));
@@ -1422,6 +1425,19 @@ public class ApiClient {
} }
} }
/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
mpBuilder.addPart(partHeaders, RequestBody.create(file, mediaType));
}
/** /**
* Get network interceptor to add it to the httpClient to track download progress for * Get network interceptor to add it to the httpClient to track download progress for
* async requests. * async requests.

View File

@@ -1415,9 +1415,12 @@ public class ApiClient {
for (Entry<String, Object> param : formParams.entrySet()) { for (Entry<String, Object> param : formParams.entrySet()) {
if (param.getValue() instanceof File) { if (param.getValue() instanceof File) {
File file = (File) param.getValue(); File file = (File) param.getValue();
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\"; filename=\"" + file.getName() + "\""); addPartToMultiPartBuilder(mpBuilder, param.getKey(), file);
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file)); } else if (param.getValue() instanceof List) {
mpBuilder.addPart(partHeaders, RequestBody.create(file, mediaType)); List<File> files = (List<File>) param.getValue();
for (File file : files) {
addPartToMultiPartBuilder(mpBuilder, param.getKey(), file);
}
} else { } else {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\""); Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\"");
mpBuilder.addPart(partHeaders, RequestBody.create(parameterToString(param.getValue()), null)); mpBuilder.addPart(partHeaders, RequestBody.create(parameterToString(param.getValue()), null));
@@ -1441,6 +1444,19 @@ public class ApiClient {
} }
} }
/**
* Add a Content-Disposition Header for the given key and file to the MultipartBody Builder.
*
* @param mpBuilder MultipartBody.Builder
* @param key The key of the Header element
* @param file The file to add to the Header
*/
private void addPartToMultiPartBuilder(MultipartBody.Builder mpBuilder, String key, File file) {
Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + key + "\"; filename=\"" + file.getName() + "\"");
MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file));
mpBuilder.addPart(partHeaders, RequestBody.create(file, mediaType));
}
/** /**
* Get network interceptor to add it to the httpClient to track download progress for * Get network interceptor to add it to the httpClient to track download progress for
* async requests. * async requests.