JAX-RS BeanParam issue with multipart/formdata and file upload
See original GitHub issueI try to put the form parameters of a multipart request into a bean like this:
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadWithBean(@Parameter @BeanParam UploadRequest personData) {
InputStream upload = personData.getUpload();
if (upload == null) {
return Response.ok(UploadResponse.FAILED).build();
}
return Response.ok(UploadResponse.SUCCESS).build();
}
In this case, the JSON produced ist the following:
"schema": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"picture": {
"$ref": "#/components/schemas/FormDataContentDisposition"
}
}
}
instead of (without the @BeanParam annotation).
"schema": {
"$ref": "#/components/schemas/UploadRequest"
}
If I leave the @BeanParam annotation, i would get a 415 response code, so this seems to be the right way.
If I further annotate the file-parameter InputStream of my bean, the upload will not occur and InputStream is null.
@Schema(name = "UploadRequest", title="Schema for Upload")
public class UploadRequest {
@FormDataParam("name")
// @Schema(name="name", type="string", accessMode=AccessMode.READ_WRITE)
private String name;
@FormDataParam("picture")
@Schema(name="picture", type="string", format="binary") // <----- upload is NULL when @Schema is present
private InputStream upload;
@FormDataParam("picture")
private FormDataContentDisposition disposition;
}
See the provided example to test. swagger-test-upload.zip
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
How do I do a multipart/form file upload with jax-rs?
The key is to leverage the @MultipartForm annotations that comes with RESTEasy. This enables you to define a POJO that contains all the...
Read more >Multipart FileUpload – breaking changes between JAX-RS 1.1 ...
This solution works in WebSphere 8.5, 9.0 and Liberty. It solves the breaking changes between JAX-RS 1.1 and JAX-RS 2.0. Without drawbacks.
Read more >Jersey 2.37 User Guide - GitHub Pages
Creating JAX-RS application with MultiPart feature enabled. ... example the following extracts the form parameter named "name" from the POSTed form data:.
Read more >JAX-RS – TickLint
Jersey (JAX-RS) supports multiple files upload. A dynamic number of files can also be uploaded. In this tutorial we will solve the following...
Read more >File Uploads with JAX-RS 2 - No Fluff Just Stuff
If you search for how to upload a file to a JAX-RS 2 endpoint, ... be of type multipart/form-data , which @FormParam doesn't...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The
@RequestBodyis still needed to work properly (using 2.1.4). When the mapping ist annotated, it produces the following JSON:Without annotation, I get:
The former is correct, but the “upload”-schema is useless, or am I wrong? Provided example is attached.
swagger-test-upload2.zip
Turns out that the issue was that we were using private fields with an underscore at the end, and so some part of Jackson/Swagger core wasn’t able to apply the annotations (pathparam, queryparam, etc…) to the interpreted field from the public getter/setter like so:
So in this example, Swagger core didn’t see that privateField was an accessible PathParam. I’m fine with this limitation, and I see there is no simple, robust way for Swagger core to correlate the field with the method, but I’m still a little confused because the beanParam resolution does happen correctly on the server - ie, Spring is able to properly inject the BeanParam, so why can’t Swagger core use the logic that Spring uses?