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

Problem in swagger.json for FormDataParam and FormDataBodyPart

Open andreaformica opened this issue 9 years ago • 4 comments

Dear experts, I have a problem with the generation of a swagger.json reflecting a REST service with the following signature:

	@Consumes(MediaType.MULTIPART_FORM_DATA)
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/upload")
	@ApiOperation(value = "Create a Payload in the database.", notes = "This method allows to insert a Payload."
			+ "Arguments: PayloadDto should be provided in the body as a JSON file.", response = PayloadDto.class)
	public Response createPayloadMultiForm(@Context UriInfo info,
			@ApiParam(value = "The file", required = true) @FormDataParam("file") InputStream uploadedInputStream,
			@ApiParam(hidden=true) @FormDataParam("file") FormDataContentDisposition fileDetail,
			@ApiParam(value = "The payload", required = true) @FormDataParam("payload") FormDataBodyPart jsonpayload)
			throws CdbWebException {
		this.log.info("PayloadRestController processing request to upload payload ");
		try {
			jsonpayload.setMediaType(MediaType.APPLICATION_JSON_TYPE);
			PayloadDto payload = jsonpayload.getValueAs(PayloadDto.class);

As you can see I am trying to upload a file plus provide a parameter in the body. My parameter is a sort of complex object, and I would like to pass it as a JSON string. The file part is correctly generated in swagger.json, while the "payload" parameter does not appear. If instead of this FormDataBodyPart I put a String object type then it appears in the swagger.json.

I am using compile group: 'io.swagger', name: 'swagger-jersey2-jaxrs', version:'1.5.9' and jersey 2.16

Thanks in advance for your suggestions. Andrea

andreaformica avatar Dec 06 '16 09:12 andreaformica

@andreaformica - the current version of the spec, 2.0, does not allow you to pass complex object as form data parameters. The best you can currently do is describe those as string. The next version of the spec will allow for more flexibility.

webron avatar Dec 06 '16 17:12 webron

Ok, thanks a lot, what I did as a "patch" was to put the following annotations on top of the method:

	    @ApiImplicitParam(name = "payload", value = "Json body for payloaddto", required = true, dataType = "string", paramType = "formData"),
	})

and this seems to do the job... Is this an acceptable solution ? Andrea

andreaformica avatar Dec 06 '16 21:12 andreaformica

Yeah, pretty much.

webron avatar Dec 06 '16 22:12 webron

Ok, thanks a lot, what I did as a "patch" was to put the following annotations on top of the method:

	    @ApiImplicitParam(name = "payload", value = "Json body for payloaddto", required = true, dataType = "string", paramType = "formData"),
	})

and this seems to do the job... Is this an acceptable solution ? Andrea

I tried the same, but not working for me... Can you please add the full code snippet ..

rajucet11 avatar Jan 30 '19 10:01 rajucet11