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
🐛 Fix jsonable_encoder
for dataclasses with pydantic-compatible fields
#3607
🐛 Fix jsonable_encoder
for dataclasses with pydantic-compatible fields
#3607
Conversation
233efef
to
b79b58d
Compare
I am having this same issue, and was going to propose the same fix. I think test cases should be added to test_jsonable_encoder.py since the |
Codecov Report
@@ Coverage Diff @@
## master #3607 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 535 535
Lines 13828 13831 +3
=========================================
+ Hits 13828 13831 +3
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
📝 Docs preview for commit e722c7b at: https://6308d0a41d7c7029546073b2--fastapi.netlify.app |
jsonable_encoder
for dataclasses with pydantic-compatible fieldsjsonable_encoder
for dataclasses with pydantic-compatible fields
Amazing, thanks @himbeles! And thanks for adding several tests! 🚀 🍰 |
This PR fixes an issue (#3608) I encountered when returning
dataclasses
responses with pydantic-compatible (but incompatible with json encoder of Python standard library) fields, e.g.datetime
objects orpathlib
file paths.This was achieved by adding a call to
jsonable_encoder(dict)
after converting the dataclass to a dict injsonable_encoder
.For testing, I have added a
datetime
field to theItem
class in tests/test_serialize_response_dataclass.py.Without this PR, the last two tests in this file fail (
test_no_response_model_objectlist
,test_no_response_model_object
).Note that the tests also pass with a pre 0.67 version of FastApi (before official dataclass response support).