Skip to content

fix(parser): revert a regression in v3 when raising ValidationError #5259

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
merged 3 commits into from
Sep 27, 2024

Conversation

leandrodamascena
Copy link
Contributor

@leandrodamascena leandrodamascena commented Sep 27, 2024

Issue number: #5252

Summary

Changes

This PR addresses an issue where we were incorrectly catching and re-raising ValidationError as InvalidModelTypeError. This regression was introduced in v3.0.0

More context:
In v2 we raise ValidationError directly from Pydantic when we can't validate data against model, but in v3 we were catching ValidationError and re-raising it as InvalidModelTypeError. However, ValidationError is a data validation error that many of our customers use to catch exceptions when data is incorrect. By re-raising it as a different error type, we were breaking this expected behavior.

Potential impact:
Any customer that updated to v3 and is relying on InvalidModelTypeError for what were originally ValidationError will need to be updated the code again.

Solution:

  • Removed the code that catches ValidationError and re-raises it as InvalidModelTypeError.
  • ValidationError will now be raised as is, preserving the original error type and message.
  • Created a specific test for this

User experience

Before this PR

from aws_lambda_powertools.utilities.parser import parse, BaseModel, ValidationError
from typing import List, Optional

class OrderItem(BaseModel):
    id: int
    quantity: int
    description: str

class Order(BaseModel):
    id: int
    description: str
    items: List[OrderItem] # nesting models are supported
    optional_field: Optional[str] = None # this field may or may not be available when parsing


payload = {
    "id": 10876546789,
    "description": "My order",
    "items": [
        {
            # this will cause a validation error
            "id": [1015938732],
            "quantity": 1,
            "description": "item xpto"
        }
    ]
}

def my_function():
    try:
        parsed_payload: Order = parse(event=payload, model=Order)
        # payload dict is now parsed into our model
        return parsed_payload.items
    except ValidationError:
        return {
            "status_code": 400,
            "message": "Invalid order"
        }
/home/john/.pyenv/versions/sftp-processor-3.12/lib/python3.12/site-packages/aws_lambda_powertools/package_logger.py:20: UserWarning: POWERTOOLS_DEBUG environment variable is enabled. Setting logging level to DEBUG.
  if powertools_debug_is_set():
2024-09-26 16:07:09,201 aws_lambda_powertools.utilities.parser.parser [DEBUG] Parsing and validating event model; no envelope used
Traceback (most recent call last):
  File "/home/john/.pyenv/versions/sftp-processor-3.12/lib/python3.12/site-packages/aws_lambda_powertools/utilities/parser/parser.py", line 195, in parse
    return adapter.validate_python(event)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/john/.pyenv/versions/sftp-processor-3.12/lib/python3.12/site-packages/pydantic/type_adapter.py", line 135, in wrapped
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/john/.pyenv/versions/sftp-processor-3.12/lib/python3.12/site-packages/pydantic/type_adapter.py", line 366, in validate_python
    return self.validator.validate_python(object, strict=strict, from_attributes=from_attributes, context=context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for Order
items.0.id
  Input should be a valid integer [type=int_type, input_value=[1015938732], input_type=list]
    For further information visit https://errors.pydantic.dev/2.9/v/int_type

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/john/sandbox/sftp-processor/sftp-request-processor/sftp_request_model.py", line 260, in <module>
    parsed_payload: Order = parse(event=payload, model=Order)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/john/.pyenv/versions/sftp-processor-3.12/lib/python3.12/site-packages/aws_lambda_powertools/utilities/parser/parser.py", line 203, in parse
    raise InvalidModelTypeError(
aws_lambda_powertools.utilities.parser.exceptions.InvalidModelTypeError: Error: 1 validation error for Order
items.0.id
  Input should be a valid integer [type=int_type, input_value=[1015938732], input_type=list]
    For further information visit https://errors.pydantic.dev/2.9/v/int_type. Please ensure the Input model inherits from BaseModel,
and your payload adheres to the specified Input model structure.
Model=<class '__main__.Order'>

After this PR

Customers can catch ValidationError as expected.

{'status_code': 400, 'message': 'Invalid order'}

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

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.

@leandrodamascena leandrodamascena requested a review from a team September 27, 2024 08:11
@boring-cyborg boring-cyborg bot added the tests label Sep 27, 2024
@pull-request-size pull-request-size bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Sep 27, 2024
@github-actions github-actions bot added the bug Something isn't working label Sep 27, 2024
Copy link

codecov bot commented Sep 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.20%. Comparing base (72b9546) to head (da03498).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #5259      +/-   ##
===========================================
- Coverage    96.21%   96.20%   -0.01%     
===========================================
  Files          229      229              
  Lines        10753    10753              
  Branches      2002     2002              
===========================================
- Hits         10346    10345       -1     
- Misses         321      322       +1     
  Partials        86       86              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for fixing this, as discussed earlier today let's treat this as a regression and call out the fix in the release notes.

Copy link

@leandrodamascena leandrodamascena merged commit 0327666 into develop Sep 27, 2024
10 of 11 checks passed
@leandrodamascena leandrodamascena deleted the fix/parser-regression branch September 27, 2024 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working size/S Denotes a PR that changes 10-29 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

parse function raises InvalidModelTypeError instead of ValidationError
2 participants