]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:memo: Add note about path declaration order
authorSebastián Ramírez <tiangolo@gmail.com>
Sat, 16 Feb 2019 15:23:42 +0000 (19:23 +0400)
committerSebastián Ramírez <tiangolo@gmail.com>
Sat, 16 Feb 2019 15:23:42 +0000 (19:23 +0400)
docs/src/path_params/tutorial003.py
docs/src/security/tutorial004.py
docs/tutorial/path-params.md

index ba67bb6f9c8b77a0745c11780b346fb806c4098e..5f0aa09234cd609383884a18f519e433ed758b0b 100644 (file)
@@ -1,10 +1,13 @@
-from uuid import UUID
-
 from fastapi import FastAPI
 
 app = FastAPI()
 
 
-@app.get("/items/{item_id}")
-async def read_item(item_id: UUID):
-    return {"item_id": item_id}
+@app.get("/users/me")
+async def read_user_me():
+    return {"user_id": "the current user"}
+
+
+@app.get("/users/{user_id}")
+async def read_user(user_id: str):
+    return {"user_id": user_id}
index 31895c5cb2d0348fd3391169f560fd3f8e8103c8..edbac86f62aeeb4e60281091fb3f0b2427e5ed5b 100644 (file)
@@ -1,7 +1,7 @@
 from datetime import datetime, timedelta
 
 import jwt
-from fastapi import Depends, FastAPI, Security, HTTPException
+from fastapi import Depends, FastAPI, HTTPException, Security
 from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
 from jwt import PyJWTError
 from passlib.context import CryptContext
index 34563b28c06baceb6c2e354aad975fc7573f085b..d795b76e4e20bfe4699dd21dcb2f500751fca717 100644 (file)
@@ -98,6 +98,24 @@ You can use the same type declarations with `str`, `float`, `bool` and many othe
 
 These are explored in the next chapters of the tutorial.
 
+
+## Order matters
+
+When creating *path operations*, you can find situations where you have a fixed path.
+
+Like `/users/me`, let's say that it's to get data about the current user.
+
+And then you can also have a path `/users/{user_id}` to get data about a specific user by some user ID.
+
+Because path operations are evaluated in order, you need to make sure that the path for `/users/me` is declared before the one for `/users/{user_id}`:
+
+```Python hl_lines="6 11"
+{!./src/path_params/tutorial003.py!}
+```
+
+Otherwise, the path for `/users/{user_id}` would match also for `/users/me`, "thinking" that it's receiving a parameter `user_id` with a value of `"me"`.
+
+
 ## Recap
 
 With **FastAPI**, by using short, intuitive and standard Python type declarations, you get: