Skip to content

Bug: Data validation doesn't support the return of Response objects #3393

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
rubenfonseca opened this issue Nov 22, 2023 · 2 comments · Fixed by #3394
Closed

Bug: Data validation doesn't support the return of Response objects #3393

rubenfonseca opened this issue Nov 22, 2023 · 2 comments · Fixed by #3394
Assignees
Labels
bug Something isn't working event_handlers

Comments

@rubenfonseca
Copy link
Contributor

Expected Behaviour

Even though I'm using data validation, I would like to still be able to return Response objects, which means I can still use all the other features of the event handler, including compression, headers, status codes, etc.

Current Behaviour

Right now, I can't return a Response object from my API. To do so, I would have to change the method signature, which would defeat the data validation middleware.

Code snippet

class Model(BaseModel):
    name: str
    age: int

# WHEN a handler is defined with a body parameter
@app.post("/")
def handler(user: Model) -> Model:
    return user # can't return anything else

Possible Solution

class Model(BaseModel):
    name: str
    age: int

@app.post("/")
def handler(user: Model) -> Response[Model]:
    return Response(body=user, status_code=200)

Where Response becomes a generic class, and mypy will validate that it matches the type in the body.

Steps to Reproduce

Check the code snippet above.

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.7

Packaging format used

PyPi

Debugging logs

No response

@rubenfonseca rubenfonseca added bug Something isn't working triage Pending triage from maintainers labels Nov 22, 2023
@rubenfonseca rubenfonseca moved this from Triage to Working on it in Powertools for AWS Lambda (Python) Nov 22, 2023
@rubenfonseca rubenfonseca added this to the OpenAPI in Event Handler milestone Nov 22, 2023
@rubenfonseca rubenfonseca self-assigned this Nov 22, 2023
@rubenfonseca rubenfonseca removed the triage Pending triage from maintainers label Nov 22, 2023
@github-project-automation github-project-automation bot moved this from Working on it to Coming soon in Powertools for AWS Lambda (Python) Nov 22, 2023
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.

@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Nov 22, 2023
Copy link
Contributor

This is now released under 2.28.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Nov 23, 2023
@heitorlessa heitorlessa moved this from Coming soon to Shipped in Powertools for AWS Lambda (Python) Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working event_handlers
Projects
Status: Shipped
1 participant