Skip to content

Commit

Permalink
馃摑 Updated docs for path-params (#1521)
Browse files Browse the repository at this point in the history
* Added response example; URL for quick access; typo fixes

* Added line breaks for readability

* Fix typo on redoc url

* 馃摑 Update format, links, rewordings

Co-authored-by: Sebasti谩n Ram铆rez <tiangolo@gmail.com>
  • Loading branch information
yankeexe and tiangolo committed Jun 13, 2020
1 parent bf58788 commit 748bedd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
21 changes: 15 additions & 6 deletions docs/en/docs/tutorial/path-params.md
Expand Up @@ -85,7 +85,7 @@ And when you open your browser at <a href="http://127.0.0.1:8000/docs" class="ex

And because the generated schema is from the <a href="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md" class="external-link" target="_blank">OpenAPI</a> standard, there are many compatible tools.

Because of this, **FastAPI** itself provides an alternative API documentation (using ReDoc):
Because of this, **FastAPI** itself provides an alternative API documentation (using ReDoc), which you can access at <a href="http://127.0.0.1:8000/redoc" class="external-link" target="_blank">http://127.0.0.1:8000/redoc</a>:

<img src="/img/tutorial/path-params/image02.png">

Expand Down Expand Up @@ -125,7 +125,7 @@ Import `Enum` and create a sub-class that inherits from `str` and from `Enum`.

By inheriting from `str` the API docs will be able to know that the values must be of type `string` and will be able to render correctly.

And create class attributes with fixed values, those fixed values will be the available valid values:
Then create class attributes with fixed values, which will be the available valid values:

```Python hl_lines="1 6 7 8 9"
{!../../../docs_src/path_params/tutorial005.py!}
Expand All @@ -147,7 +147,7 @@ Then create a *path parameter* with a type annotation using the enum class you c

### Check the docs

Because the available values for the *path parameter* are specified, the interactive docs can show them nicely:
Because the available values for the *path parameter* are predefined, the interactive docs can show them nicely:

<img src="/img/tutorial/path-params/image03.png">

Expand All @@ -167,7 +167,7 @@ You can compare it with the *enumeration member* in your created enum `ModelName

You can get the actual value (a `str` in this case) using `model_name.value`, or in general, `your_enum_member.value`:

```Python hl_lines="19"
```Python hl_lines="20"
{!../../../docs_src/path_params/tutorial005.py!}
```

Expand All @@ -178,12 +178,21 @@ You can get the actual value (a `str` in this case) using `model_name.value`, or

You can return *enum members* from your *path operation*, even nested in a JSON body (e.g. a `dict`).

They will be converted to their corresponding values before returning them to the client:
They will be converted to their corresponding values (strings in this case) before returning them to the client:

```Python hl_lines="18 20 21"
```Python hl_lines="18 21 23"
{!../../../docs_src/path_params/tutorial005.py!}
```

In your client you will get a JSON response like:

```JSON
{
"model_name": "alexnet",
"message": "Deep Learning FTW!"
}
```

## Path parameters containing paths

Let's say you have a *path operation* with a path `/files/{file_path}`.
Expand Down
2 changes: 2 additions & 0 deletions docs_src/path_params/tutorial005.py
Expand Up @@ -16,6 +16,8 @@ class ModelName(str, Enum):
async def get_model(model_name: ModelName):
if model_name == ModelName.alexnet:
return {"model_name": model_name, "message": "Deep Learning FTW!"}

if model_name.value == "lenet":
return {"model_name": model_name, "message": "LeCNN all the images"}

return {"model_name": model_name, "message": "Have some residuals"}

1 comment on commit 748bedd

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

馃帀 Published on https://fastapi.tiangolo.com as production
馃殌 Deployed on https://5ee509dbd717522c7fc5b630--fastapi.netlify.app

Please sign in to comment.