From b16234f49d9ba91b9ad252d8ca2628625a478f0d Mon Sep 17 00:00:00 2001 From: nicolasiannarilli <92038234+nicolasiannarilli@users.noreply.github.com> Date: Wed, 7 Feb 2024 17:27:36 +0100 Subject: [PATCH] fix resteasy ApiClient.mustache fix serialize multipart with files fix deserialize byte and file --- .../Java/libraries/resteasy/ApiClient.mustache | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/resteasy/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/resteasy/ApiClient.mustache index 725e01185296..c15c0e9e0c61 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/resteasy/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/resteasy/ApiClient.mustache @@ -9,6 +9,7 @@ import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.file.Files; import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -495,7 +496,14 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { MultipartFormDataOutput multipart = new MultipartFormDataOutput(); //MultiPart multiPart = new MultiPart(); for (Entry param: formParams.entrySet()) { - if (param.getValue() instanceof File) { + if( param.getValue() instanceof List && !( ( List ) param.getValue() ).isEmpty() + && ( ( List ) param.getValue() ).get( 0 ) instanceof File ) { + @SuppressWarnings( "unchecked" ) + List files = ( List ) param.getValue(); + for( File file : files ) { + multipart.addFormData(param.getKey(),new FileInputStream(file),MediaType.APPLICATION_OCTET_STREAM_TYPE, file.getName()); + } + } else if (param.getValue() instanceof File) { File file = (File) param.getValue(); try { multipart.addFormData(param.getKey(),new FileInputStream(file),MediaType.APPLICATION_OCTET_STREAM_TYPE, file.getName()); @@ -534,10 +542,10 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return null; } - if ("byte[]".equals(returnType.toString())) { + if (returnType.getRawType().equals(byte[].class)) { // Handle binary response (byte array). return (T) response.readEntity(byte[].class); - } else if (returnType.equals(File.class)) { + } else if (returnType.getRawType().equals(File.class)) { // Handle file downloading. @SuppressWarnings("unchecked") T file = (T) downloadFileFromResponse(response); @@ -563,7 +571,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { public File downloadFileFromResponse(Response response) throws ApiException { try { File file = prepareDownloadFile(response); - Files.copy(response.readEntity(InputStream.class), file.toPath()); + Files.copy(response.readEntity(InputStream.class), file.toPath(), StandardCopyOption.REPLACE_EXISTING); return file; } catch (IOException e) { throw new ApiException(e);