New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Returning dataclass
with pydantic-compatible fields (e.g. datetime) fails for FastAPI >= 0.67
#3608
Comments
dataclass
responses with pydantic-compatible fields (e.g. datetime) failsdataclass
with pydantic-compatible fields (e.g. datetime) fails for FastAPI >= 0.67
It also fails, when specifying the response model in the route: from datetime import datetime
from fastapi import FastAPI
from fastapi.testclient import TestClient
from dataclasses import dataclass
app = FastAPI()
@dataclass
class Item:
name: str
date: datetime
@app.get("/item", response_model=Item)
def get_object():
return Item(name="object", date=datetime(2021, 7, 26))
client = TestClient(app)
def test_object():
response = client.get("/item")
response.raise_for_status()
assert response.json() == {
"name": "object",
"date": datetime(2021, 7, 26).isoformat(),
} |
Hello, I've added 1 more test case in another issue for proving the change introduced in 0.67 version broke the contract of jsonable_encoder. |
This is a pretty important bug for me, but the fix seems to not have any traction. |
This is also important for me, and the PR already exists, let me know if you need any help on this. |
This is very important to me aswell |
First check
Example & Description
I have encountered an issue when returning
dataclass
responses with some pydantic-compatible (but incompatible with json encoder of Python standard library) fields, e.g.datetime
objects orpathlib
file paths.Here's a self-contained, minimal, reproducible, example where I have added a
datetime
field to theItem
class in tests/test_serialize_response_dataclass.py:I would
I have created a PR #3607 with an attempt to fix the issue and extend the existing tests in tests/test_serialize_response_dataclass.py.
Without this PR, all tests in this updated file fail.
Note that the example above and the extended tests also pass with a pre 0.67 version of FastApi (before official dataclass response support).
Environment
The text was updated successfully, but these errors were encountered: