Visecy / Karuha

A simple Tinode chatbot framework.
Apache License 2.0
26 stars 2 forks source link

Cache data `from_meta` error, data type doesn't match #17

Closed THUzxj closed 2 months ago

THUzxj commented 2 months ago

Hi, I faced with one error in caching data functions in the latest version

Traceback (most recent call last):
  File "/redacted/Karuha/karuha/event/base.py", line 17, in handler_runner
    ret = super(Event, event).call_handler(func)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/redacted/Karuha/karuha/utils/invoker.py", line 123, in call_handler
    return handler(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/redacted/Karuha/karuha/data/cache.py", line 356, in handle_meta
    cache_me_subscription(user, sub_meta)
  File "/redacted/Karuha/karuha/data/cache.py", line 316, in cache_me_subscription
    sub = Subscription.from_meta(sub_meta)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/redacted/Karuha/karuha/data/meta.py", line 226, in from_meta
    return cls(
           ^^^^
  File "/redacted/.local/lib/python3.11/site-packages/pydantic/main.py", line 193, in __init__
    self.__pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Subscription
  Input should be a valid dictionary [type=dict_type, input_value=b'{"comment":""}', input_type=bytes]

In karuha/data/meta.py line 228, we can solve it by changing

private=meta.private or None

to

private= json.loads(meta.private) if meta.private else None,

The other dict fields should also be changed.

Ovizro commented 2 months ago

This is indeed a bug. I declared the type of the public and trusted fields as Json, but forgot to change the type of the private field. I will submit a commit to fix this.

Ovizro commented 2 months ago

Now you can try the code on the dev branch to see if that solves the problem.

THUzxj commented 2 months ago

It works well now. Thanks for your immediate reply!