Enum can't be serialized anymore after upgrade to 7.6.2
sushi2all opened this issue · 2 comments
Hi there,
I've bumped into an error that never happened before upgrading to version 7.6.2.
I'm getting an error saying that an enum class instance is not json serializable. Here's the full error line:
File \"/usr/local/lib/python3.11/json/encoder.py\", line 180, in default\n raise TypeError(f'Object of type {o.__class__.__name__} '\nTypeError: Object of type QueueType is not JSON serializable\n"
QueueType is an Enum class as follows:
from enum import Enum
class QueueType(Enum):
OPERATOR = 'o'
EQUIPMENT = 'e'
SITE = 's'and the code generating the error is the following:
# FastAPI endpoint
@router.put('/queue')
async def update_queue(queue_update: Queue):
try:
match = dict(type=queue_update.type, site_key=queue_update.site_key) # <-- the type parameter is of type QueueType
db.collection('Queue').update_match(match, queue_update, keep_none=False, sync=True) # <-- line raising the errorAny insight on what's happening? A regression?
Hello @sushi2all,
Unfortunately, the update_match method (among others, see 7.6.1) had to be refactored, due to their usage of _api/simple HTTP API, which has been long deprecated since ArangoDB 3.4.
The simple queries provided by the /_api/simple endpoint were limited in functionality and internally resorted to AQL queries anyway. Therefore, we kept the driver methods that used to rely on them, but adapted them to use AQL instead. Apart from being safer and up-to-date, users might notice even a performance improvement from this.
Because the filters (e.g. your match dict) have to be part of an AQL query, they are Json serialized now. Normally this shouldn't pose any problems, but there are corner cases, such as with Enum. You'll have to make your Enum Json serializable. There are several ways to go about this, but I would suggest inheriting from str:
class QueueType(str, Enum):
OPERATOR = 'o'
EQUIPMENT = 'e'
SITE = 's'Similarly, if your Enum is int-based, you can inherit from IntEnum.
Let me know if this helps!
Your suggestion worked, thank you! 🙏🏻