Swagger-codegen: Python client does not set Content-Type correctly for file uploads

Created on 26 Apr 2016  路  2Comments  路  Source: swagger-api/swagger-codegen

From the following swagger snippet:

paths:
  /dataset/:
    post:
      summary: Upload a new dataset
      parameters:
        - name: file
          in: formData
          description: The data set to upload, as a multipart/form-data file
          required: true
          type: file

Results in this code:

class DataApi(object):
    # ...snip...
    def dataset_post(self, **kwargs):
        # ...snip...
        # HTTP header `Content-Type`
        header_params['Content-Type'] = self.api_client.\
            select_header_content_type([])
        # ...snip...

ApiClient.select_header_content_type() seems a bit mysterious since it takes a list of content types and, if the list is empty, returns application/json, and if not, does some normalization, and returns application/json or the first content type in the list. So the above code will just always return application/json no matter what. Not really sure what the point of this is.

We cannot set a default content-type header in the ApiClient and pass it to the DataApi object, because that line of code will just override it.

This is somewhat troublesome, since I want to upload a file, and that requires the content type multipart/form-data, and my server application complains if this is not the case. The usual solution of "encode to base64, throw it into a JSON field and then compress it in stream" is sort of non-ideal since the files in this case could be 100's of MB to several GB, so that could put quite a bit of extra effort on the client side.

More notably, this violates what the swagger spec says the code's behaviour should be, since we specify in: formData and it will never actually do that.

(I feel like I'm giving you guys a lot of work lately; I'm going to see if I can devote some work-time to try to contribute some fixes, but no promises. :disappointed: )

Python Bug

Most helpful comment

@icefoxen it is not a bug, i think you are missing consumes: - multipart/form-data, see this for reference: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml#l253

All 2 comments

@icefoxen it is not a bug, i think you are missing consumes: - multipart/form-data, see this for reference: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml#l253

Thanks, adding the consumes: - multipart/form-data lines fixes the problem. Though it still seems like having in: formData with any other value for consumes:, including none, should be an error, since there's no way for that to work without it being multipart/form-data specifically...

Was this page helpful?
0 / 5 - 0 ratings