Skip to content
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

Open
Ezhvsalate opened this issue Jul 15, 2021 · 3 comments
Open

Object of type <class '...'> is not JSON-serializable #57

Ezhvsalate opened this issue Jul 15, 2021 · 3 comments

Comments

@Ezhvsalate
Copy link

Hello!

Just tried to use your package to cache frequent GET requests in my FastApi app.

Endpoint looks like this:

@router.get("/{id}", response_model=Malt)
@cache_one_hour()
def get_malt_by_id(*, db: Session = Depends(get_db), id: UUID4, ):
    return actions.malt.get(db=db, id=id)   # actions returns SQLAlchemy 'Malt' model which is converted to Pydantic 'Malt' schema after that, also tries to return Malt.from_orm(actions...) - same result

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.

class Malt(BaseModel):
    name: str

    class Config:
        orm_mode = True

My example seems to be quite the same to your example with User model from documentation. So why doesn't it work?

@a-luna a-luna assigned a-luna and unassigned a-luna Jul 15, 2021
@a-luna
Copy link
Owner

a-luna commented Jul 15, 2021

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)

PlayerSchema is a pydantic model class for a SQLAlchemy model class named Player, and get_player retrieves the requested Player object from the database. This endpoint works perfectly, until the @cache decorator is added and the FAILED_TO_CACHE_KEY errors start to appear.

The fix I found was to manually convert the SQLAlchemy type to a dict using the from_orm method of the pydantic model class as shown below:

def convert_player_to_dict(player: Player) -> Dict[str, Union[str, int]]:
    return PlayerSchema.from_orm(player).dict()

Player is a SQLAlchemy model class, PlayerSchema is a pydantic model class, and player is an instance of Player. I simply wrapped the get_player function with convert_player_to_dict as shown below and everything is being cached as expected:

@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!

@Ezhvsalate
Copy link
Author

@a-luna, thanks for your response.

I tried your type of fix too and it doesn't work for me: I get the following:

 FAILED_TO_CACHE_KEY: Object of type <class 'dict'> is not JSON-serializable: key=cache:prj.api
.routes.dictionaries.bjcp_styles.get_bjcp_style_by_id(id=49895337-df68-4d07-819b-f08ebabfefaa)

After some hacking I found the reason in JSON encoder and modified a fastapi_redis_cache.util.BetterJsonEncoder in such way:

from uuid import UUID
from enum import Enum
prom pydantic import BaseModel

class BetterJsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return {"val": obj.strftime(DATETIME_AWARE), "_spec_type": str(datetime)}
        elif isinstance(obj, date):
            return {"val": obj.strftime(DATE_ONLY), "_spec_type": str(date)}
        elif isinstance(obj, Decimal):
            return {"val": str(obj), "_spec_type": str(Decimal)}
        elif isinstance(obj, BaseModel):
            return obj.dict()
        elif isinstance(obj, UUID):
            return str(obj)
        elif isinstance(obj, Enum):
            return str(obj.value)
        else:  # pragma: no cover
            return super().default(obj)

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).
Anyway I couldn't get rid of PydanticModel.from_orm() cause we don't know what pydantic class should be parsed from ORM model.

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.
Hope that will help :)

@b-simjoo
Copy link

b-simjoo commented Aug 6, 2023

Hi everybody, I have the same issue and have to edit BetterJsonEncoder and make it even more better!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants