Skip to content

Bug: Use appropriate error when passing non string query params #7364

@aalvrz

Description

@aalvrz

Expected Behaviour

A correctly raised exception with a descriptive message. For example:

TypeError: Query param value must be a string

Current Behaviour

When using enable_validation=True and declaring integer query params in routes, passing string values results in the following error:

    return {k: v.split(",") for k, v in self.query_string_parameters.items()}
               ^^^^^^^
AttributeError: 'int' object has no attribute 'split'

This is really misleading and forces the developer to dig into Powertools source code to determine what is going on.

Code snippet

#### App Code

app = APIGatewayRestResolver(enabled_validation=True)


@app.get("/todos")
def get_todos(page_num: Annotated[int, Query()] = None):
    pass



#### Test Code

event = {
    # ...
    "queryStringParameters": {"page_num": 5}  # <-- Integer value
}

Possible Solution

It looks like Powertools assumes values to be strings, based on the usage of split(). If so, then we should probably raise a TypeError when encountering a query param value that is not a string prior to automatically splitting.

Steps to Reproduce

See Code Snippet

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.9

Packaging format used

PyPi

Debugging logs

Metadata

Metadata

Labels

documentationImprovements or additions to documentationnot-a-bugpending-releaseFix or implementation already in dev waiting to be released

Type

No type

Projects

Status

Coming soon

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions