Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ASGI root_path for openapi docs #1199

Merged
merged 11 commits into from
Jun 11, 2020
Prev Previous commit
Next Next commit
📝 Update Extending OpenAPI with openapi_prefix parameter
  • Loading branch information
tiangolo committed Jun 11, 2020
commit 8e2f059cce65ab619657440cfcc12098f440aa1b
15 changes: 11 additions & 4 deletions docs/en/docs/advanced/extending-openapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,22 @@ First, write all your **FastAPI** application as normally:

Then, use the same utility function to generate the OpenAPI schema, inside a `custom_openapi()` function:

```Python hl_lines="2 15 16 17 18 19 20"
```Python hl_lines="2 15 16 17 18 19 20 21"
{!../../../docs_src/extending_openapi/tutorial001.py!}
```

!!! tip
The `openapi_prefix` will contain any prefix needed for the generated OpenAPI *path operations*.

FastAPI will automatically use the `root_path` to pass it in the `openapi_prefix`.

But the important thing is that your function should receive that parameter `openapi_prefix` and pass it along.

### Modify the OpenAPI schema

Now you can add the ReDoc extension, adding a custom `x-logo` to the `info` "object" in the OpenAPI schema:

```Python hl_lines="21 22 23"
```Python hl_lines="22 23 24"
{!../../../docs_src/extending_openapi/tutorial001.py!}
```

Expand All @@ -72,15 +79,15 @@ That way, your application won't have to generate the schema every time a user o

It will be generated only once, and then the same cached schema will be used for the next requests.

```Python hl_lines="13 14 24 25"
```Python hl_lines="13 14 25 26"
{!../../../docs_src/extending_openapi/tutorial001.py!}
```

### Override the method

Now you can replace the `.openapi()` method with your new function.

```Python hl_lines="28"
```Python hl_lines="29"
{!../../../docs_src/extending_openapi/tutorial001.py!}
```

Expand Down
1 change: 1 addition & 0 deletions docs_src/extending_openapi/tutorial001.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def custom_openapi(openapi_prefix: str):
version="2.5.0",
description="This is a very custom OpenAPI schema",
routes=app.routes,
openapi_prefix=openapi_prefix,
)
openapi_schema["info"]["x-logo"] = {
"url": "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"
Expand Down