Skip to content

Enum can't be serialized anymore after upgrade to 7.6.2 #284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sushi2all opened this issue Sep 5, 2023 · 2 comments
Closed

Enum can't be serialized anymore after upgrade to 7.6.2 #284

sushi2all opened this issue Sep 5, 2023 · 2 comments
Assignees

Comments

@sushi2all
Copy link

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 error

Any insight on what's happening? A regression?

@apetenchea
Copy link
Member

apetenchea commented Sep 6, 2023

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!

@apetenchea apetenchea self-assigned this Sep 6, 2023
@sushi2all
Copy link
Author

Your suggestion worked, thank you! 🙏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants