Pydantic 2.0 seems to have drastically changed.
Previously with FastAPI and Pydantic 1.X I could define the schema like this, where receipt is optional:
class VerifyReceiptIn(BaseModel):
device_id: str
device_type: DeviceType
receipt: Optional[str]
Then in FastAPI endpoint I could do this:
@router_verify_receipt.post(
"/",
status_code=201,
response_model=VerifyReceiptOut,
responses={201: {"model": VerifyReceiptOut}, 400: {"model": HTTPError}},
)
async def verify_receipt(body: VerifyReceiptIn):
auth_service = AuthService()
...
And the unit test without receipt in body was fine with it, but now with Pydantic 2.0 it's failing. Now it claims that Receipt is required and throws a 422 error.
response = await client.post(
"/verify-receipt/",
headers={"api-token": "abc123"},
json={
"device_id": "u1",
"device_type": DeviceType.ANDROID.value,
},
)
But this is why we had Optional. Why do I have to pass receipt=None
in body? This is not ideal as it will break everything on production. Is there a way around this? Thanks