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

Swagger java feign client generation

Open andreaformica opened this issue 10 years ago • 5 comments

Description

I have a problem with a java client generated using swagger-codegen. The problem occurs when using a function to upload a file into the server. Server side I have been using Jersey. For file upload the service is declared like this:

	@POST
	@Consumes({ MediaType.MULTIPART_FORM_DATA })
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/upload")
	@ApiOperation(value = "Create a measurement entry.", notes = "Input data are the measurement file.", response = NswMeasurementDataDto.class)
	public Response create(@Context UriInfo info, 
			@ApiParam(value = "The file", required = true) @FormDataParam("file") InputStream uploadedInputStream,
			@ApiParam(hidden=true) @FormDataParam("file") FormDataContentDisposition fileDetail)
			throws CdbWebException {

The python client works, and also the java client for files with extensions: .xml, .png, .txt but not for other extensions.

Swagger-codegen version

2.2.1

Swagger declaration file content or url
Command line used for generation

swagger-codegen generate -c ./config.json -i http://localhost:8080/nsw/v1/rest/swagger.json -l java -o ./swagger

The config.json is

{
  "groupId":"atlas.nsw.client",
  "artifactId":"nsw-client",
  "artifactVersion":"1.0.0",
  "library":"feign",
  "apiPackage":"atlas.nsw.client.swagger.api",
  "modelPackage":"atlas.nsw.client.swagger.model"
}    
Steps to reproduce
Related issues
Suggest a Fix

The solution I found is to change a couple of lines in FormAwareEncoder.java:

    String type = (URLConnection.guessContentTypeFromName(fileName) == null) ? "content/unknown" : URLConnection.guessContentTypeFromName(fileName);
    String contentType = "Content-Type: " + type;

andreaformica avatar Nov 01 '16 17:11 andreaformica

@andreaformica may I know if you've time to submit a PR for the suggested fix so that we can review the change?

wing328 avatar Nov 02 '16 02:11 wing328

There is now an official form encoder. I think we should use it.

cbornet avatar Nov 02 '16 06:11 cbornet

The form-encoder is working so I'll PR it soon.

cbornet avatar Nov 03 '16 18:11 cbornet

@andreaformica fixed via #4124. Please pull the latest to give it a try.

Thanks for the PR from @cbornet

wing328 avatar Nov 04 '16 13:11 wing328

Thanks guys, and sorry for the late answer, I was offline for a few days. I will test again with the latest version.

andreaformica avatar Nov 07 '16 12:11 andreaformica