Features
* ✨ Add support for omitting `...` as default value when declaring required parameters with:
* `Path()`
* `Query()`
* `Header()`
* `Cookie()`
* `Body()`
* `Form()`
* `File()`
New docs at [Tutorial - Query Parameters and String Validations - Make it required](https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#make-it-required). PR [4906](https://github.com/tiangolo/fastapi/pull/4906) by [tiangolo](https://github.com/tiangolo).
Up to now, declaring a required parameter while adding additional validation or metadata needed using `...` (Ellipsis).
For example:
Python
from fastapi import Cookie, FastAPI, Header, Path, Query
app = FastAPI()
app.get("/items/{item_id}")
def main(
item_id: int = Path(default=..., gt=0),
query: str = Query(default=..., max_length=10),
session: str = Cookie(default=..., min_length=3),
x_trace: str = Header(default=..., title="Tracing header"),
):
return {"message": "Hello World"}
...all these parameters are required because the default value is `...` (Ellipsis).
But now it's possible and supported to just omit the default value, as would be done with Pydantic fields, and the parameters would still be required.
✨ For example, this is now supported:
Python
from fastapi import Cookie, FastAPI, Header, Path, Query
app = FastAPI()
app.get("/items/{item_id}")
def main(
item_id: int = Path(gt=0),
query: str = Query(max_length=10),
session: str = Cookie(min_length=3),
x_trace: str = Header(title="Tracing header"),
):
return {"message": "Hello World"}
To declare parameters as optional (not required), you can set a default value as always, for example using `None`:
Python
from typing import Union
from fastapi import Cookie, FastAPI, Header, Path, Query
app = FastAPI()
app.get("/items/{item_id}")
def main(
item_id: int = Path(gt=0),
query: Union[str, None] = Query(default=None, max_length=10),
session: Union[str, None] = Cookie(default=None, min_length=3),
x_trace: Union[str, None] = Header(default=None, title="Tracing header"),
):
return {"message": "Hello World"}
Docs
* 📝 Add docs recommending `Union` over `Optional` and migrate source examples. New docs at [Python Types Intro - Using `Union` or `Optional`](https://fastapi.tiangolo.com/python-types/#using-union-or-optional). PR [4908](https://github.com/tiangolo/fastapi/pull/4908) by [tiangolo](https://github.com/tiangolo).
* 🎨 Fix default value as set in tutorial for Path Operations Advanced Configurations. PR [4899](https://github.com/tiangolo/fastapi/pull/4899) by [tiangolo](https://github.com/tiangolo).
* 📝 Add documentation for redefined path operations. PR [4864](https://github.com/tiangolo/fastapi/pull/4864) by [madkinsz](https://github.com/madkinsz).
* 📝 Updates links for Celery documentation. PR [4736](https://github.com/tiangolo/fastapi/pull/4736) by [sammyzord](https://github.com/sammyzord).
* ✏ Fix example code with sets in tutorial for body nested models. PR [3030](https://github.com/tiangolo/fastapi/pull/3030) by [hitrust](https://github.com/hitrust).
* ✏ Fix links to Pydantic docs. PR [4670](https://github.com/tiangolo/fastapi/pull/4670) by [kinuax](https://github.com/kinuax).
* 📝 Update docs about Swagger UI self-hosting with newer source links. PR [4813](https://github.com/tiangolo/fastapi/pull/4813) by [Kastakin](https://github.com/Kastakin).
* 📝 Add link to external article: Building the Poll App From Django Tutorial With FastAPI And React. PR [4778](https://github.com/tiangolo/fastapi/pull/4778) by [jbrocher](https://github.com/jbrocher).
* 📝 Add OpenAPI warning to "Body - Fields" docs with extra schema extensions. PR [4846](https://github.com/tiangolo/fastapi/pull/4846) by [ml-evs](https://github.com/ml-evs).
Translations
* 🌐 Fix code examples in Japanese translation for `docs/ja/docs/tutorial/testing.md`. PR [4623](https://github.com/tiangolo/fastapi/pull/4623) by [hirotoKirimaru](https://github.com/hirotoKirimaru).
Internal
* ♻ Refactor dict value extraction to minimize key lookups `fastapi/utils.py`. PR [3139](https://github.com/tiangolo/fastapi/pull/3139) by [ShahriyarR](https://github.com/ShahriyarR).
* ✅ Add tests for required nonable parameters and body fields. PR [4907](https://github.com/tiangolo/fastapi/pull/4907) by [tiangolo](https://github.com/tiangolo).
* 👷 Fix installing Material for MkDocs Insiders in CI. PR [4897](https://github.com/tiangolo/fastapi/pull/4897) by [tiangolo](https://github.com/tiangolo).
* 👷 Add pre-commit CI instead of custom GitHub Action. PR [4896](https://github.com/tiangolo/fastapi/pull/4896) by [tiangolo](https://github.com/tiangolo).
* 👷 Add pre-commit GitHub Action workflow. PR [4895](https://github.com/tiangolo/fastapi/pull/4895) by [tiangolo](https://github.com/tiangolo).
* 📝 Add dark mode auto switch to docs based on OS preference. PR [4869](https://github.com/tiangolo/fastapi/pull/4869) by [ComicShrimp](https://github.com/ComicShrimp).
* 🔥 Remove un-used old pending tests, already covered in other places. PR [4891](https://github.com/tiangolo/fastapi/pull/4891) by [tiangolo](https://github.com/tiangolo).
* 🔧 Add Python formatting hooks to pre-commit. PR [4890](https://github.com/tiangolo/fastapi/pull/4890) by [tiangolo](https://github.com/tiangolo).
* 🔧 Add pre-commit with first config and first formatting pass. PR [4888](https://github.com/tiangolo/fastapi/pull/4888) by [tiangolo](https://github.com/tiangolo).
* 👷 Disable CI installing Material for MkDocs in forks. PR [4410](https://github.com/tiangolo/fastapi/pull/4410) by [dolfinus](https://github.com/dolfinus).