diff --git a/clients/java/src/test/java/com/phrase/client/api/UploadsApiTest.java b/clients/java/src/test/java/com/phrase/client/api/UploadsApiTest.java index 7b3e6832..a19a8408 100644 --- a/clients/java/src/test/java/com/phrase/client/api/UploadsApiTest.java +++ b/clients/java/src/test/java/com/phrase/client/api/UploadsApiTest.java @@ -37,6 +37,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * API tests for UploadsApi @@ -104,18 +106,29 @@ public void uploadCreateTest() throws ApiException, IOException, InterruptedExce Boolean skipUnverification = null; String fileEncoding = null; Object localeMapping = null; - Object formatOptions = null; Boolean autotranslate = null; Boolean markReviewed = null; Boolean tagOnlyAffectedKeys = null; - Upload response = api.uploadCreate(projectId, file, fileFormat, localeId, xPhraseAppOTP, branch, tags, updateTranslations, updateTranslationKeys, updateTranslationsOnSourceMatch, updateDescriptions, convertEmoji, skipUploadTags, skipUnverification, fileEncoding, localeMapping, formatOptions, autotranslate, markReviewed, tagOnlyAffectedKeys); + + Map nestedFormatOptionsMap = new HashMap<>(); + nestedFormatOptionsMap.put("nested_option", "sub_option"); + + Map formatOptionsMap = new HashMap<>(); + formatOptionsMap.put("omit_separator_space", "true"); + formatOptionsMap.put("fallback_language", "en"); + formatOptionsMap.put("more_options", nestedFormatOptionsMap); + + Upload response = api.uploadCreate(projectId, file, fileFormat, localeId, xPhraseAppOTP, branch, tags, updateTranslations, updateTranslationKeys, updateTranslationsOnSourceMatch, updateDescriptions, convertEmoji, skipUploadTags, skipUnverification, fileEncoding, localeMapping, formatOptionsMap, autotranslate, markReviewed, tagOnlyAffectedKeys); Assert.assertEquals("valid id returned", "id_example", response.getId()); Assert.assertEquals("valid creation date returned", OffsetDateTime.parse("2015-01-28T09:52:53Z"), response.getCreatedAt()); RecordedRequest recordedRequest = mockBackend.takeRequest(); Assert.assertEquals("Request path", "//projects/projectId_example/uploads", recordedRequest.getPath()); - Assert.assertTrue("Request payload", recordedRequest.getBody().readUtf8().contains("Content-Disposition: form-data; name=\"file\"")); + String requestBody = recordedRequest.getBody().readUtf8(); + Assert.assertTrue("payload contains filename", requestBody.contains("Content-Disposition: form-data; name=\"file\"")); + Assert.assertTrue("payload contains fileFormat", Pattern.compile("Content-Disposition: form-data; name=\"file_format\"\\s+Content-Length: 11\\s+simple_json", Pattern.MULTILINE).matcher(requestBody).find()); + Assert.assertTrue("payload contains nested format options", Pattern.compile("Content-Disposition: form-data; name=\"format_options\\[more_options\\]\\[nested_option\\]\"\\s+Content-Length: 10\\s+sub_option", Pattern.MULTILINE).matcher(requestBody).find()); } /** diff --git a/openapi-generator/templates/java/libraries/okhttp-gson/ApiClient.mustache b/openapi-generator/templates/java/libraries/okhttp-gson/ApiClient.mustache index 44f1275d..23af378c 100644 --- a/openapi-generator/templates/java/libraries/okhttp-gson/ApiClient.mustache +++ b/openapi-generator/templates/java/libraries/okhttp-gson/ApiClient.mustache @@ -667,14 +667,11 @@ public class ApiClient { {{! TODO check why nested params ordering is different }} params.addAll(parsedMappedParams(key, value, new ArrayList())); } - } - return params; } - /** * Formats the specified query parameter to a list containing a single {@code Pair} object. * @@ -1348,8 +1345,12 @@ public class ApiClient { MediaType mediaType = MediaType.parse(guessContentTypeFromFile(file)); mpBuilder.addPart(partHeaders, RequestBody.create(mediaType, file)); } else { - Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + param.getKey() + "\""); - mpBuilder.addPart(partHeaders, RequestBody.create(null, parameterToString(param.getValue()))); + // flatten the nested structures first + List flatParams = parsedMappedParams(param.getKey(), param.getValue(), new ArrayList()); + for (Pair pair : flatParams) { + Headers partHeaders = Headers.of("Content-Disposition", "form-data; name=\"" + pair.getName() + "\""); + mpBuilder.addPart(partHeaders, RequestBody.create(null, parameterToString(pair.getValue()))); + } } } return mpBuilder.build(); @@ -1473,11 +1474,11 @@ public class ApiClient { for(Map.Entry entry : mappedValue.entrySet()){ String nestedKey = key + "[" + entry.getKey() + "]"; - parsedMappedParams(nestedKey, entry.getValue(), params); + parsedMappedParams(nestedKey, entry.getValue(), params); } } else{ - params.add(new Pair(key, parameterToString(value))); + params.add(new Pair(key, parameterToString(value))); } return params;