Skip to content
Open
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
6 changes: 5 additions & 1 deletion flask_pydantic/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ def validate_many_models(
def validate_path_params(func: Callable, kwargs: dict) -> Tuple[dict, list]:
errors = []
validated = {}
# Only validate parameters that are actual path parameters from the route
# request.view_args contains only the path parameters extracted from the URL
path_param_names = set(request.view_args.keys()) if request.view_args else set()

for name, type_ in func.__annotations__.items():
if name in {"query", "body", "form", "return"}:
if name not in path_param_names:
continue
try:
if not isinstance(type_, V1BaseModel):
Expand Down
54 changes: 53 additions & 1 deletion tests/func/test_app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import re
from typing import List, Optional
from functools import wraps
from typing import Any, Callable, List, Optional

import pytest
from flask import jsonify, request
Expand Down Expand Up @@ -89,6 +90,27 @@ def int_path_param(obj_id):
return IdObj(id=obj_id)


@pytest.fixture
def app_with_path_param_route_and_injector(app):
class Injectable:
def __init__(self, foo: str):
self.foo = foo

def inject(fun: Callable) -> Callable:
@wraps(fun)
def wrapper(*args: Any, **kwargs: Any) -> Any:
kwargs["injectable"] = Injectable("injected")
return fun(*args, **kwargs)

return wrapper

@app.route("/path_param/<obj_id>/", methods=["GET"])
@inject
@validate()
def int_path_param(obj_id: int, injectable: Injectable):
return {"id": obj_id, "injectable": injectable.foo}


@pytest.fixture
def app_with_custom_root_type(app):
class Person(BaseModel):
Expand Down Expand Up @@ -429,6 +451,36 @@ def test_str_param_passes(self, client):
assert_matches(expected_response, response.json)


@pytest.mark.usefixtures("app_with_path_param_route_and_injector")
class TestPathIntParameterAndInjector:
def test_correct_param_passes(self, client):
id_ = 12
expected_response = {"id": id_, "injectable": "injected"}
response = client.get(f"/path_param/{id_}/")
assert_matches(expected_response, response.json)

def test_string_parameter(self, client):
expected_response = {
"validation_error": {
"path_params": [
{
"input": "not_an_int",
"loc": ["obj_id"],
"msg": "Input should be a valid integer, unable to parse string as an integer",
"type": "int_parsing",
"url": re.compile(
r"https://errors\.pydantic\.dev/.*/v/int_parsing"
),
}
]
}
}
response = client.get("/path_param/not_an_int/")

assert_matches(expected_response, response.json)
assert response.status_code == 400


@pytest.mark.usefixtures("app_with_optional_body")
class TestGetJsonParams:
def test_empty_body_fails(self, client):
Expand Down
Loading