Skip to content

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

Closed
@rubenfonseca

Description

@rubenfonseca

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Shipped

Relationships

None yet

Development

No branches or pull requests

Issue actions