diff --git a/CHANGELOG.md b/CHANGELOG.md index e0fbb34b68..9342503c3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,8 +15,8 @@ ## Maintenance * **deps:** bump squidfunk/mkdocs-material from `58eef6c` to `9aad7af` in /docs ([#3670](https://github.com/aws-powertools/powertools-lambda-python/issues/3670)) -* **deps:** bump codecov/codecov-action from 3.1.4 to 3.1.5 ([#3674](https://github.com/aws-powertools/powertools-lambda-python/issues/3674)) * **deps:** bump pydantic from 1.10.13 to 1.10.14 ([#3655](https://github.com/aws-powertools/powertools-lambda-python/issues/3655)) +* **deps:** bump codecov/codecov-action from 3.1.4 to 3.1.5 ([#3674](https://github.com/aws-powertools/powertools-lambda-python/issues/3674)) * **deps:** bump the layer-balancer group in /layer/scripts/layer-balancer with 1 update ([#3665](https://github.com/aws-powertools/powertools-lambda-python/issues/3665)) * **deps-dev:** bump aws-cdk from 2.122.0 to 2.123.0 ([#3673](https://github.com/aws-powertools/powertools-lambda-python/issues/3673)) * **deps-dev:** bump ruff from 0.1.13 to 0.1.14 ([#3656](https://github.com/aws-powertools/powertools-lambda-python/issues/3656)) diff --git a/aws_lambda_powertools/event_handler/middlewares/openapi_validation.py b/aws_lambda_powertools/event_handler/middlewares/openapi_validation.py index e819947b14..fd7507603d 100644 --- a/aws_lambda_powertools/event_handler/middlewares/openapi_validation.py +++ b/aws_lambda_powertools/event_handler/middlewares/openapi_validation.py @@ -212,7 +212,7 @@ def _get_body(self, app: EventHandlerInstance) -> Dict[str, Any]: """ content_type_value = app.current_event.get_header_value("content-type") - if not content_type_value or content_type_value.startswith("application/json"): + if not content_type_value or content_type_value.strip().startswith("application/json"): try: return app.current_event.json_body except json.JSONDecodeError as e: diff --git a/tests/functional/event_handler/test_openapi_validation_middleware.py b/tests/functional/event_handler/test_openapi_validation_middleware.py index ea4305257d..23fa131ab9 100644 --- a/tests/functional/event_handler/test_openapi_validation_middleware.py +++ b/tests/functional/event_handler/test_openapi_validation_middleware.py @@ -289,6 +289,32 @@ def handler(user: Model) -> Model: assert json.loads(result["body"]) == {"name": "John", "age": 30} +def test_validate_body_param_with_stripped_headers(): + # GIVEN an APIGatewayRestResolver with validation enabled + app = APIGatewayRestResolver(enable_validation=True) + + class Model(BaseModel): + name: str + age: int + + # WHEN a handler is defined with a body parameter + # WHEN headers has spaces + @app.post("/") + def handler(user: Model) -> Model: + return user + + LOAD_GW_EVENT["httpMethod"] = "POST" + LOAD_GW_EVENT["headers"] = {"Content-type": " application/json "} + LOAD_GW_EVENT["path"] = "/" + LOAD_GW_EVENT["body"] = json.dumps({"name": "John", "age": 30}) + + # THEN the handler should be invoked and return 200 + # THEN the body must be a JSON object + result = app(LOAD_GW_EVENT, {}) + assert result["statusCode"] == 200 + assert json.loads(result["body"]) == {"name": "John", "age": 30} + + def test_validate_body_param_with_invalid_date(): # GIVEN an APIGatewayRestResolver with validation enabled app = APIGatewayRestResolver(enable_validation=True)