Object of type set is not JSON serializable
- Dominant language
- Python
- Stars
- 11.1k
- Forks
- 1k
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 2
Description
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
```python
@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
```jsonc
{
// ...
"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:
```python
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
Contributor guide
Research direction
Read chalice/app.py around handle_extra_types and tests/unit/test_app.py around test_http_request_to_dict_is_json_serializable. Add a regression test covering response serialization when an item contains a set, then run the relevant unit tests and verify the response can be serialized without the reported TypeError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, python
- Domain
- api, backend, databases
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100