Missing part in MultipartFormDataOutput depending on order of keys

Viewed 100

I have a Java REST endpoint which should return a multipart response with a json document and a pdf file. I chose "form-data" and not "mixed" because I want to assign a name to the parts in case I have multiple different json or file parts in the future.

The problem: In the output, the parts seem to be sorted depending on the key I specify, which makes sense, since formDataMap of MultipartFormDataOutput is a HashMap. If the json part is first, everything works as it should. BUT if the file part is sorted first, then the json part is missing in the output, as well as the form-data boundary at the end.

I feel I've tried every possible variation by switching out media types, response types, annotations, trying different pdf files, etc but nothing seems to help.

Does anyone have an idea how to explain this behavior? Any help is welcome, thanks!

@POST
@Path("test")
@Consumes(MediaType.APPLICATION_JSON)
@Produces("multipart/form-data")
public Response test(MyObject o)
{
   // process input and get the byte arrays for the json document and pdf file

   var output = new MultipartFormDataOutput();
   output.addFormData("foo.json", new ByteArrayInputStream(myJsonByteArray), MediaType.APPLICATION_JSON_TYPE.withCharset("UTF-8"), "foo.json");
   output.addFormData("document.pdf", new ByteArrayInputStream(myPdfByteArray), MediaType.valueOf("application/pdf"), "document.pdf");
   return Response.ok(output).build();
}

I'm on Quarkus 1.13.7 with Resteasy 4.5.12

1 Answers

Not sure if this helps, but try wrapping MultipartFormDataOutput with GenericEntity before returning.

GenericEntity<MultipartFormDataOutput> outputGen = new GenericEntity<MultipartFormDataOutput>(output) {};

return Response.ok(outputGen).build();
Related