yuval9313/FastApi-RESTful

[QUESTION]How to operate Swagger?

MgArcher opened this issue · 7 comments

image
How do I write comments in Swegger?

I think we can do as follows:
Have the docstring of function to become it's description with :summary as summary

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

Opera Snapshot_2022-01-11_222527_localhost

Amazing, thanks @DanielEidlin

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")

8f96728297077b771925fe0146cf861

@DanielEidlin can you review this please?