fix(event_handler): normalize Union and RootModel sequences in body validation#8067
Conversation
|
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
…n/RootModel The original fix only checked one level deep. This makes _is_or_contains_sequence recursive so it catches: - Optional[RootModel[List[Model]]] (Union containing RootModel) - RootModel[Union[Model, List[Model]]] (RootModel wrapping Union) Adds 16 regression tests covering edge cases: Optional, empty list, single-element list, pipe syntax, cross-resolver, large payloads, etc. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
leandrodamascena
left a comment
There was a problem hiding this comment.
Hey @danjhd thanks a lot for working on this. I fixed errors in mypy/ruff and asked Claude to add more tests.
Everything is fine to merge now.
APPROVED!
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #8067 +/- ##
===========================================
- Coverage 96.65% 96.64% -0.01%
===========================================
Files 282 282
Lines 13775 13790 +15
Branches 1096 1102 +6
===========================================
+ Hits 13314 13328 +14
Misses 339 339
- Partials 122 123 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
3137ecb
into
aws-powertools:develop
|
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |



Issue number: closes #8057
Summary
Changes
Fixed a bug in the OpenAPI validation middleware where body parameters with Union types containing sequences (e.g., Union[Model, List[Model]]) or RootModel[List[Model]] were incorrectly normalized. The middleware was extracting only the first element from list inputs instead of preserving the full list.
Key changes:
User experience
Before:
When defining an endpoint with a body parameter like Union[Item, List[Item]] and sending a JSON array with multiple items:
Sending
[{"name": "item1"}, {"name": "item2"}, {"name": "item3"}]would incorrectly pass only the first item to the handler, causing data loss.After:
The handler now correctly receives the full list when a list is sent, or a single item when a single object is sent. Both
Union[Model, List[Model]]andRootModel[List[Model]]patterns work as expected, allowing flexible API designs that accept either single items or collections.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.