RedisJSON / redisjson-py

An extension to redis-py for using Redis' ReJSON module
https://redisjson.io
BSD 2-Clause "Simplified" License
160 stars 34 forks source link

Method setDecoder is not working #45

Closed bentsku closed 3 years ago

bentsku commented 3 years ago

Hello,

The method setDecoder is not working properly, if you use it after initialising your Client instance, it won't be used in the callbacks.

Passing the decoder as a parameter when initialising the Client instance works as expected.

I believe it is because we set the callbacks in the __init__ method, and when we set the new decoder, we reassign self._decode with the new decoder decode method, but the callbacks are still pointing to the old function.

Those callbacks are not recreated with the set_response_callback method.

https://github.com/RedisJSON/redisjson-py/blob/b57f8c675f90f13c5f65bb551f77ce4ab3097452/rejson/client.py#L52-L71

I guess several fixes are possible, having _decode being a declared method of the class, which would be calling the _decode_function property (I don't like the name, what should it be ?) that would be set in the setDecoder method. The callbacks would always be pointing to the right method.

A quick fix that would also be fixing the TypeError for every command would be in the form of this snippet.

class Client(StrictRedis):
    _encoder = None
    _encode = None
    _decoder = None
    _decode_function = None

   [...]

    def _decode(self, s, *args, **kwargs):
        try:
            return self._decode_function(s, *args, **kwargs)
        except TypeError:
            if s is not None:
               raise
            return None

This would fix the callback problem (tested). I can open a pull request if you'd like this fix.

Thank you