Skip to content

Conversation

dap0am
Copy link
Contributor

@dap0am dap0am commented Aug 21, 2025

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

  • Enhanced get_field_info_annotated_type() in params.py to handle Field(discriminator) + Body() combinations
  • Modified ModelField.__post_init__() in compat.py to preserve discriminator metadata when creating TypeAdapters
  • Added comprehensive tests for discriminator validation and other Field features

Testing

  • Added test_field_discriminator_validation() to verify discriminator functionality with tagged unions
  • Added test_field_other_features_still_work() to ensure other Field features remain functional
  • All existing tests pass without modification

Example Usage

from typing import Literal
from typing_extensions import Annotated
from pydantic import BaseModel, Field
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.event_handler.openapi.params import Body

app = APIGatewayRestResolver(enable_validation=True)

class FooAction(BaseModel):
    action: Literal["foo"]
    foo_data: str

class BarAction(BaseModel):
    action: Literal["bar"]
    bar_data: int

# Now works with Field discriminator
Action = Annotated[FooAction | BarAction, Field(discriminator="action")]

@app.post("/actions")
def create_action(action: Annotated[Action, Body()]):
    return {"received": action.model_dump()}

Checklist

  • Tests are passing
  • Linting and type checking pass
  • Follows project contribution guidelines
  • Maintains backward compatibility

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

…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
@dap0am dap0am requested a review from a team as a code owner August 21, 2025 08:30
@dap0am dap0am requested a review from anafalcao August 21, 2025 08:30
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 21, 2025
@leandrodamascena
Copy link
Contributor

Hey @dap0am please run make format to fix the CI.

Copy link

@dap0am
Copy link
Contributor Author

dap0am commented Aug 21, 2025

Hey @dap0am please run make format to fix the CI.

Done, thanks.

@leandrodamascena
Copy link
Contributor

Hey @dap0am please run make format to fix the CI.

Done, thanks.

Thanks for fixing! You can run make pr before commit the code. So, you can see if there are errors in the code you refactored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event_handlers size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Support types annotated with Field for event_handler validation
2 participants