swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

jersey-jaxrs file upload broken with swagger 1.3.0

Open gaffa opened this issue 13 years ago • 7 comments

With latest swagger release and current trunk-version of swagger-ui I have the following behaviour:

Java-Code:

@Path("/{translationId}/complaint")
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(notes = "Files a complaint on a translationData", value = "fileComplaint")
    @ApiResponses({ @ApiResponse(code = 404, message = "translationData with given id is not known") })
    public Response fileComplaint(
            @ApiParam(value = "id of translationData request", required = true) @PathParam("translationId") Integer translationId,
            @ApiParam(required = true, value = "complaint request") @Valid @FormDataParam("complaint") ComplaintWrapper complaintWrapper,
            @ApiParam(required = false, value = "attachment") @FormDataParam("attachment") InputStream attachment,
            @FormDataParam("attachment") FormDataContentDisposition attachmentData) {

Resulting JSON:

{
        "path": "/translations/{translationId}/complaint",
        "operations": [
            {
                "method": "POST",
                "summary": "fileComplaint",
                "notes": "Files a complaint on a translationData",
                "type": "void",
                "nickname": "fileComplaint",
                "produces": ["application/json"],
                "consumes": ["multipart/form-data"],
                "parameters": [
                    {
                        "name": "translationId",
                        "description": "id of translationData request",
                        "required": true,
                        "allowMultiple": false,
                        "type": "integer",
                        "format": "int32",
                        "paramType": "path"
                    },
                    {
                        "name": "body",
                        "description": "complaint request",
                        "required": true,
                        "allowMultiple": false,
                        "type": "ComplaintWrapper",
                        "paramType": "body"
                    },
                    {
                        "name": "body",
                        "description": "attachment",
                        "required": false,
                        "allowMultiple": false,
                        "type": "java.io.InputStream",
                        "paramType": "body"
                    },
                    {
                        "name": "body",
                        "required": false,
                        "allowMultiple": false,
                        "type": "FormDataContentDisposition",
                        "paramType": "body"
                    }
                ],
                "responseMessages": [
                    {
                        "code": 404,
                        "message": "translationData with given id is not known"
                    }
                ]
            }
        ]
    }

Resulting UI:

image

Is this a swagger-core issue? Looks like it to me. Otherwise I will post it to swagger-ui...

gaffa avatar Aug 19 '13 08:08 gaffa

Hi, two things. First, you need to use the Jersey reader to scan your resources (the above tells me you're using the DefaultJaxRSReader. Next, you are correct, even when scanned correctly, swagger-ui file upload is currently broken. Expect that to be addessed this week.

fehguy avatar Aug 19 '13 15:08 fehguy

Hi! Thank you. For some reasons we had to migrate to spring anyway. So I cannot try this any more :( Regards!

gaffa avatar Aug 29 '13 07:08 gaffa

You can always look at https://github.com/martypitt/swagger-springmvc if you're moving to spring.

fehguy avatar Aug 29 '13 15:08 fehguy

Hi, Is this issue addressed ? I am facing the same issue and didn't find a solution yet [using 1.3.6 version]

varunmehra03 avatar Jul 23 '14 06:07 varunmehra03

@varunmehra03 Swagger-core does not affect file uploads. There are two possible issues - either the swagger output doesn't describe a parameter as a file or swagger-ui is giving you some issues.

In order to analyze it, I'd suggest giving full details of your configuration (including versions of core and ui), and possibly the json output in a thread in the google group. If we find out there's a problem, then you should open an issue on it.

webron avatar Jul 23 '14 07:07 webron

This is a snippet from the Sample resource class with swagger annotations.

@Component @Scope("request") @Produces({ MediaType.APPLICATION_JSON, HalMediaType.APPLICATION_HAL_JSON }) @Path("/rules") @Api(value = "/rules", description = "Rules resource") public class TestResource {

@POST
@Consumes("multipart/form-data")
@Path("/file")
@ApiOperation(value = "Upload file ", notes = "file can be uploaded using this api.", response = RuleSet.class)
public RuleSet uploadFile(@FormDataParam("name") String name, 
                              @FormDataParam("comment") String description, 
                              @FormDataParam("file") InputStream stream,
                              @FormDataParam("file") FormDataContentDisposition disposition) {
......
}

}

This is the JSON generated.

{ "apiVersion": "1.0.0", "swaggerVersion": "1.2", "basePath": "/sample-app/api", "resourcePath": "/rules", "produces": [ "application/json", "application/hal+json" ], "apis": [ { "path": "/rules/file", "operations": [ { "method": "POST", "summary": "Upload a file ", "notes": "file can be uploaded using this api.", "type": "RuleSet", "nickname": "uploadFile", "consumes": [ "multipart/form-data" ], "authorizations": { }, "parameters": [ { "name": "body", "required": false, "type": "string", "paramType": "body", "allowMultiple": false }, { "name": "body", "required": false, "type": "string", "paramType": "body", "allowMultiple": false }, { "name": "body", "required": false, "type": "java.io.InputStream", "paramType": "body", "allowMultiple": false }, { "name": "body", "required": false, "type": "FormDataContentDisposition", "paramType": "body", "allowMultiple": false } ] } ] } }

Here, for the "type": "java.io.InputStream", the param type is "body". Instead, it should be a file browse. What exactly am i missing? I have searched through the groups and it seems people have reported this issue - https://groups.google.com/forum/#!msg/swagger-swaggersocket/wIp5CdNyWqs/c0BosfyG78gJ Not sure whether it has been fixed.

I am using maven dependency for swagger-jersey-jaxrs_2.10 - version 1.3.6. And swagger-ui

varunmehra03 avatar Jul 23 '14 09:07 varunmehra03

We have a scala jersey sample with file upload here - https://github.com/wordnik/swagger-core/tree/master/samples/scala-jaxrs-fileupload - and it works fine.

I suspect something is wrong with your configuration. I need full details of the jersey version that you use and your pom.xml. Please don't paste it here as it ends up being all over the place and impossible to read (as you can see from the previous pastes). Again, I'd starting a thread on the google group as this is not really this issue.

webron avatar Jul 23 '14 13:07 webron