Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Validation-Inconsistency on REST-API #4125

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

Closed
dschro-1993 opened this issue Apr 12, 2024 · 11 comments
Closed

Validation-Inconsistency on REST-API #4125

dschro-1993 opened this issue Apr 12, 2024 · 11 comments
Assignees
Labels
bug Something isn't working layers Lambda Layers work

Comments

@dschro-1993
Copy link

dschro-1993 commented Apr 12, 2024

Expected Behaviour

Hey Guys, when I create a BaseModel like this

class Foo(BaseModel):
  AccountId: str = Field(pattern=r"^\d{12}$")

And create an Instance of it like this:

foo = Foo(AccountId="12345678901a")

you will see the following exception:

...should match pattern '^\d{12}$' [type=pattern_mismatch, input_value='12345678901a', input_type=str]

If I let the APIGatewayRestResolver do the validation implicitly:

api = event_handler.APIGatewayRestResolver(
  enable_validation = True
)

And pass a payload to the endpoint like:

{"AccountId": "12345678901a"}

it is accepted/valid...do you know why that is?
When min_length and max_length is used for validation only, I see the expected behaviour...
But pattern seems to be skipped/iqnored when validation is done implicitly.
Any help is appreciated.

Current Behaviour

APIGatewayRestResolver should return same Exception and 422 when payload does not match pattern.

Code snippet

see above

Possible Solution

No response

Steps to Reproduce

see above

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.12

Debugging logs

No response

@dschro-1993 dschro-1993 added bug Something isn't working triage Pending triage from maintainers labels Apr 12, 2024
Copy link

boring-cyborg bot commented Apr 12, 2024

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@rubenfonseca
Copy link
Contributor

Hi @dschro-1993 can you show me your api handler code that uses Foo?

@dschro-1993
Copy link
Author

Handler:

from aws_lambda_powertools import event_handler

# ...router-imports...

api = event_handler.APIGatewayRestResolver(
  enable_validation = True
)

api.include_router(...)

def entrypoint(request: dict, context: dict) -> dict:
  return api.resolve(
    request,
    context,
  )

Router:

from aws_lambda_powertools.event_handler.api_gateway import Router

from models import Foo

router = Router()

@router.post("/foo")
def create(foo: Foo) -> dict:
  """Foo Create-Request"""
  # ...

Model:

class Foo(BaseModel):
  AccountId: str = Field(pattern=r"^\d{12}$")

@rubenfonseca
Copy link
Contributor

Can you please tell me which Pydantic version are you using?

I've just deployed this code for a test:

from aws_lambda_powertools import Logger
from aws_lambda_powertools.event_handler import (
    APIGatewayRestResolver,
)
from aws_lambda_powertools.event_handler.api_gateway import Router
from pydantic import Field, BaseModel

logger = Logger()


class Foo(BaseModel):
    AccountId: str = Field(pattern=r"^\d{12}$")


router = Router()


@router.post(
    "/foo",
)
def helloworld(foo: Foo) -> dict:
    logger.info("Hello, World!", foo=foo)
    return {"accountId": foo.AccountId}


app = APIGatewayRestResolver(enable_validation=True)
app.include_router(router)


@logger.inject_lambda_context
def lambda_handler(event, context):
    return app.resolve(event, context)

A request with a wrong body failed:
image

But a request with a good body succeeded:
image

@rubenfonseca rubenfonseca added need-more-information Pending information to continue and removed triage Pending triage from maintainers labels Apr 12, 2024
@rubenfonseca rubenfonseca moved this from Triage to Pending customer in Powertools for AWS Lambda (Python) Apr 12, 2024
@dschro-1993
Copy link
Author

I use the Powertools lambda layer arm64 in its latest version:
https://docs.powertools.aws.dev/lambda/python/latest/#lambda-layer

# ...
resource "aws_lambda_function" "foo" {
  #...
  layers = [
    "arn:aws:lambda:${data.aws_region.current.name}:017000801446:layer:AWSLambdaPowertoolsPythonV2-Arm64:68"
  ]
}

@dschro-1993
Copy link
Author

Oh damn, that layer is built upon Pydantic v1, correct? So I would have to bundle v2 on my own if I want to see v2 behaviour in my lambda?

@rubenfonseca
Copy link
Contributor

Let me try locally with v1 first to understand if this is the cause of problem

@rubenfonseca
Copy link
Contributor

Got it, with Pydantic v1 you have to use regex instead of pattern! Just tested it and it works :D

image

We have an issue collecting interest for a managed Layer that includes Pydantic v2. Can you add your +1 there?

If this solves your problem, let me know if we can close this issue :)

@dschro-1993
Copy link
Author

Thanks for your investigation, that was very helpful!
Left a +1 for the v2 layer :)

@rubenfonseca
Copy link
Contributor

Awesome! Thank you for opening this report :) Closing it for now

@github-project-automation github-project-automation bot moved this from Pending customer to Coming soon in Powertools for AWS Lambda (Python) Apr 12, 2024
@rubenfonseca rubenfonseca added need-more-information Pending information to continue layers Lambda Layers work and removed need-more-information Pending information to continue labels Apr 12, 2024
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@rubenfonseca rubenfonseca self-assigned this Apr 12, 2024
@rubenfonseca rubenfonseca removed the need-more-information Pending information to continue label Apr 12, 2024
@leandrodamascena leandrodamascena moved this from Coming soon to Shipped in Powertools for AWS Lambda (Python) Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working layers Lambda Layers work
Projects
Status: Shipped
Development

No branches or pull requests

2 participants