-
Notifications
You must be signed in to change notification settings - Fork 455
feat(event-handler): add support for Pydantic Field discriminator in validation #7227
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
Conversation
…validation (aws-powertools#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
Hey @dap0am please run |
Done, thanks. |
Thanks for fixing! You can run |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @dap0am! Please let me know if you need any help here.
Hi @leandrodamascena nope, I am just waiting for your review. |
Hi @dap0am, can you fix the CI and make the appropriate changes so as not to break the current tests? The CI is failing because there are some refactorings that can introduce bug regressions into the existing code and break all customers using it. Use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dap0am please fix the CI before we start the review.
Will do that, thanks. |
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.
Hi @dap0am, some of the unit tests are failing - please take a look if you can and also resolve the SonarCloud issues listed in the comment above. Thanks! |
…rCloud 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
Apply ruff formatting to params.py to resolve failing format check in CI
@leandrodamascena @dreamorosi I have fixed the last issue causing the tests to fail. |
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).
Hi @dap0am, the tests are failing, so we're unable to merge this. Please make sure to run the tests as discussed above before pushing code. We'll try a couple more iterations, but if we're unable to get to a green CI and no SonarCloud issues we might close the PR. |
Thanks for your comment, I was having issue running the full test suite locally, but I think I have fixed it now, as my test are now passing. |
Thanks! If there's anything we can improve to make it easier for contributors to run it, please let us know! |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #7227 +/- ##
========================================
Coverage 96.36% 96.37%
========================================
Files 275 275
Lines 13027 13048 +21
Branches 970 974 +4
========================================
+ Hits 12554 12575 +21
Misses 366 366
Partials 107 107 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @dap0am thanks for working on this! Overall it looks good to me and the code is working as expected. I see Codecov is complaining about missing coverage and I'll take a look. Also, we need to update the documentation. Do you want to update or do you want me to update the documentation to include we support this new field?
Thanls
I can put something in the documentation. |
Thanks! Please let me know if need help. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI is failing @dap0am!
Format issue in the example file, I have fixed it now and passed the test locally. |
@leandrodamascena CI passed now, aside the codecov issue you mentioned to look into, let me know when you review my changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @dap0am thanks for working on this! I just made some small changes in methods names + documentation and we are good to merge!.
APPROVED! 🚀
|
Thanks @leandrodamascena and @dreamorosi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GTM
Merge conflicts resolved after upstream PRs aws-powertools#7227 and aws-powertools#7253: Core Changes: - Updated File class rename from File -> _File with public File alias - Fixed Union import for _resolve_field_type function - Removed unused _File import in dependant.py - Maintained all UploadFile and validation functionality Testing: - All 25 comprehensive tests passing - 96.36%+ codecov coverage maintained - Code quality checks passing (make format && make pr) Compatibility: - Preserves public File API for backward compatibility - Maintains UploadFile OpenAPI schema generation - All validation middleware features intact
Summary
This PR adds support for Pydantic's
Field(discriminator='...')
syntax in event handler validation, enabling tagged unions to work correctly with the validation middleware.Fixes #5953
Changes
get_field_info_annotated_type()
inparams.py
to handleField(discriminator) + Body()
combinationsModelField.__post_init__()
incompat.py
to preserve discriminator metadata when creating TypeAdaptersTesting
test_field_discriminator_validation()
to verify discriminator functionality with tagged unionstest_field_other_features_still_work()
to ensure other Field features remain functionalExample Usage
Checklist
Acknowledgment
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.