apiflask / apiflask

A lightweight Python web API framework.

Home Page:https://apiflask.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Behaviur when use files type parameters

raulblanxart opened this issue · comments

Hello,
I'm using your example https://github.com/apiflask/apiflask/blob/main/examples/file_upload/app.py

When I use swagger and send the file field empty, i recive a 422 error. The problem is in "swagger engine" because the request not arrive to the code. But in swagger I can select the empty value checkpoint.

I want to have this parameter like a not required.

Environment:

  • Python version: 3.8.10
  • Flask version: 3.0.2
  • APIFlask version: 2.1.1

The reason is if we don't select a file, swagger will send an empty value like this:

POST http://localhost:5000/images HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------70636434840698183742419445910

-----------------------------70636434840698183742419445910
Content-Disposition: form-data; name="image"


-----------------------------70636434840698183742419445910--

Then apiflask get an empty string in the parameter image, and send 422 response to the client because a ValidationError("Not a valid file.") has occurred.

I‘m not sure if we should handle this case or it's a bug of swagger. @greyli

After tests, it seems to be the default behavior of swagger(Not a bug). We should handle it ourselves.

Because the File field has been migrated to flask-marshmallow, I can't resolve it directly.

I’ll fix it and add tests after this pr is merged and released.

@uncle-lv Thansk for the fast reaction and fix the bug.

Now it's working fine :)

@uncle-lv Well done! Thank you for the fix.