[QUESTION]How to operate Swagger?
MgArcher opened this issue · 7 comments
MgArcher commented
yuval9313 commented
I think we can do as follows:
Have the docstring of function to become it's description with :summary
as summary
DanielEidlin commented
As @negadive suggested you can use the summary
and description
arguments to add information to your API's swagger.
Here is an example using the @cbv
decorator:
app = FastAPI()
router = InferringRouter()
@cbv(router)
class ItemsApi:
@router.post("/item", response_model=Item, summary="Create an item")
async def post(self, item: Item):
"""
Create an item with all the information:
- **name**: each item must have a name
- **description**: a long description
- **price**: required
- **tax**: if the item doesn't have tax, you can omit this
- **tags**: a set of unique tag strings for this item
"""
return item
yuval9313 commented
Amazing, thanks @DanielEidlin
MgArcher commented
I have an idea for a solution.Modify the cbv_base.py
class Api:
def __init__(self, app: FastAPI):
self.app = app
def add_resource(self, resource: Resource, *urls: str, **kwargs: Any) -> None:
router = APIRouter()
_cbv(router, type(resource), *urls, instance=resource)
self.app.include_router(router)
from fastapi_restful import Api as api
class Api(api):
"""add_resource"""
def add_resource(self, resource: Resource, *urls: str, **kwargs: Any) -> None:
router = APIRouter()
_cbv(router, type(resource), *urls, instance=resource)
# alter
router.routes[0].tags = kwargs.get("tages")
router.routes[0].summary = kwargs.get("summary")
self.app.include_router(router)
api.add_resource(idCard.IdCard(), '/idcard_recoginize', tages=["test"], summary="test2")
yuval9313 commented
@DanielEidlin can you review this please?