Skip to content

Commit 7ae51e3

Browse files
committed
redis: FastAPI can't JSON-serialize bytes, while redis return them. Add translation class
Signed-off-by: Denys Fedoryshchenko <denys.f@collabora.com>
1 parent 11cb98e commit 7ae51e3

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

api/pubsub_mongo.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,21 @@ async def _update_subscriber_state(self, subscriber_id: str,
295295
{'$set': update}
296296
)
297297

298+
@staticmethod
299+
def _decode_redis_message(msg: Dict) -> Dict:
300+
"""Decode Redis message bytes to strings for JSON serialization"""
301+
return {
302+
'type': msg.get('type'),
303+
'pattern': (msg.get('pattern').decode('utf-8')
304+
if msg.get('pattern') else None),
305+
'channel': (msg['channel'].decode('utf-8')
306+
if isinstance(msg['channel'], bytes)
307+
else msg['channel']),
308+
'data': (msg['data'].decode('utf-8')
309+
if isinstance(msg['data'], bytes)
310+
else msg['data']),
311+
}
312+
298313
def _eventhistory_to_cloudevent(self, event: Dict) -> str:
299314
"""Convert eventhistory document to CloudEvent JSON string
300315
@@ -547,10 +562,10 @@ async def listen(self, sub_id: int,
547562

548563
# Filter by owner if not promiscuous
549564
if sub.promiscuous:
550-
return msg
565+
return self._decode_redis_message(msg)
551566
if 'owner' in msg_data and msg_data['owner'] != sub.user:
552567
continue
553-
return msg
568+
return self._decode_redis_message(msg)
554569

555570
async def publish(self, channel: str, message: str):
556571
"""Publish a message on a channel (Redis only, no durability)"""

0 commit comments

Comments
 (0)