Skip to content

fix(event_handler): raise more specific SerializationError exception for unsupported types in data validation #4415

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

Merged
2 changes: 1 addition & 1 deletion aws_lambda_powertools/event_handler/openapi/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def jsonable_encoder( # noqa: PLR0911
)
except ValueError as exc:
raise SerializationError(
f"Unable to serializer the object {obj} as it is not a supported type. Error details: {str(exc)}",
f"Unable to serialize the object {obj} as it is not a supported type. Error details: {exc}",
"See: https://docs.powertools.aws.dev/lambda/python/latest/core/event_handler/api_gateway/#serializing-objects",
) from exc

Expand Down
19 changes: 16 additions & 3 deletions docs/core/event_handler/api_gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,22 @@ In the following example, we use a new `Header` OpenAPI type to add [one out of

#### Serializing objects

We support the serialization of various Python objects, including Pydantic models, dataclasses, enumerations, file paths, scalar types (strings, integers, floats, and None), dictionaries, various sequence types (lists, sets, frozen sets, generators, tuples, and deques), and others defined [here](https://github.com/aws-powertools/powertools-lambda-python/blob/develop/aws_lambda_powertools/event_handler/openapi/encoders.py#L24).

For objects we do not support, such as SQLAlchemy models, we suggest providing your own [custom serializer](#custom-serializer).
With data validation enabled, we natively support serializing the following data types to JSON:

| Data type | Serialized type |
| -------------------------------------------------------------------- | -------------------------------------------------------------------------------- |
| **Pydantic models** | `dict` |
| **Python Dataclasses** | `dict` |
| **Enum** | Enum values |
| **Datetime** | Datetime ISO format string |
| **Decimal** | `int` if no exponent, or `float` |
| **Path** | `str` |
| **UUID** | `str` |
| **Set** | `list` |
| **Python primitives** _(dict, string, sequences, numbers, booleans)_ | [Python's default JSON serializable types](https://docs.python.org/3/library/json.html#encoders-and-decoders){target="_blank" rel="nofollow"} |

???+ info "See [custom serializer section](#custom-serializer) for bringing your own."
Otherwise, we will raise `SerializationError` for any unsupported types _e.g., SQLAlchemy models_.

### Accessing request details

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/event_handler/test_openapi_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,5 @@ def test_openapi_encode_with_error():
class MyClass:
__slots__ = []

with pytest.raises(SerializationError, match="Unable to serializer the object*"):
with pytest.raises(SerializationError, match="Unable to serialize the object*"):
jsonable_encoder(MyClass())