marshmallow-code/flask-smorest

Swagger UI not allowing upload of text files.

k1rm4d4 opened this issue · 4 comments

Hello!

I was trying to upload two .txt files to do some text processing. I uploaded through the input fields in Swagger UI generated by API. But, the Swagger-UI gives me the following error:

Details

image

Here's the relevant parts of the code:

class HeaderSchema(ma.Schema):
    id = ma.fields.String()

class MultiFileSchema(ma.Schema):
    prev_file = Upload(format='binary')
    post_file = Upload(format='binary')
    

class FormSchema(ma.Schema):
    type = ma.fields.String()

@blp.route("/result-file")
class ResultFile(MethodView):
    @blp.arguments(HeaderSchema, location='headers')
    @blp.arguments(MultiFileSchema, location='files')
    @blp.arguments(FormSchema, location='form')
    @blp.response(200)
    def post(self, header_args = None, files_data = None, form_data = None ):
        .
        .
        .

The code works fine when tested using Postman. Problem only shows up in Swagger-UI

Also, when I didn't attach the files in the Swagger-ui, the code worked fine and returned intended response, so likely the issue might be with the Upload() field.

What could I be doing wrong?

Thanks!

Maybe the Upload field doesn't yield the proper documentation.

I'm open to suggestion about how to improve/fix this if it is the case.

It might be interesting to also try with different OpenAPI versions, and perhaps with RapiDoc.

Hello, so I tried setting the OPENAPI_VERSION = "2.0", and it worked!
Now, I'm able to upload the files successfully. But, I have no idea why using OpenAPI 2x made it work.

I will try to peek into the source files later to check if I can find the cause.

OpenAPI 2 is quite different and although I never heard of a deprecation date, I'd be reluctant to push for its use.

However, there are slight differences in v3.0.x and v3.1.x and it could be worth trying both, if only to help debugging this. I don't think apispec is perfectly 3.1 compliant.

The test with RapiDoc could help ruling out a Swagger UI issue or at least give a hint.

Okay, I'll investigate this, keeping in mind the points you have provided.

I'll check:

  • API with different versions of 3x OAS, and check out apispec also.
  • Cross check with RapiDoc