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

fastapi-cache 0.1.4: TypeError: Unicode-objects must be encoded before hashing #19

Closed
diegocgaona opened this issue May 6, 2021 · 3 comments

Comments

@diegocgaona
Copy link

Hi,

I updated fastapi-cache using Github to solve the problem of "not JSON serializable", but now I have a new error:

  File "/lib/python3.9/site-packages/fastapi_cache/decorator.py", line 40, in inner
    cache_key = key_builder(
  File "/lib/python3.9/site-packages/fastapi_cache/key_builder.py", line 21, in default_key_builder
    + hashlib.md5(  # nosec:B303
TypeError: Unicode-objects must be encoded before hashing

Before (with version 1.3.4) I was using something like this bellow, but with the two lines commented and returning the json_updated_user:

@router.get("/me/items/", response_model=List[schemas.User_Pydantic])
@cache(namespace="test", expire=60, coder=JsonCoder)
async def read_current_user_items(user: UserDB = Depends(fastapi_users.get_current_active_user)):
    user_cards = await schemas.Card_Pydantic.from_queryset(
        Card.filter(owner_id=user.id).all())
    for city in user_cards:
        await weather_api.get_api_info_by_id(city.id)
    # user_with_items = await schemas.User_Pydantic.from_queryset(UserModel.filter(id=user.id).all())
    # json_updated_user = jsonable_encoder(user_with_items)
    return await schemas.User_Pydantic.from_queryset(UserModel.filter(id=user.id).all())

Without jsonable_encoder I get the error "not JSON serializable", but now I have the hash error.
If I comment the @cache decorator, it works fine (and I don't need the jsonable_encoder).

Thanks in advance!

@long2ice
Copy link
Owner

long2ice commented May 6, 2021

You need write you custom key_builder

@diegocgaona
Copy link
Author

Ok, after some tries I had modified the default key_builder, putting an .encode('utf-8'). It works, I don't know if it is the best way.

def my_key_builder(
        func,
        namespace: Optional[str] = "",
        request: Optional[Request] = None,
        response: Optional[Response] = None,
        args: Optional[tuple] = None,
        kwargs: Optional[dict] = None,
):
    from fastapi_cache import FastAPICache

    prefix = f"{FastAPICache.get_prefix()}:{namespace}:"
    cache_key = (
            prefix
            + hashlib.md5(f"{func.__module__}:{func.__name__}:{args}:{kwargs}".encode('utf-8'))
            .hexdigest()
    )
    return cache_key

@long2ice , this is correct? This could be the default to handle cases like mine or would cause problems with others?

Thanks!!

@long2ice
Copy link
Owner

Fixed

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

2 participants