Swagger java feign client generation
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 may I know if you've time to submit a PR for the suggested fix so that we can review the change?
There is now an official form encoder. I think we should use it.
The form-encoder is working so I'll PR it soon.
@andreaformica fixed via #4124. Please pull the latest to give it a try.
Thanks for the PR from @cbornet
Thanks guys, and sorry for the late answer, I was offline for a few days. I will test again with the latest version.