diff --git a/docs/en/docs/tutorial/query-params-str-validations.md b/docs/en/docs/tutorial/query-params-str-validations.md index 4765b36cbe..c6bd22c171 100644 --- a/docs/en/docs/tutorial/query-params-str-validations.md +++ b/docs/en/docs/tutorial/query-params-str-validations.md @@ -220,14 +220,20 @@ So, when you need to declare a value as required while using `Query`, you can si {* ../../docs_src/query_params_str_validations/tutorial006_an_py310.py hl[9] *} -### Required, can be `None` { #required-can-be-none } +### Optional with `None` { #required-can-be-none } -You can declare that a parameter can accept `None`, but that it's still required. This would force clients to send a value, even if the value is `None`. +You can declare that a parameter is optional and can accept `None`, even when using `Query` for validations. -To do that, you can declare that `None` is a valid type but simply do not declare a default value: +To do that, you can declare that `None` is a valid type and use `None` as the default value: {* ../../docs_src/query_params_str_validations/tutorial006c_an_py310.py hl[9] *} +/// tip + +This is the same as using `Query(None, min_length=3)`, the parameter is optional and will have a default value of `None`. + +/// + ## Query parameter list / multiple values { #query-parameter-list-multiple-values } When you define a query parameter explicitly with `Query` you can also declare it to receive a list of values, or said in another way, to receive multiple values. diff --git a/docs_src/query_params_str_validations/tutorial006c_an_py310.py b/docs_src/query_params_str_validations/tutorial006c_an_py310.py index 2995d9c979..43e5f58b51 100644 --- a/docs_src/query_params_str_validations/tutorial006c_an_py310.py +++ b/docs_src/query_params_str_validations/tutorial006c_an_py310.py @@ -6,7 +6,7 @@ app = FastAPI() @app.get("/items/") -async def read_items(q: Annotated[str | None, Query(min_length=3)]): +async def read_items(q: Annotated[str | None, Query(min_length=3)] = None): results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} if q: results.update({"q": q}) diff --git a/docs_src/query_params_str_validations/tutorial006c_py310.py b/docs_src/query_params_str_validations/tutorial006c_py310.py index 88b499c7af..3dbdd27449 100644 --- a/docs_src/query_params_str_validations/tutorial006c_py310.py +++ b/docs_src/query_params_str_validations/tutorial006c_py310.py @@ -4,7 +4,7 @@ app = FastAPI() @app.get("/items/") -async def read_items(q: str | None = Query(min_length=3)): +async def read_items(q: str | None = Query(min_length=3, default=None)): results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} if q: results.update({"q": q}) diff --git a/tests/test_tutorial/test_query_params_str_validations/test_tutorial006c.py b/tests/test_tutorial/test_query_params_str_validations/test_tutorial006c.py index 74c10b885d..e576d1786d 100644 --- a/tests/test_tutorial/test_query_params_str_validations/test_tutorial006c.py +++ b/tests/test_tutorial/test_query_params_str_validations/test_tutorial006c.py @@ -22,24 +22,18 @@ def get_client(request: pytest.FixtureRequest): return client -@pytest.mark.xfail( - reason="Code example is not valid. See https://github.com/fastapi/fastapi/issues/12419" -) def test_query_params_str_validations_no_query(client: TestClient): response = client.get("/items/") assert response.status_code == 200 - assert response.json() == { # pragma: no cover + assert response.json() == { "items": [{"item_id": "Foo"}, {"item_id": "Bar"}], } -@pytest.mark.xfail( - reason="Code example is not valid. See https://github.com/fastapi/fastapi/issues/12419" -) def test_query_params_str_validations_empty_str(client: TestClient): response = client.get("/items/?q=") assert response.status_code == 200 - assert response.json() == { # pragma: no cover + assert response.json() == { "items": [{"item_id": "Foo"}, {"item_id": "Bar"}], } @@ -99,7 +93,7 @@ def test_openapi_schema(client: TestClient): "operationId": "read_items_items__get", "parameters": [ { - "required": True, + "required": False, "schema": { "anyOf": [ {"type": "string", "minLength": 3},