Skip to content

Feature request: Enable to change the deserializer body #6600

@LucasCMFBraga

Description

@LucasCMFBraga

Use case

I updated the schema of my application, changing the amount field from int to float to comply with my company's requirements. However, I noticed that the default JSON parser was losing floating-point precision during requests.

def test_api_gateway_resolver_numeric_value():
    app = ApiGatewayResolver()

    @app.post("/my/path")
    def test_handler():
        return app.current_event.json_body

    # WHEN calling the event handler
    event = {}
    event.update(LOAD_GW_EVENT)
    event["body"] = '{"amount":  2.2999999999999998}'
    event["httpMethod"] = "POST"

    result = app(event, {})
    # THEN process event correctly
    assert result["statusCode"] == 200
    assert result["multiValueHeaders"]["Content-Type"] == [content_types.APPLICATION_JSON]
    assert result["body"] == '{"amount":  2.2999999999999998}' # assert '{"amount":2.3}' == '{"amount":  2.2999999999999998}'

Solution/User Experience

I've already implemented a solution and will be opening a PR soon.
The fix involves passing a custom deserializer instance to the ApiGatewayResolver constructor to handle floating-point precision correctly.

Alternative solutions

Acknowledgment

Metadata

Metadata

Assignees

Projects

Status

Working on it

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions