-
Notifications
You must be signed in to change notification settings - Fork 24
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
Object of type <class '...'> is not JSON-serializable #57
Comments
I recently ran into this issue myself! From your code snippet it appears that you may have tried the same fix and indicated that it didn't solve the issue. Here is an endpoint where I was encountering the same error: @router.get("/details", response_model=PlayerSchema)
@cache()
def get_player_details(mlb_id: int, app = Depends(get_app)):
return get_player(mlb_id, app)
The fix I found was to manually convert the SQLAlchemy type to a def convert_player_to_dict(player: Player) -> Dict[str, Union[str, int]]:
return PlayerSchema.from_orm(player).dict()
@router.get("/details", response_model=PlayerSchema)
@cache()
def get_player_details(mlb_id: int, app = Depends(get_app)):
return convert_player_to_dict(get_player(mlb_id, app)) Let me know if this works! |
@a-luna, thanks for your response. I tried your type of fix too and it doesn't work for me: I get the following:
After some hacking I found the reason in JSON encoder and modified a
So it can handle Enums and UUIDs, and we don't more need to use .dict() (it's included in encoder, but will cause Pydantic as dependency so make this decision yourself plz). And one more thing: I saw your mapping with additional '_spec_type' fields but have no idea why it will be required in this case. |
Hi everybody, I have the same issue and have to edit |
Hello!
Just tried to use your package to cache frequent GET requests in my FastApi app.
Endpoint looks like this:
When I try to make requests I can see following in the logs:
FAILED_TO_CACHE_KEY: Object of type <class '....db.models.dictionaries.malt.Malt'> is not JSON-serializable: key=cache:mypkg.api.routes.dictionaries.malts.get_malt_by_id(id=7af9090c-dcb5-4778-b090-5b6890618566)
Pydantic schema looks like following (basically it is much more complex but I removed everything but name and still get same result.
My example seems to be quite the same to your example with User model from documentation. So why doesn't it work?
The text was updated successfully, but these errors were encountered: