fromtypingimportOptional,SetfromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()classItem(BaseModel):name:strdescription:Optional[str]=Noneprice:floattax:Optional[float]=Nonetags:Set[str]=set()@app.post("/items/",response_model=Item,summary="Create an item",description="Create an item with all the information, name, description, price, tax and a set of unique tags",)asyncdefcreate_item(item:Item):returnitem
5. Description from docstring
일반적으로 설명이라는 것은 길고 여러 줄을 포함하는 경향이 있으므로, docstring으로 path operation의 설명을 선언하는 방법도 좋다.
fromtypingimportOptional,SetfromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()classItem(BaseModel):name:strdescription:Optional[str]=Noneprice:floattax:Optional[float]=Nonetags:Set[str]=set()@app.post("/items/",response_model=Item,summary="Create an item")asyncdefcreate_item(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 """returnitem
위의 예제를 실행하면 다음과 같이 확인된다.
6. Response description
response_description 매개변수를 사용하여 response 설명을 지정할 수 있다.
fromtypingimportOptional,SetfromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()classItem(BaseModel):name:strdescription:Optional[str]=Noneprice:floattax:Optional[float]=Nonetags:Set[str]=set()@app.post("/items/",response_model=Item,summary="Create an item",response_description="The created item",)asyncdefcreate_item(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 """returnitem
위의 예제를 실행하면 다음과 같이 확인된다.
7. Deprecate a path operation
어떠한 path operation을 서버에서 더 이상 사용하지 않는 경우 deprecated 매개변수를 전달하면 된다.