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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions aws_lambda_powertools/utilities/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import typing
from typing import TYPE_CHECKING, Any, Callable, overload

from pydantic import PydanticSchemaGenerationError, ValidationError
from pydantic import PydanticSchemaGenerationError

from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
from aws_lambda_powertools.utilities.parser.exceptions import InvalidEnvelopeError, InvalidModelTypeError
Expand Down Expand Up @@ -108,7 +108,7 @@ def handler(event: Order, context: LambdaContext):

logger.debug(f"Calling handler {handler.__name__}")
return handler(parsed_event, context, **kwargs)
except (ValidationError, AttributeError) as exc:
except AttributeError as exc:
raise InvalidModelTypeError(f"Error: {str(exc)}. Please ensure the type you're trying to parse into is correct")


Expand Down Expand Up @@ -199,7 +199,7 @@ def handler(event: Order, context: LambdaContext):
# when we pass a data structure that does not match the model (trying to parse a true/false/etc into a model)
except PydanticSchemaGenerationError as exc:
raise InvalidModelTypeError(f"The event supplied is unable to be validated into {type(model)}") from exc
except ValidationError as exc:
except AttributeError as exc:
raise InvalidModelTypeError(
f"Error: {str(exc)}. Please ensure the Input model inherits from BaseModel,\n"
"and your payload adheres to the specified Input model structure.\n"
Expand Down
14 changes: 12 additions & 2 deletions tests/functional/parser/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pydantic
import pytest
from pydantic import ValidationError
from typing_extensions import Annotated

from aws_lambda_powertools.utilities.parser import (
Expand All @@ -18,7 +19,7 @@ def test_parser_unsupported_event(dummy_schema, invalid_value):
def handle_no_envelope(event: Dict, _: LambdaContext):
return event

with pytest.raises(exceptions.InvalidModelTypeError):
with pytest.raises(ValidationError):
handle_no_envelope(event=invalid_value, context=LambdaContext())


Expand Down Expand Up @@ -75,7 +76,7 @@ def validate_field(cls, value):
assert event_parsed.version == int(event_raw["version"])


@pytest.mark.parametrize("invalid_schema", [str, False, [], ()])
@pytest.mark.parametrize("invalid_schema", [False, [], ()])
def test_parser_with_invalid_schema_type(dummy_event, invalid_schema):
@event_parser(model=invalid_schema)
def handle_no_envelope(event: Dict, _: LambdaContext):
Expand Down Expand Up @@ -120,6 +121,15 @@ def handler(evt: dummy_schema, _: LambdaContext):
handler(dummy_event["payload"], LambdaContext())


def test_parser_event_with_payload_not_match_schema(dummy_event, dummy_schema):
@event_parser(model=dummy_schema)
def handler(event, _):
assert event.message == "hello world"

with pytest.raises(ValidationError):
handler({"project": "powertools"}, LambdaContext())


@pytest.mark.parametrize(
"test_input,expected",
[
Expand Down