From eeb26df074c6ffad6e6fa3fb42ca7c52a88c91af Mon Sep 17 00:00:00 2001 From: Denys Fedoryshchenko Date: Wed, 21 Jan 2026 14:38:15 +0200 Subject: [PATCH] redis: FastAPI can't JSON-serialize bytes, while redis return them. Add translation class Signed-off-by: Denys Fedoryshchenko --- api/pubsub_mongo.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/api/pubsub_mongo.py b/api/pubsub_mongo.py index 811022e4..513385f0 100644 --- a/api/pubsub_mongo.py +++ b/api/pubsub_mongo.py @@ -295,6 +295,21 @@ async def _update_subscriber_state(self, subscriber_id: str, {'$set': update} ) + @staticmethod + def _decode_redis_message(msg: Dict) -> Dict: + """Decode Redis message bytes to strings for JSON serialization""" + return { + 'type': msg.get('type'), + 'pattern': (msg.get('pattern').decode('utf-8') + if msg.get('pattern') else None), + 'channel': (msg['channel'].decode('utf-8') + if isinstance(msg['channel'], bytes) + else msg['channel']), + 'data': (msg['data'].decode('utf-8') + if isinstance(msg['data'], bytes) + else msg['data']), + } + def _eventhistory_to_cloudevent(self, event: Dict) -> str: """Convert eventhistory document to CloudEvent JSON string @@ -547,10 +562,10 @@ async def listen(self, sub_id: int, # Filter by owner if not promiscuous if sub.promiscuous: - return msg + return self._decode_redis_message(msg) if 'owner' in msg_data and msg_data['owner'] != sub.user: continue - return msg + return self._decode_redis_message(msg) async def publish(self, channel: str, message: str): """Publish a message on a channel (Redis only, no durability)"""