From b1c309a666dcecad92b8169537aaaf93cfdabb24 Mon Sep 17 00:00:00 2001 From: Chaitanya Sai Meka Date: Mon, 10 Nov 2025 14:55:38 +0530 Subject: [PATCH] docs: fix inline code formatting and add tested example for Required, can be None section --- .../tutorial/query-params-str-validations.md | 19 ++++--------------- .../tutorial006d_an_py310.py | 10 ++++++++++ .../test_tutorial006d_an_py310.py | 9 +++++++++ 3 files changed, 23 insertions(+), 15 deletions(-) create mode 100644 docs_src/query_params_str_validations/tutorial006d_an_py310.py create mode 100644 tests/test_tutorial/test_query_params_str_validations/test_tutorial006d_an_py310.py diff --git a/docs/en/docs/tutorial/query-params-str-validations.md b/docs/en/docs/tutorial/query-params-str-validations.md index 332636080..d709fe561 100644 --- a/docs/en/docs/tutorial/query-params-str-validations.md +++ b/docs/en/docs/tutorial/query-params-str-validations.md @@ -271,23 +271,12 @@ For example, you might try: q: Annotated[str | None, Query(min_length=3)] = ... ``` -But this will still expect a string value, and if the client omits q or tries to send q=None, FastAPI will raise a validation error. -In other words, None is not an acceptable runtime value for query parameters — only strings are. +But this will still expect a **string** value, and if the client omits `q` or tries to send `q=None`, FastAPI will raise a validation error. +In other words, `None` is not an acceptable runtime value for query parameters — only strings are. -If you want to accept special values (like "None" or an empty string) and interpret them as None in your application, you can handle them manually in your function: +If you want to accept special values (like `"None"` or an empty string) and interpret them as `None` in your application, you can handle them manually in your function: -```Python -from fastapi import FastAPI, Query -from typing import Annotated - -app = FastAPI() - -@app.get("/items/") -async def read_items(q: Annotated[str | None, Query()] = ...): - if q in ("None", "", "null"): - q = None - return {"q": q} -``` +{* ../../docs_src/query_params_str_validations/tutorial006d_an_py310.py hl[9] *} ## Query parameter list / multiple values { #query-parameter-list-multiple-values } diff --git a/docs_src/query_params_str_validations/tutorial006d_an_py310.py b/docs_src/query_params_str_validations/tutorial006d_an_py310.py new file mode 100644 index 000000000..0962fbce7 --- /dev/null +++ b/docs_src/query_params_str_validations/tutorial006d_an_py310.py @@ -0,0 +1,10 @@ +from fastapi import FastAPI, Query +from typing import Annotated + +app = FastAPI() + +@app.get("/items/") +async def read_items(q: Annotated[str | None, Query()] = ...): + if q in ("None", "", "null"): + q = None + return {"q": q} diff --git a/tests/test_tutorial/test_query_params_str_validations/test_tutorial006d_an_py310.py b/tests/test_tutorial/test_query_params_str_validations/test_tutorial006d_an_py310.py new file mode 100644 index 000000000..57fccec94 --- /dev/null +++ b/tests/test_tutorial/test_query_params_str_validations/test_tutorial006d_an_py310.py @@ -0,0 +1,9 @@ +from fastapi.testclient import TestClient +from docs_src.query_params_str_validations.tutorial006d_an_py310 import app + +client = TestClient(app) + +def test_read_items(): + response = client.get("/items/", params={"q": "None"}) + assert response.status_code == 200 + assert response.json() == {"q": None}