aws / chalice

Python Serverless Microframework for AWS
Apache License 2.0
10.61k stars 1.01k forks source link

Object of type set is not JSON serializable #1879

Open leonelgalan opened 2 years ago

leonelgalan commented 2 years ago

I'm getting an item from a DynamoDB Table that has a set attribute. Simply returning the item at the end of the request through Chalice raises this error.

Chalice

@app.route("/items/{item_id}", methods=["GET"])
def get_item(item_id):
   # ...
   item = dynamodb_table.get_item(Key=key).get("Item", key)
   return item

Item in DynamoDB

{
  // ...
  "Set": {
    "SS": [
      "A",
      "B"
    ]
  }
}

Stack trace

[ERROR] TypeError: Object of type set is not JSON serializable
Traceback (most recent call last):
  File "/var/task/chalice/app.py", line 1239, in __call__
    return handler(event, context)
  File "/var/task/chalice/app.py", line 1672, in __call__
    return response.to_dict(self.api.binary_types)
  File "/var/task/chalice/app.py", line 473, in to_dict
    body = json.dumps(body, separators=(',', ':'),
  File "/var/lang/lib/python3.9/json/__init__.py", line 234, in dumps
    return cls(
  File "/var/lang/lib/python3.9/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/var/lang/lib/python3.9/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/var/task/chalice/app.py", line 64, in handle_extra_types
    raise TypeError('Object of type %s is not JSON serializable'

Solution

Add:

    if isinstance(obj, set):
        return list(obj)

To:

https://github.com/aws/chalice/blob/61f43459a078524b4d80cbb816bb4d5d67884a47/chalice/app.py#L55-L65

That should work, but before writing a PR I want to make sure my solution is sound and maybe have some guidance on how to write a test for it. Currently it seems the only test where handle_extra_types is used is test_http_request_to_dict_is_json_serializable, but there is no test for the response.

https://github.com/aws/chalice/blob/adcc8dbdb0ef86b7aa9238bf90e718c6cd3413f3/tests/unit/test_app.py#L1805

rafagan commented 2 years ago

Any update on that?

msealander commented 9 months ago

I'm running into this issue going through the workshop: https://chalice-workshop.readthedocs.io/

It works fine until the DynamoDB part.