diff --git a/docs/em/docs/advanced/templates.md b/docs/em/docs/advanced/templates.md index 1fb57725af175..0a73a4f47e811 100644 --- a/docs/em/docs/advanced/templates.md +++ b/docs/em/docs/advanced/templates.md @@ -27,7 +27,7 @@ $ pip install jinja2 * πŸ“£ `Request` πŸ”’ *➑ πŸ› οΈ* πŸ‘ˆ πŸ”œ πŸ“¨ πŸ“„. * βš™οΈ `templates` πŸ‘† ✍ ✍ & πŸ“¨ `TemplateResponse`, πŸšΆβ€β™€οΈ `request` 1️⃣ πŸ”‘-πŸ’² πŸ‘« Jinja2️⃣ "πŸ”‘". -```Python hl_lines="4 11 15-16" +```Python hl_lines="4 11 15-18" {!../../../docs_src/templates/tutorial001.py!} ``` diff --git a/docs/en/docs/advanced/generate-clients.md b/docs/en/docs/advanced/generate-clients.md index e8d771f7123f4..3a810baee1957 100644 --- a/docs/en/docs/advanced/generate-clients.md +++ b/docs/en/docs/advanced/generate-clients.md @@ -229,9 +229,17 @@ But for the generated client we could **modify** the OpenAPI operation IDs right We could download the OpenAPI JSON to a file `openapi.json` and then we could **remove that prefixed tag** with a script like this: -```Python -{!../../../docs_src/generate_clients/tutorial004.py!} -``` +=== "Python" + + ```Python + {!> ../../../docs_src/generate_clients/tutorial004.py!} + ``` + +=== "Node.js" + + ```Python + {!> ../../../docs_src/generate_clients/tutorial004.js!} + ``` With that, the operation IDs would be renamed from things like `items-get_items` to just `get_items`, that way the client generator can generate simpler method names. diff --git a/docs/en/docs/advanced/templates.md b/docs/en/docs/advanced/templates.md index 38618aeeb09cd..583abda7fb0e4 100644 --- a/docs/en/docs/advanced/templates.md +++ b/docs/en/docs/advanced/templates.md @@ -25,14 +25,16 @@ $ pip install jinja2 * Import `Jinja2Templates`. * Create a `templates` object that you can re-use later. * Declare a `Request` parameter in the *path operation* that will return a template. -* Use the `templates` you created to render and return a `TemplateResponse`, passing the `request` as one of the key-value pairs in the Jinja2 "context". +* Use the `templates` you created to render and return a `TemplateResponse`, pass the name of the template, the request object, and a "context" dictionary with key-value pairs to be used inside of the Jinja2 template. -```Python hl_lines="4 11 15-16" +```Python hl_lines="4 11 15-18" {!../../../docs_src/templates/tutorial001.py!} ``` !!! note - Notice that you have to pass the `request` as part of the key-value pairs in the context for Jinja2. So, you also have to declare it in your *path operation*. + Before FastAPI 0.108.0, Starlette 0.29.0, the `name` was the first parameter. + + Also, before that, in previous versions, the `request` object was passed as part of the key-value pairs in the context for Jinja2. !!! tip By declaring `response_class=HTMLResponse` the docs UI will be able to know that the response will be HTML. @@ -58,7 +60,7 @@ It will show the `id` taken from the "context" `dict` you passed: ## Templates and static files -And you can also use `url_for()` inside of the template, and use it, for example, with the `StaticFiles` you mounted. +You can also use `url_for()` inside of the template, and use it, for example, with the `StaticFiles` you mounted. ```jinja hl_lines="4" {!../../../docs_src/templates/templates/item.html!} diff --git a/docs/en/docs/contributing.md b/docs/en/docs/contributing.md index cfdb607d772e0..35bc1c50197d4 100644 --- a/docs/en/docs/contributing.md +++ b/docs/en/docs/contributing.md @@ -150,32 +150,7 @@ For it to sort them correctly, you need to have FastAPI installed locally in you First, make sure you set up your environment as described above, that will install all the requirements. -The documentation uses MkDocs. - -And there are extra tools/scripts in place to handle translations in `./scripts/docs.py`. - -!!! tip - You don't need to see the code in `./scripts/docs.py`, you just use it in the command line. - -All the documentation is in Markdown format in the directory `./docs/en/`. - -Many of the tutorials have blocks of code. - -In most of the cases, these blocks of code are actual complete applications that can be run as is. - -In fact, those blocks of code are not written inside the Markdown, they are Python files in the `./docs_src/` directory. - -And those Python files are included/injected in the documentation when generating the site. - -### Docs for tests - -Most of the tests actually run against the example source files in the documentation. - -This helps making sure that: - -* The documentation is up to date. -* The documentation examples can be run as is. -* Most of the features are covered by the documentation, ensured by test coverage. +### Docs live During local development, there is a script that builds the site and checks for any changes, live-reloading: @@ -229,6 +204,37 @@ Completion will take effect once you restart the terminal. +### Docs Structure + +The documentation uses MkDocs. + +And there are extra tools/scripts in place to handle translations in `./scripts/docs.py`. + +!!! tip + You don't need to see the code in `./scripts/docs.py`, you just use it in the command line. + +All the documentation is in Markdown format in the directory `./docs/en/`. + +Many of the tutorials have blocks of code. + +In most of the cases, these blocks of code are actual complete applications that can be run as is. + +In fact, those blocks of code are not written inside the Markdown, they are Python files in the `./docs_src/` directory. + +And those Python files are included/injected in the documentation when generating the site. + +### Docs for tests + +Most of the tests actually run against the example source files in the documentation. + +This helps making sure that: + +* The documentation is up to date. +* The documentation examples can be run as is. +* Most of the features are covered by the documentation, ensured by test coverage. + + + ### Apps and docs at the same time If you run the examples with, e.g.: diff --git a/docs/en/docs/release-notes.md b/docs/en/docs/release-notes.md index 0c118e7c9edc0..bb96bce6684b4 100644 --- a/docs/en/docs/release-notes.md +++ b/docs/en/docs/release-notes.md @@ -7,6 +7,27 @@ hide: ## Latest Changes +### Docs + +* ✏️ Fix typo in dependencies with yield source examples. PR [#10847](https://github.com/tiangolo/fastapi/pull/10847) by [@tiangolo](https://github.com/tiangolo). + +## 0.108.0 + +### Upgrades + +* ⬆️ Upgrade Starlette to `>=0.29.0,<0.33.0`, update docs and usage of templates with new Starlette arguments. PR [#10846](https://github.com/tiangolo/fastapi/pull/10846) by [@tiangolo](https://github.com/tiangolo). + +## 0.107.0 + +### Upgrades + +* ⬆️ Upgrade Starlette to 0.28.0. PR [#9636](https://github.com/tiangolo/fastapi/pull/9636) by [@adriangb](https://github.com/adriangb). + +### Docs + +* πŸ“ Add docs: Node.js script alternative to update OpenAPI for generated clients. PR [#10845](https://github.com/tiangolo/fastapi/pull/10845) by [@alejsdev](https://github.com/alejsdev). +* πŸ“ Restructure Docs section in Contributing page. PR [#10844](https://github.com/tiangolo/fastapi/pull/10844) by [@alejsdev](https://github.com/alejsdev). + ## 0.106.0 ### Breaking Changes @@ -42,7 +63,7 @@ def get_username(): try: yield "Rick" except OwnerError as e: - raise HTTPException(status_code=400, detail=f"Onwer error: {e}") + raise HTTPException(status_code=400, detail=f"Owner error: {e}") @app.get("/items/{item_id}") diff --git a/docs_src/dependencies/tutorial008b.py b/docs_src/dependencies/tutorial008b.py index 4a1a70dcfcda9..163e96600f9b3 100644 --- a/docs_src/dependencies/tutorial008b.py +++ b/docs_src/dependencies/tutorial008b.py @@ -17,7 +17,7 @@ def get_username(): try: yield "Rick" except OwnerError as e: - raise HTTPException(status_code=400, detail=f"Onwer error: {e}") + raise HTTPException(status_code=400, detail=f"Owner error: {e}") @app.get("/items/{item_id}") diff --git a/docs_src/dependencies/tutorial008b_an.py b/docs_src/dependencies/tutorial008b_an.py index 3a0f1399a98f4..84d8f12c14887 100644 --- a/docs_src/dependencies/tutorial008b_an.py +++ b/docs_src/dependencies/tutorial008b_an.py @@ -18,7 +18,7 @@ def get_username(): try: yield "Rick" except OwnerError as e: - raise HTTPException(status_code=400, detail=f"Onwer error: {e}") + raise HTTPException(status_code=400, detail=f"Owner error: {e}") @app.get("/items/{item_id}") diff --git a/docs_src/dependencies/tutorial008b_an_py39.py b/docs_src/dependencies/tutorial008b_an_py39.py index 30c9cdc699c2e..3b8434c816711 100644 --- a/docs_src/dependencies/tutorial008b_an_py39.py +++ b/docs_src/dependencies/tutorial008b_an_py39.py @@ -19,7 +19,7 @@ def get_username(): try: yield "Rick" except OwnerError as e: - raise HTTPException(status_code=400, detail=f"Onwer error: {e}") + raise HTTPException(status_code=400, detail=f"Owner error: {e}") @app.get("/items/{item_id}") diff --git a/docs_src/generate_clients/tutorial004.js b/docs_src/generate_clients/tutorial004.js new file mode 100644 index 0000000000000..18dc38267bcde --- /dev/null +++ b/docs_src/generate_clients/tutorial004.js @@ -0,0 +1,29 @@ +import * as fs from "fs"; + +const filePath = "./openapi.json"; + +fs.readFile(filePath, (err, data) => { + const openapiContent = JSON.parse(data); + if (err) throw err; + + const paths = openapiContent.paths; + + Object.keys(paths).forEach((pathKey) => { + const pathData = paths[pathKey]; + Object.keys(pathData).forEach((method) => { + const operation = pathData[method]; + if (operation.tags && operation.tags.length > 0) { + const tag = operation.tags[0]; + const operationId = operation.operationId; + const toRemove = `${tag}-`; + if (operationId.startsWith(toRemove)) { + const newOperationId = operationId.substring(toRemove.length); + operation.operationId = newOperationId; + } + } + }); + }); + fs.writeFile(filePath, JSON.stringify(openapiContent, null, 2), (err) => { + if (err) throw err; + }); +}); diff --git a/docs_src/templates/tutorial001.py b/docs_src/templates/tutorial001.py index 245e7110b195d..81ccc8d4d0b3f 100644 --- a/docs_src/templates/tutorial001.py +++ b/docs_src/templates/tutorial001.py @@ -13,4 +13,6 @@ @app.get("/items/{id}", response_class=HTMLResponse) async def read_item(request: Request, id: str): - return templates.TemplateResponse("item.html", {"request": request, "id": id}) + return templates.TemplateResponse( + request=request, name="item.html", context={"id": id} + ) diff --git a/fastapi/__init__.py b/fastapi/__init__.py index 4348bd98e4dad..02ac83b5e4aee 100644 --- a/fastapi/__init__.py +++ b/fastapi/__init__.py @@ -1,6 +1,6 @@ """FastAPI framework, high performance, easy to learn, fast to code, ready for production""" -__version__ = "0.106.0" +__version__ = "0.108.0" from starlette import status as status diff --git a/pyproject.toml b/pyproject.toml index fa072e59523da..38728d99e945b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,11 +40,9 @@ classifiers = [ "Topic :: Internet :: WWW/HTTP", ] dependencies = [ - "starlette>=0.27.0,<0.28.0", + "starlette>=0.29.0,<0.33.0", "pydantic>=1.7.4,!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,!=2.1.0,<3.0.0", "typing-extensions>=4.8.0", - # TODO: remove this pin after upgrading Starlette 0.31.1 - "anyio>=3.7.1,<4.0.0", ] dynamic = ["version"] diff --git a/tests/test_tutorial/test_dependencies/test_tutorial008b.py b/tests/test_tutorial/test_dependencies/test_tutorial008b.py index ed4f4aaca8cd3..86acba9e4ff95 100644 --- a/tests/test_tutorial/test_dependencies/test_tutorial008b.py +++ b/tests/test_tutorial/test_dependencies/test_tutorial008b.py @@ -14,7 +14,7 @@ def test_get_no_item(): def test_owner_error(): response = client.get("/items/plumbus") assert response.status_code == 400, response.text - assert response.json() == {"detail": "Onwer error: Rick"} + assert response.json() == {"detail": "Owner error: Rick"} def test_get_item(): diff --git a/tests/test_tutorial/test_dependencies/test_tutorial008b_an.py b/tests/test_tutorial/test_dependencies/test_tutorial008b_an.py index aa76ad6afc940..7f51fc52a5133 100644 --- a/tests/test_tutorial/test_dependencies/test_tutorial008b_an.py +++ b/tests/test_tutorial/test_dependencies/test_tutorial008b_an.py @@ -14,7 +14,7 @@ def test_get_no_item(): def test_owner_error(): response = client.get("/items/plumbus") assert response.status_code == 400, response.text - assert response.json() == {"detail": "Onwer error: Rick"} + assert response.json() == {"detail": "Owner error: Rick"} def test_get_item(): diff --git a/tests/test_tutorial/test_dependencies/test_tutorial008b_an_py39.py b/tests/test_tutorial/test_dependencies/test_tutorial008b_an_py39.py index aa76ad6afc940..7f51fc52a5133 100644 --- a/tests/test_tutorial/test_dependencies/test_tutorial008b_an_py39.py +++ b/tests/test_tutorial/test_dependencies/test_tutorial008b_an_py39.py @@ -14,7 +14,7 @@ def test_get_no_item(): def test_owner_error(): response = client.get("/items/plumbus") assert response.status_code == 400, response.text - assert response.json() == {"detail": "Onwer error: Rick"} + assert response.json() == {"detail": "Owner error: Rick"} def test_get_item():