Skip to content

Commit

Permalink
Merge branch 'tiangolo:master' into de/docs/reference/security/index.md
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslindemann committed Dec 30, 2023
2 parents 7ec3c1e + 040ad98 commit a2433a5
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 46 deletions.
2 changes: 1 addition & 1 deletion docs/em/docs/advanced/templates.md
Expand Up @@ -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!}
```

Expand Down
14 changes: 11 additions & 3 deletions docs/en/docs/advanced/generate-clients.md
Expand Up @@ -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.

Expand Down
10 changes: 6 additions & 4 deletions docs/en/docs/advanced/templates.md
Expand Up @@ -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.
Expand All @@ -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!}
Expand Down
58 changes: 32 additions & 26 deletions docs/en/docs/contributing.md
Expand Up @@ -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 <a href="https://www.mkdocs.org/" class="external-link" target="_blank">MkDocs</a>.

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:

Expand Down Expand Up @@ -229,6 +204,37 @@ Completion will take effect once you restart the terminal.

</div>

### Docs Structure

The documentation uses <a href="https://www.mkdocs.org/" class="external-link" target="_blank">MkDocs</a>.

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.:
Expand Down
23 changes: 22 additions & 1 deletion docs/en/docs/release-notes.md
Expand Up @@ -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
Expand Down Expand Up @@ -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}")
Expand Down
2 changes: 1 addition & 1 deletion docs_src/dependencies/tutorial008b.py
Expand Up @@ -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}")
Expand Down
2 changes: 1 addition & 1 deletion docs_src/dependencies/tutorial008b_an.py
Expand Up @@ -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}")
Expand Down
2 changes: 1 addition & 1 deletion docs_src/dependencies/tutorial008b_an_py39.py
Expand Up @@ -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}")
Expand Down
29 changes: 29 additions & 0 deletions 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;
});
});
4 changes: 3 additions & 1 deletion docs_src/templates/tutorial001.py
Expand Up @@ -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}
)
2 changes: 1 addition & 1 deletion 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

Expand Down
4 changes: 1 addition & 3 deletions pyproject.toml
Expand Up @@ -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"]

Expand Down
2 changes: 1 addition & 1 deletion tests/test_tutorial/test_dependencies/test_tutorial008b.py
Expand Up @@ -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():
Expand Down
Expand Up @@ -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():
Expand Down
Expand Up @@ -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():
Expand Down

0 comments on commit a2433a5

Please sign in to comment.