Skip to content
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

fix(event-handler): strip whitespace from Content-Type headers during OpenAPI schema validation #3677

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading