Fast API에서는 response_model이라는 인자로 손쉽게 데이터를 정형화하여 내보낼 수 있다. response model은 pydantic으로 정의된 모델이나 이를 담는 데이터 형식이 될 수 있다.

from typing import List, Optional

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
    tags: List[str] = []

@app.post("/items/", response_model=Item)
async def create_item(item: Item):
    return item

path operation 부분인 app.post의 인자로 response_model=Item이라고 명시하여 create_item의 return 값이 Item에 정의된 형식에 맞춰서 내보내지게 된다.

만약 Item이 담긴 리스트를 내보내고 싶다면, List[Item]처럼 정의하면 된다.

또한, create_item 함수가 Item 모델에서 선언한 필드나 타입 이외에 더 많은 데이터를 가지고 있더라도, 모델에 선언된 데이터만 내보내지게 된다.

즉, response_models

의 이점을 가진다.

아래는 유저 모델을 가지고 작성한 예제이다.

from typing import Optional

from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()

class UserIn(BaseModel):
    username: str
    password: str
    email: EmailStr
    full_name: Optional[str] = None

class UserOut(BaseModel):
    username: str
    email: EmailStr
    full_name: Optional[str] = None

@app.post("/user/", response_model=UserOut)
async def create_user(user: UserIn):
    return user

UserIn 모델은 response body로 들어오며 새로운 유저를 만들기 위한 폼이다. 반면에 UserOut 모델은 생성된 유저에 대한 정보를 다시 클라이언트에게 보내게 되는 모델이다.

자세히 보면, password는 내보내지 않는 것을 볼 수 있다.

위와같이 정의가 되면 /docs에는 아래와 같이 자동으로 documenation이 되게 된다.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4b6524c9-0213-4859-b839-93db0401b19a/Untitled.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/441e5f2f-86ad-4541-8bca-3ab10196f840/Untitled.png

Response Model Exclude Unset

만약 response_model로 선택된 모델에 default 값이 선언되어 있는 필드가 있을 때를 생각해보자.

default 값을 api 함수에서 직접 바꾸지 않았을 때, 즉 default 값이 내보내지게 될 때 이러한 바뀌지 않은 값들을 제외시키는 방법이 있다.

그것이 바로 response_model_exclude_unset이다.