Swagger-codegen: Generated JAVA API does not support file upload

Created on 18 Jul 2016  路  5Comments  路  Source: swagger-api/swagger-codegen

Description

I generated a Java API which should allow me to upload files, but it does not. Looks like there is something wrong with choosing the right content type

Swagger-codegen version
Swagger declaration file content or url

...

basePath: /
produces:

  • application/json
    paths:
    /1/upload:
    post:
    summary: Upload document
    operationId: upload
    parameters:
    #...
    - name: file1
    in: formData
    description: File 1
    required: true
    type: file
    responses:
    default:
    description: UploadResponse
    schema:
    $ref: '#/definitions/UploadResponse'
    #...
    definitions:
    UploadResponse:
    type: object
    properties:
    taskId:
    type: string
    #...
Steps to reproduce
  1. Generate JAVA API
  2. use upload
Java Bug

Most helpful comment

Here are HTTP Post like the autogenerated C# and Java clients produce. The C# Version works fine, the Java Version not. In the Java client's POST, everything what is declared in "parameters" in the swagger.yaml has the extra "Content-Type: multipart/form-data" which causes the error. In the C# client's POST the uploaded files Content-Type is application/octet-stream.
Changing the ApiClient.java like I did here forces the Java Client to produce a working HTTP POST.

All 5 comments

I did not. Thank you, that partially solves the problem. Now I can upload files. Unfortunately, now the Content Type multipart/form-data is also set for the Parameters, which should not be the case. Is there an option to change that in the swagger.yaml? Here is what I changed in the Class ApiClient to make it work:
public Object serialize(Object obj, String contentType, Map<String, Object> formParams) throws ApiException { if (contentType.startsWith("multipart/form-data")) { //... mp.bodyPart(new FileDataBodyPart(param.getKey(),file,MediaType.APPLICATION_OCTET_STREAM_TYPE)) //originally, it is also MediaType.MULTIPART_FORM_DATA_TYPE here } else { mp.field(param.getKey(), parameterToString(param.getValue()), MediaType.MULTIPART_FORM_DATA_TYPE); } } return mp; //... }

What do you mean by content type multipart/form-data set for parameter?j

Please share your spec via http://gist.github.com.

Here are HTTP Post like the autogenerated C# and Java clients produce. The C# Version works fine, the Java Version not. In the Java client's POST, everything what is declared in "parameters" in the swagger.yaml has the extra "Content-Type: multipart/form-data" which causes the error. In the C# client's POST the uploaded files Content-Type is application/octet-stream.
Changing the ApiClient.java like I did here forces the Java Client to produce a working HTTP POST.

For Java API client, can you try the retrofit2 API client instead to see if it has the same issue?

Here is an FAQ on how to specify library: https://github.com/swagger-api/swagger-codegen/wiki/FAQ#how-can-i-generate-an-android-sdk

Was this page helpful?
0 / 5 - 0 ratings