🐛 Fix jsonable_encoder for dataclasses with pydantic-compatible fields#3607
Merged
tiangolo merged 6 commits intoAug 26, 2022
Merged
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. |
Contributor
|
📝 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
Member
|
Amazing, thanks @himbeles! And thanks for adding several tests! 🚀 🍰 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes an issue (#3608) I encountered when returning
dataclassesresponses with pydantic-compatible (but incompatible with json encoder of Python standard library) fields, e.g.datetimeobjects orpathlibfile 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
datetimefield to theItemclass 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).