Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions docs/en/docs/tutorial/path-params.md
Original file line number Diff line number Diff line change
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>:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure that we can assume the existence of this URL.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it was already merged, nvm

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, I was thinking that, but the example right above for Swagger UI assumes it's running (from the previous examples), so I guess it makes sense to have the link here too.


<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
Original file line number Diff line number Diff line change
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"}