diff --git a/docs/en/docs/tutorial/query-params.md b/docs/en/docs/tutorial/query-params.md index 0ed34c998..4596880f2 100644 --- a/docs/en/docs/tutorial/query-params.md +++ b/docs/en/docs/tutorial/query-params.md @@ -238,7 +238,7 @@ And when you open your browser at -However, since the query parameters are decaled in the request as +However, since the query parameters are decaled in the request as ``` http://127.0.0.1:8000/query/mixed-type-params?query=1&foo=bar&foo=baz @@ -264,4 +264,4 @@ http://127.0.0.1:8000/query/mixed-type-params?query=1&foo=bar&foo=baz } } ``` -As you can see the `query` parameter is added to every `Query` argument for which it is valid. This is because **FastAPI** does not know which `Query` argument you want to add the `query` parameter to, and `1` validates as an `int`, `str`, and `List[str]`. `foo` is only added to the `string_mapping` and `sequence_mapping_queries` arguments because it is not a valid `int`. \ No newline at end of file +As you can see the `query` parameter is added to every `Query` argument for which it is valid. This is because **FastAPI** does not know which `Query` argument you want to add the `query` parameter to, and `1` validates as an `int`, `str`, and `List[str]`. `foo` is only added to the `string_mapping` and `sequence_mapping_queries` arguments because it is not a valid `int`. diff --git a/docs_src/query_params/tutorial007_py310.py b/docs_src/query_params/tutorial007_py310.py index d8a9f5079..7d0ffca04 100644 --- a/docs_src/query_params/tutorial007_py310.py +++ b/docs_src/query_params/tutorial007_py310.py @@ -1,4 +1,4 @@ -from typing import Mapping, List +from typing import List, Mapping from fastapi import FastAPI, Query @@ -13,8 +13,8 @@ def get_mixed_mapping_mixed_type_query_params( sequence_mapping_queries: Mapping[str, List[int]] = Query({}), ): return { - "query":query, - "string_mapping": mapping_query_str, - "mapping_query_int": mapping_query_int, - "sequence_mapping_queries": sequence_mapping_queries - } + "query": query, + "string_mapping": mapping_query_str, + "mapping_query_int": mapping_query_int, + "sequence_mapping_queries": sequence_mapping_queries, + } diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index ccb9f6e4e..98a91ccf2 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -694,7 +694,8 @@ def request_params_to_args( value[key][index] = marker for key in value: value[key] = [x for x in value[key] if x != marker] - values[field.name] = value + v_, _ = field.validate(value, values, loc=loc) + values[field.name] = v_ elif ( isinstance(errors_, list) and is_scalar_mapping_field(field) @@ -704,7 +705,8 @@ def request_params_to_args( # remove all invalid parameters for _, _, key in [error["loc"] for error in new_errors]: value.pop(key) - values[field.name] = value + v_, _ = field.validate(value, values, loc=loc) + values[field.name] = v_ elif isinstance(errors_, list): new_errors = _regenerate_error_with_loc(errors=errors_, loc_prefix=()) errors.extend(new_errors) diff --git a/tests/test_tutorial/test_query_params/test_tutorial007_py310.py b/tests/test_tutorial/test_query_params/test_tutorial007_py310.py index 169742f8f..a6dfc140f 100644 --- a/tests/test_tutorial/test_query_params/test_tutorial007_py310.py +++ b/tests/test_tutorial/test_query_params/test_tutorial007_py310.py @@ -1,33 +1,21 @@ import pytest -from dirty_equals import IsDict from fastapi.testclient import TestClient -from fastapi.utils import match_pydantic_error_url @pytest.fixture(name="client") def get_client(): - from docs_src.query_params.tutorial006 import app + from docs_src.query_params.tutorial007_py310 import app c = TestClient(app) return c def test_foo_needy_very(client: TestClient): - response = client.get("/items/foo?needy=very") + response = client.get("/query/mixed-type-params?query=1&query=2&foo=bar&foo=baz") assert response.status_code == 200 assert response.json() == { - "query": 1, - "string_mapping": { - "query": "1", - "foo": "baz" - }, - "mapping_query_int": { - "query": 1 - }, - "sequence_mapping_queries": { - "query": [ - "1" - ], - "foo": [] - } - } \ No newline at end of file + "query": 2, + "string_mapping": {"query": "2", "foo": "baz"}, + "mapping_query_int": {"query": 2}, + "sequence_mapping_queries": {"query": [1, 2], "foo": []}, + }