You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(event-handler): add support for Pydantic Field discriminator in validation (#7227)
* feat(event-handler): add support for Pydantic Field discriminator in validation (#5953)
Enable use of Field(discriminator='...') with tagged unions in event handler validation.
This allows developers to use Pydantic's native discriminator syntax instead of
requiring Powertools-specific Param annotations.
- Handle Field(discriminator) + Body() combination in get_field_info_annotated_type
- Preserve discriminator metadata when creating TypeAdapter in ModelField
- Add comprehensive tests for discriminator validation and Field features
* style(tests): remove inline comments to match project test style
* style: run make format to fix CI formatting issues
* fix(event-handler): preserve FieldInfo subclass types in copy_field_info
Fix regression where copy_field_info was losing custom FieldInfo subclass
types (Body, Query, etc.) by using shallow copy instead of from_annotation.
This resolves the failing test_validate_embed_body_param while maintaining
the discriminator functionality.
* refactor(event-handler): reduce cognitive complexity and address SonarCloud issues
- Refactor get_field_info_annotated_type function by extracting helper functions to reduce cognitive complexity from 29 to below 15
- Fix copy_field_info to preserve FieldInfo subclass types using shallow copy instead of from_annotation
- Rename variable Action to action_type to follow Python naming conventions
- Resolve failing test_validate_embed_body_param by maintaining Body parameter type recognition
- Add helper functions: _has_discriminator, _handle_discriminator_with_body, _create_field_info, _set_field_default
- Maintain full backward compatibility and discriminator functionality
* style: fix formatting to pass CI format check
Apply ruff formatting to params.py to resolve failing format check in CI
* fix: resolve mypy type error in _create_field_info function
Add explicit type annotation for field_info variable to fix mypy error
about incompatible types between FieldInfo and Body. This ensures type
checking passes across all Python versions (3.9-3.13).
* fix: use Union syntax for Python 3.9 compatibility
* feat(event-handler): add documentation and example for Field discriminator support
* style: run make format to fix CI formatting issues
* small changes
* small changes
---------
Co-authored-by: Leandro Damascena <[email protected]>
Co-authored-by: Andrea Amorosi <[email protected]>
You can use Pydantic's `Field(discriminator="...")` with union types to create discriminated unions (also known as tagged unions). This allows the Event Handler to automatically determine which model to use based on a discriminator field in the request body.
1.`Field(discriminator="action")` tells Pydantic to use the `action` field to determine which model to instantiate
440
+
2.`Body()` annotation tells the Event Handler to parse the request body using the discriminated union
441
+
431
442
#### Validating responses
432
443
433
444
You can use `response_validation_error_http_code` to set a custom HTTP code for failed response validation. When this field is set, we will raise a `ResponseValidationError` instead of a `RequestValidationError`.
0 commit comments