BlackSheep icon indicating copy to clipboard operation
BlackSheep copied to clipboard

No form generated in swagger docs page for FromText, FromFiles

Open yinkaisheng opened this issue 8 months ago • 2 comments

# -*- coding: utf-8 -*-
import blacksheep as bs
from blacksheep.server.openapi.v3 import OpenAPIHandler
from openapidocs.v3 import Info
import uvicorn
from loguru import logger

app = bs.Application(show_error_details=True)
docs = OpenAPIHandler(info=Info(title="Example API", version="0.0.1"))
docs.bind_app(app)

@bs.post("/upload")
async def api_upload(req: bs.Request, text: bs.FromText, file: bs.FromFiles):
    logger.info(f'from {req.client_ip} url={req.url} text={text.value} file={file.value}')
    return 'ok'

if __name__ == '__main__':   
    uvicorn.run(app, host='0.0.0.0', port=8021)

I try to use blacksheep-2.1.0. When I run the above code and open /docs, I see Image Won't it generate a form for the params text and file?

Then I use Postman to send the request.

Image

Image The text.value and file.value contain boundary. Do I need to parse it manually? It seems that FromText is the whole request body, including the file part. I have already checked the documentation at https://www.neoteroi.dev/blacksheep/requests, but I didn't find the usage.

If I use fastapi, the usage is simple

@app.post("/upload")
async def api_upload(req: fastapi.Request,
                     text: str = fastapi.Form(..., example="hello"),
                     file: fastapi.UploadFile = fastapi.File(..., example="example.py")):
    logger.info(f'from {req.client.host} url={req.url} text={text} file={file.filename}')
    data = await file.read()
    return 'ok'

Then I can test it in docs page without Postman. Image

How should I use BlackSheep to create an upload example similar to the one in FastAPI?

yinkaisheng avatar Apr 24 '25 07:04 yinkaisheng

Hi @yinkaisheng Thanks for opening this issue and providing an example. Indeed, support for automatic generation of OpenAPI documentation for multipart form data is still lacking in BlackSheep. I started analyzing what it would take to support it better, and it will require some changes to offer a good UX. I am willing to work on this, but I need to complete a few other things first.

RobertoPrevato avatar Apr 26 '25 20:04 RobertoPrevato

OK, looking forward to it.

yinkaisheng avatar Apr 27 '25 01:04 yinkaisheng