question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JAX-RS BeanParam issue with multipart/formdata and file upload

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
markusdemetzcommented, Jul 29, 2020

#3580 (available in next release 2.1.3) fixes the handling of hidden annotations with @BeanParam, therefore the @RequestBody annotation on the resource method will not be needed.

The @RequestBody is still needed to work properly (using 2.1.4). When the mapping ist annotated, it produces the following JSON: annotated

Without annotation, I get: not-annotated

The former is correct, but the “upload”-schema is useless, or am I wrong? Provided example is attached.

swagger-test-upload2.zip

0reactions
eak24commented, Dec 18, 2020

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:

public class MyClass {
  
    @PathParam
    private String privateField_;

    private String getPrivateField() {
        return privateField_;
    }
}

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found