mirror of https://github.com/tiangolo/fastapi.git
✨ Add support for not needing `...` as default value in required Query(), Path(), Header(), etc. (#4906)
* ✨ Do not require default value in Query(), Path(), Header(), etc * 📝 Update source examples for docs with default and required values * ✅ Update tests with new default values and not required Ellipsis * 📝 Update docs for Query params and update info about default value, required, Ellipsis
This commit is contained in:
parent
31690dda2c
commit
9262fa8362
|
|
@ -16,12 +16,12 @@ Let's take this application as example:
|
|||
{!> ../../../docs_src/query_params_str_validations/tutorial001_py310.py!}
|
||||
```
|
||||
|
||||
The query parameter `q` is of type `Optional[str]` (or `str | None` in Python 3.10), that means that it's of type `str` but could also be `None`, and indeed, the default value is `None`, so FastAPI will know it's not required.
|
||||
The query parameter `q` is of type `Union[str, None]` (or `str | None` in Python 3.10), that means that it's of type `str` but could also be `None`, and indeed, the default value is `None`, so FastAPI will know it's not required.
|
||||
|
||||
!!! note
|
||||
FastAPI will know that the value of `q` is not required because of the default value `= None`.
|
||||
|
||||
The `Optional` in `Optional[str]` is not used by FastAPI, but will allow your editor to give you better support and detect errors.
|
||||
The `Union` in `Union[str, None]` will allow your editor to give you better support and detect errors.
|
||||
|
||||
## Additional validation
|
||||
|
||||
|
|
@ -59,24 +59,24 @@ And now use it as the default value of your parameter, setting the parameter `ma
|
|||
{!> ../../../docs_src/query_params_str_validations/tutorial002_py310.py!}
|
||||
```
|
||||
|
||||
As we have to replace the default value `None` with `Query(None)`, the first parameter to `Query` serves the same purpose of defining that default value.
|
||||
As we have to replace the default value `None` in the function with `Query()`, we can now set the default value with the parameter `Query(default=None)`, it serves the same purpose of defining that default value.
|
||||
|
||||
So:
|
||||
|
||||
```Python
|
||||
q: Optional[str] = Query(None)
|
||||
q: Union[str, None] = Query(default=None)
|
||||
```
|
||||
|
||||
...makes the parameter optional, the same as:
|
||||
|
||||
```Python
|
||||
q: Optional[str] = None
|
||||
q: Union[str, None] = None
|
||||
```
|
||||
|
||||
And in Python 3.10 and above:
|
||||
|
||||
```Python
|
||||
q: str | None = Query(None)
|
||||
q: str | None = Query(default=None)
|
||||
```
|
||||
|
||||
...makes the parameter optional, the same as:
|
||||
|
|
@ -97,17 +97,17 @@ But it declares it explicitly as being a query parameter.
|
|||
or the:
|
||||
|
||||
```Python
|
||||
= Query(None)
|
||||
= Query(default=None)
|
||||
```
|
||||
|
||||
as it will use that `None` as the default value, and that way make the parameter **not required**.
|
||||
|
||||
The `Optional` part allows your editor to provide better support, but it is not what tells FastAPI that this parameter is not required.
|
||||
The `Union[str, None]` part allows your editor to provide better support, but it is not what tells FastAPI that this parameter is not required.
|
||||
|
||||
Then, we can pass more parameters to `Query`. In this case, the `max_length` parameter that applies to strings:
|
||||
|
||||
```Python
|
||||
q: str = Query(None, max_length=50)
|
||||
q: Union[str, None] = Query(default=None, max_length=50)
|
||||
```
|
||||
|
||||
This will validate the data, show a clear error when the data is not valid, and document the parameter in the OpenAPI schema *path operation*.
|
||||
|
|
@ -118,7 +118,7 @@ You can also add a parameter `min_length`:
|
|||
|
||||
=== "Python 3.6 and above"
|
||||
|
||||
```Python hl_lines="9"
|
||||
```Python hl_lines="10"
|
||||
{!> ../../../docs_src/query_params_str_validations/tutorial003.py!}
|
||||
```
|
||||
|
||||
|
|
@ -134,13 +134,13 @@ You can define a <abbr title="A regular expression, regex or regexp is a sequenc
|
|||
|
||||
=== "Python 3.6 and above"
|
||||
|
||||
```Python hl_lines="10"
|
||||
```Python hl_lines="11"
|
||||
{!> ../../../docs_src/query_params_str_validations/tutorial004.py!}
|
||||
```
|
||||
|
||||
=== "Python 3.10 and above"
|
||||
|
||||
```Python hl_lines="8"
|
||||
```Python hl_lines="9"
|
||||
{!> ../../../docs_src/query_params_str_validations/tutorial004_py310.py!}
|
||||
```
|
||||
|
||||
|
|
@ -156,7 +156,7 @@ But whenever you need them and go and learn them, know that you can already use
|
|||
|
||||
## Default values
|
||||
|
||||
The same way that you can pass `None` as the first argument to be used as the default value, you can pass other values.
|
||||
The same way that you can pass `None` as the value for the `default` parameter, you can pass other values.
|
||||
|
||||
Let's say that you want to declare the `q` query parameter to have a `min_length` of `3`, and to have a default value of `"fixedquery"`:
|
||||
|
||||
|
|
@ -178,26 +178,68 @@ q: str
|
|||
instead of:
|
||||
|
||||
```Python
|
||||
q: Optional[str] = None
|
||||
q: Union[str, None] = None
|
||||
```
|
||||
|
||||
But we are now declaring it with `Query`, for example like:
|
||||
|
||||
```Python
|
||||
q: Optional[str] = Query(None, min_length=3)
|
||||
q: Union[str, None] = Query(default=None, min_length=3)
|
||||
```
|
||||
|
||||
So, when you need to declare a value as required while using `Query`, you can use `...` as the first argument:
|
||||
So, when you need to declare a value as required while using `Query`, you can simply not declare a default value:
|
||||
|
||||
```Python hl_lines="7"
|
||||
{!../../../docs_src/query_params_str_validations/tutorial006.py!}
|
||||
```
|
||||
|
||||
### Required with Ellipsis (`...`)
|
||||
|
||||
There's an alternative way to explicitly declare that a value is required. You can set the `default` parameter to the literal value `...`:
|
||||
|
||||
```Python hl_lines="7"
|
||||
{!../../../docs_src/query_params_str_validations/tutorial006b.py!}
|
||||
```
|
||||
|
||||
!!! info
|
||||
If you hadn't seen that `...` before: it is a special single value, it is <a href="https://docs.python.org/3/library/constants.html#Ellipsis" class="external-link" target="_blank">part of Python and is called "Ellipsis"</a>.
|
||||
|
||||
It is used by Pydantic and FastAPI to explicitly declare that a value is required.
|
||||
|
||||
This will let **FastAPI** know that this parameter is required.
|
||||
|
||||
### Required with `None`
|
||||
|
||||
You can declare that a parameter can accept `None`, but that it's still required. This would force clients to send a value, even if the value is `None`.
|
||||
|
||||
To do that, you can declare that `None` is a valid type but still use `default=...`:
|
||||
|
||||
=== "Python 3.6 and above"
|
||||
|
||||
```Python hl_lines="8"
|
||||
{!> ../../../docs_src/query_params_str_validations/tutorial006c.py!}
|
||||
```
|
||||
|
||||
=== "Python 3.10 and above"
|
||||
|
||||
```Python hl_lines="7"
|
||||
{!> ../../../docs_src/query_params_str_validations/tutorial006c_py310.py!}
|
||||
```
|
||||
|
||||
!!! tip
|
||||
Pydantic, which is what powers all the data validation and serialization in FastAPI, has a special behavior when you use `Optional` or `Union[Something, None]` without a default value, you can read more about it in the Pydantic docs about <a href="https://pydantic-docs.helpmanual.io/usage/models/#required-optional-fields" class="external-link" target="_blank">Required Optional fields</a>.
|
||||
|
||||
### Use Pydantic's `Required` instead of Ellipsis (`...`)
|
||||
|
||||
If you feel uncomfortable using `...`, you can also import and use `Required` from Pydantic:
|
||||
|
||||
```Python hl_lines="2 8"
|
||||
{!../../../docs_src/query_params_str_validations/tutorial006d.py!}
|
||||
```
|
||||
|
||||
!!! tip
|
||||
Remember that in most of the cases, when something is required, you can simply omit the `default` parameter, so you normally don't have to use `...` nor `Required`.
|
||||
|
||||
## Query parameter list / multiple values
|
||||
|
||||
When you define a query parameter explicitly with `Query` you can also declare it to receive a list of values, or said in other way, to receive multiple values.
|
||||
|
|
@ -315,7 +357,7 @@ You can add a `title`:
|
|||
|
||||
=== "Python 3.10 and above"
|
||||
|
||||
```Python hl_lines="7"
|
||||
```Python hl_lines="8"
|
||||
{!> ../../../docs_src/query_params_str_validations/tutorial007_py310.py!}
|
||||
```
|
||||
|
||||
|
|
@ -399,7 +441,7 @@ To exclude a query parameter from the generated OpenAPI schema (and thus, from t
|
|||
|
||||
=== "Python 3.10 and above"
|
||||
|
||||
```Python hl_lines="7"
|
||||
```Python hl_lines="8"
|
||||
{!> ../../../docs_src/query_params_str_validations/tutorial014_py310.py!}
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import Body, FastAPI, status
|
||||
from fastapi.responses import JSONResponse
|
||||
|
|
@ -10,7 +10,9 @@ items = {"foo": {"name": "Fighters", "size": 6}, "bar": {"name": "Tenders", "siz
|
|||
|
||||
@app.put("/items/{item_id}")
|
||||
async def upsert_item(
|
||||
item_id: str, name: Optional[str] = Body(None), size: Optional[int] = Body(None)
|
||||
item_id: str,
|
||||
name: Union[str, None] = Body(default=None),
|
||||
size: Union[int, None] = Body(default=None),
|
||||
):
|
||||
if item_id in items:
|
||||
item = items[item_id]
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Header, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
|
@ -16,11 +16,11 @@ app = FastAPI()
|
|||
class Item(BaseModel):
|
||||
id: str
|
||||
title: str
|
||||
description: Optional[str] = None
|
||||
description: Union[str, None] = None
|
||||
|
||||
|
||||
@app.get("/items/{item_id}", response_model=Item)
|
||||
async def read_main(item_id: str, x_token: str = Header(...)):
|
||||
async def read_main(item_id: str, x_token: str = Header()):
|
||||
if x_token != fake_secret_token:
|
||||
raise HTTPException(status_code=400, detail="Invalid X-Token header")
|
||||
if item_id not in fake_db:
|
||||
|
|
@ -29,7 +29,7 @@ async def read_main(item_id: str, x_token: str = Header(...)):
|
|||
|
||||
|
||||
@app.post("/items/", response_model=Item)
|
||||
async def create_item(item: Item, x_token: str = Header(...)):
|
||||
async def create_item(item: Item, x_token: str = Header()):
|
||||
if x_token != fake_secret_token:
|
||||
raise HTTPException(status_code=400, detail="Invalid X-Token header")
|
||||
if item.id in fake_db:
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ class Item(BaseModel):
|
|||
|
||||
|
||||
@app.get("/items/{item_id}", response_model=Item)
|
||||
async def read_main(item_id: str, x_token: str = Header(...)):
|
||||
async def read_main(item_id: str, x_token: str = Header()):
|
||||
if x_token != fake_secret_token:
|
||||
raise HTTPException(status_code=400, detail="Invalid X-Token header")
|
||||
if item_id not in fake_db:
|
||||
|
|
@ -27,7 +27,7 @@ async def read_main(item_id: str, x_token: str = Header(...)):
|
|||
|
||||
|
||||
@app.post("/items/", response_model=Item)
|
||||
async def create_item(item: Item, x_token: str = Header(...)):
|
||||
async def create_item(item: Item, x_token: str = Header()):
|
||||
if x_token != fake_secret_token:
|
||||
raise HTTPException(status_code=400, detail="Invalid X-Token header")
|
||||
if item.id in fake_db:
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from fastapi import Header, HTTPException
|
||||
|
||||
|
||||
async def get_token_header(x_token: str = Header(...)):
|
||||
async def get_token_header(x_token: str = Header()):
|
||||
if x_token != "fake-super-secret-token":
|
||||
raise HTTPException(status_code=400, detail="X-Token header invalid")
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import Body, FastAPI
|
||||
from pydantic import BaseModel, Field
|
||||
|
|
@ -8,14 +8,14 @@ app = FastAPI()
|
|||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = Field(
|
||||
None, title="The description of the item", max_length=300
|
||||
description: Union[str, None] = Field(
|
||||
default=None, title="The description of the item", max_length=300
|
||||
)
|
||||
price: float = Field(..., gt=0, description="The price must be greater than zero")
|
||||
tax: Optional[float] = None
|
||||
price: float = Field(gt=0, description="The price must be greater than zero")
|
||||
tax: Union[float, None] = None
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
async def update_item(item_id: int, item: Item = Body(..., embed=True)):
|
||||
async def update_item(item_id: int, item: Item = Body(embed=True)):
|
||||
results = {"item_id": item_id, "item": item}
|
||||
return results
|
||||
|
|
|
|||
|
|
@ -7,13 +7,13 @@ app = FastAPI()
|
|||
class Item(BaseModel):
|
||||
name: str
|
||||
description: str | None = Field(
|
||||
None, title="The description of the item", max_length=300
|
||||
default=None, title="The description of the item", max_length=300
|
||||
)
|
||||
price: float = Field(..., gt=0, description="The price must be greater than zero")
|
||||
price: float = Field(gt=0, description="The price must be greater than zero")
|
||||
tax: float | None = None
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
async def update_item(item_id: int, item: Item = Body(..., embed=True)):
|
||||
async def update_item(item_id: int, item: Item = Body(embed=True)):
|
||||
results = {"item_id": item_id, "item": item}
|
||||
return results
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Path
|
||||
from pydantic import BaseModel
|
||||
|
|
@ -8,17 +8,17 @@ app = FastAPI()
|
|||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
description: Union[str, None] = None
|
||||
price: float
|
||||
tax: Optional[float] = None
|
||||
tax: Union[float, None] = None
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
async def update_item(
|
||||
*,
|
||||
item_id: int = Path(..., title="The ID of the item to get", ge=0, le=1000),
|
||||
q: Optional[str] = None,
|
||||
item: Optional[Item] = None,
|
||||
item_id: int = Path(title="The ID of the item to get", ge=0, le=1000),
|
||||
q: Union[str, None] = None,
|
||||
item: Union[Item, None] = None,
|
||||
):
|
||||
results = {"item_id": item_id}
|
||||
if q:
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ class Item(BaseModel):
|
|||
@app.put("/items/{item_id}")
|
||||
async def update_item(
|
||||
*,
|
||||
item_id: int = Path(..., title="The ID of the item to get", ge=0, le=1000),
|
||||
item_id: int = Path(title="The ID of the item to get", ge=0, le=1000),
|
||||
q: str | None = None,
|
||||
item: Item | None = None,
|
||||
):
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import Body, FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
|
@ -8,19 +8,17 @@ app = FastAPI()
|
|||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
description: Union[str, None] = None
|
||||
price: float
|
||||
tax: Optional[float] = None
|
||||
tax: Union[float, None] = None
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
username: str
|
||||
full_name: Optional[str] = None
|
||||
full_name: Union[str, None] = None
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
async def update_item(
|
||||
item_id: int, item: Item, user: User, importance: int = Body(...)
|
||||
):
|
||||
async def update_item(item_id: int, item: Item, user: User, importance: int = Body()):
|
||||
results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
|
||||
return results
|
||||
|
|
|
|||
|
|
@ -17,8 +17,6 @@ class User(BaseModel):
|
|||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
async def update_item(
|
||||
item_id: int, item: Item, user: User, importance: int = Body(...)
|
||||
):
|
||||
async def update_item(item_id: int, item: Item, user: User, importance: int = Body()):
|
||||
results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
|
||||
return results
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import Body, FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
|
@ -8,14 +8,14 @@ app = FastAPI()
|
|||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
description: Union[str, None] = None
|
||||
price: float
|
||||
tax: Optional[float] = None
|
||||
tax: Union[float, None] = None
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
username: str
|
||||
full_name: Optional[str] = None
|
||||
full_name: Union[str, None] = None
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
|
|
@ -24,8 +24,8 @@ async def update_item(
|
|||
item_id: int,
|
||||
item: Item,
|
||||
user: User,
|
||||
importance: int = Body(..., gt=0),
|
||||
q: Optional[str] = None
|
||||
importance: int = Body(gt=0),
|
||||
q: Union[str, None] = None
|
||||
):
|
||||
results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
|
||||
if q:
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ async def update_item(
|
|||
item_id: int,
|
||||
item: Item,
|
||||
user: User,
|
||||
importance: int = Body(..., gt=0),
|
||||
importance: int = Body(gt=0),
|
||||
q: str | None = None
|
||||
):
|
||||
results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import Body, FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
|
@ -8,12 +8,12 @@ app = FastAPI()
|
|||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
description: Union[str, None] = None
|
||||
price: float
|
||||
tax: Optional[float] = None
|
||||
tax: Union[float, None] = None
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
async def update_item(item_id: int, item: Item = Body(..., embed=True)):
|
||||
async def update_item(item_id: int, item: Item = Body(embed=True)):
|
||||
results = {"item_id": item_id, "item": item}
|
||||
return results
|
||||
|
|
|
|||
|
|
@ -12,6 +12,6 @@ class Item(BaseModel):
|
|||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
async def update_item(item_id: int, item: Item = Body(..., embed=True)):
|
||||
async def update_item(item_id: int, item: Item = Body(embed=True)):
|
||||
results = {"item_id": item_id, "item": item}
|
||||
return results
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import Cookie, FastAPI
|
||||
|
||||
|
|
@ -6,5 +6,5 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(ads_id: Optional[str] = Cookie(None)):
|
||||
async def read_items(ads_id: Union[str, None] = Cookie(default=None)):
|
||||
return {"ads_id": ads_id}
|
||||
|
|
|
|||
|
|
@ -4,5 +4,5 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(ads_id: str | None = Cookie(None)):
|
||||
async def read_items(ads_id: str | None = Cookie(default=None)):
|
||||
return {"ads_id": ads_id}
|
||||
|
|
|
|||
|
|
@ -31,5 +31,5 @@ app.router.route_class = GzipRoute
|
|||
|
||||
|
||||
@app.post("/sum")
|
||||
async def sum_numbers(numbers: List[int] = Body(...)):
|
||||
async def sum_numbers(numbers: List[int] = Body()):
|
||||
return {"sum": sum(numbers)}
|
||||
|
|
|
|||
|
|
@ -25,5 +25,5 @@ app.router.route_class = ValidationErrorLoggingRoute
|
|||
|
||||
|
||||
@app.post("/")
|
||||
async def sum_numbers(numbers: List[int] = Body(...)):
|
||||
async def sum_numbers(numbers: List[int] = Body()):
|
||||
return sum(numbers)
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ def query_extractor(q: Optional[str] = None):
|
|||
|
||||
|
||||
def query_or_cookie_extractor(
|
||||
q: str = Depends(query_extractor), last_query: Optional[str] = Cookie(None)
|
||||
q: str = Depends(query_extractor), last_query: Optional[str] = Cookie(default=None)
|
||||
):
|
||||
if not q:
|
||||
return last_query
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ def query_extractor(q: str | None = None):
|
|||
|
||||
|
||||
def query_or_cookie_extractor(
|
||||
q: str = Depends(query_extractor), last_query: str | None = Cookie(None)
|
||||
q: str = Depends(query_extractor), last_query: str | None = Cookie(default=None)
|
||||
):
|
||||
if not q:
|
||||
return last_query
|
||||
|
|
|
|||
|
|
@ -3,12 +3,12 @@ from fastapi import Depends, FastAPI, Header, HTTPException
|
|||
app = FastAPI()
|
||||
|
||||
|
||||
async def verify_token(x_token: str = Header(...)):
|
||||
async def verify_token(x_token: str = Header()):
|
||||
if x_token != "fake-super-secret-token":
|
||||
raise HTTPException(status_code=400, detail="X-Token header invalid")
|
||||
|
||||
|
||||
async def verify_key(x_key: str = Header(...)):
|
||||
async def verify_key(x_key: str = Header()):
|
||||
if x_key != "fake-super-secret-key":
|
||||
raise HTTPException(status_code=400, detail="X-Key header invalid")
|
||||
return x_key
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
from fastapi import Depends, FastAPI, Header, HTTPException
|
||||
|
||||
|
||||
async def verify_token(x_token: str = Header(...)):
|
||||
async def verify_token(x_token: str = Header()):
|
||||
if x_token != "fake-super-secret-token":
|
||||
raise HTTPException(status_code=400, detail="X-Token header invalid")
|
||||
|
||||
|
||||
async def verify_key(x_key: str = Header(...)):
|
||||
async def verify_key(x_key: str = Header()):
|
||||
if x_key != "fake-super-secret-key":
|
||||
raise HTTPException(status_code=400, detail="X-Key header invalid")
|
||||
return x_key
|
||||
|
|
|
|||
|
|
@ -10,10 +10,10 @@ app = FastAPI()
|
|||
@app.put("/items/{item_id}")
|
||||
async def read_items(
|
||||
item_id: UUID,
|
||||
start_datetime: Optional[datetime] = Body(None),
|
||||
end_datetime: Optional[datetime] = Body(None),
|
||||
repeat_at: Optional[time] = Body(None),
|
||||
process_after: Optional[timedelta] = Body(None),
|
||||
start_datetime: Optional[datetime] = Body(default=None),
|
||||
end_datetime: Optional[datetime] = Body(default=None),
|
||||
repeat_at: Optional[time] = Body(default=None),
|
||||
process_after: Optional[timedelta] = Body(default=None),
|
||||
):
|
||||
start_process = start_datetime + process_after
|
||||
duration = end_datetime - start_process
|
||||
|
|
|
|||
|
|
@ -9,10 +9,10 @@ app = FastAPI()
|
|||
@app.put("/items/{item_id}")
|
||||
async def read_items(
|
||||
item_id: UUID,
|
||||
start_datetime: datetime | None = Body(None),
|
||||
end_datetime: datetime | None = Body(None),
|
||||
repeat_at: time | None = Body(None),
|
||||
process_after: timedelta | None = Body(None),
|
||||
start_datetime: datetime | None = Body(default=None),
|
||||
end_datetime: datetime | None = Body(default=None),
|
||||
repeat_at: time | None = Body(default=None),
|
||||
process_after: timedelta | None = Body(default=None),
|
||||
):
|
||||
start_process = start_datetime + process_after
|
||||
duration = end_datetime - start_process
|
||||
|
|
|
|||
|
|
@ -6,5 +6,5 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(user_agent: Optional[str] = Header(None)):
|
||||
async def read_items(user_agent: Optional[str] = Header(default=None)):
|
||||
return {"User-Agent": user_agent}
|
||||
|
|
|
|||
|
|
@ -4,5 +4,5 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(user_agent: str | None = Header(None)):
|
||||
async def read_items(user_agent: str | None = Header(default=None)):
|
||||
return {"User-Agent": user_agent}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,6 @@ app = FastAPI()
|
|||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
strange_header: Optional[str] = Header(None, convert_underscores=False)
|
||||
strange_header: Optional[str] = Header(default=None, convert_underscores=False)
|
||||
):
|
||||
return {"strange_header": strange_header}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,6 @@ app = FastAPI()
|
|||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
strange_header: str | None = Header(None, convert_underscores=False)
|
||||
strange_header: str | None = Header(default=None, convert_underscores=False)
|
||||
):
|
||||
return {"strange_header": strange_header}
|
||||
|
|
|
|||
|
|
@ -6,5 +6,5 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(x_token: Optional[List[str]] = Header(None)):
|
||||
async def read_items(x_token: Optional[List[str]] = Header(default=None)):
|
||||
return {"X-Token values": x_token}
|
||||
|
|
|
|||
|
|
@ -4,5 +4,5 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(x_token: list[str] | None = Header(None)):
|
||||
async def read_items(x_token: list[str] | None = Header(default=None)):
|
||||
return {"X-Token values": x_token}
|
||||
|
|
|
|||
|
|
@ -6,5 +6,5 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(x_token: Optional[list[str]] = Header(None)):
|
||||
async def read_items(x_token: Optional[list[str]] = Header(default=None)):
|
||||
return {"X-Token values": x_token}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Path, Query
|
||||
|
||||
|
|
@ -7,8 +7,8 @@ app = FastAPI()
|
|||
|
||||
@app.get("/items/{item_id}")
|
||||
async def read_items(
|
||||
item_id: int = Path(..., title="The ID of the item to get"),
|
||||
q: Optional[str] = Query(None, alias="item-query"),
|
||||
item_id: int = Path(title="The ID of the item to get"),
|
||||
q: Union[str, None] = Query(default=None, alias="item-query"),
|
||||
):
|
||||
results = {"item_id": item_id}
|
||||
if q:
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ app = FastAPI()
|
|||
|
||||
@app.get("/items/{item_id}")
|
||||
async def read_items(
|
||||
item_id: int = Path(..., title="The ID of the item to get"),
|
||||
q: str | None = Query(None, alias="item-query"),
|
||||
item_id: int = Path(title="The ID of the item to get"),
|
||||
q: str | None = Query(default=None, alias="item-query"),
|
||||
):
|
||||
results = {"item_id": item_id}
|
||||
if q:
|
||||
|
|
|
|||
|
|
@ -4,9 +4,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
async def read_items(
|
||||
q: str, item_id: int = Path(..., title="The ID of the item to get")
|
||||
):
|
||||
async def read_items(q: str, item_id: int = Path(title="The ID of the item to get")):
|
||||
results = {"item_id": item_id}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
|
|
|||
|
|
@ -4,9 +4,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
async def read_items(
|
||||
*, item_id: int = Path(..., title="The ID of the item to get"), q: str
|
||||
):
|
||||
async def read_items(*, item_id: int = Path(title="The ID of the item to get"), q: str):
|
||||
results = {"item_id": item_id}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ app = FastAPI()
|
|||
|
||||
@app.get("/items/{item_id}")
|
||||
async def read_items(
|
||||
*, item_id: int = Path(..., title="The ID of the item to get", ge=1), q: str
|
||||
*, item_id: int = Path(title="The ID of the item to get", ge=1), q: str
|
||||
):
|
||||
results = {"item_id": item_id}
|
||||
if q:
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ app = FastAPI()
|
|||
@app.get("/items/{item_id}")
|
||||
async def read_items(
|
||||
*,
|
||||
item_id: int = Path(..., title="The ID of the item to get", gt=0, le=1000),
|
||||
item_id: int = Path(title="The ID of the item to get", gt=0, le=1000),
|
||||
q: str,
|
||||
):
|
||||
results = {"item_id": item_id}
|
||||
|
|
|
|||
|
|
@ -6,9 +6,9 @@ app = FastAPI()
|
|||
@app.get("/items/{item_id}")
|
||||
async def read_items(
|
||||
*,
|
||||
item_id: int = Path(..., title="The ID of the item to get", ge=0, le=1000),
|
||||
item_id: int = Path(title="The ID of the item to get", ge=0, le=1000),
|
||||
q: str,
|
||||
size: float = Query(..., gt=0, lt=10.5)
|
||||
size: float = Query(gt=0, lt=10.5)
|
||||
):
|
||||
results = {"item_id": item_id}
|
||||
if q:
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
|
|
@ -6,7 +6,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Optional[str] = Query(None, max_length=50)):
|
||||
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str | None = Query(None, max_length=50)):
|
||||
async def read_items(q: str | None = Query(default=None, max_length=50)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
|
|
@ -6,7 +6,9 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Optional[str] = Query(None, min_length=3, max_length=50)):
|
||||
async def read_items(
|
||||
q: Union[str, None] = Query(default=None, min_length=3, max_length=50)
|
||||
):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str | None = Query(None, min_length=3, max_length=50)):
|
||||
async def read_items(q: str | None = Query(default=None, min_length=3, max_length=50)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
|
|
@ -7,7 +7,9 @@ app = FastAPI()
|
|||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
q: Optional[str] = Query(None, min_length=3, max_length=50, regex="^fixedquery$")
|
||||
q: Union[str, None] = Query(
|
||||
default=None, min_length=3, max_length=50, regex="^fixedquery$"
|
||||
)
|
||||
):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
|
|
|
|||
|
|
@ -5,7 +5,8 @@ app = FastAPI()
|
|||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
q: str | None = Query(None, min_length=3, max_length=50, regex="^fixedquery$")
|
||||
q: str
|
||||
| None = Query(default=None, min_length=3, max_length=50, regex="^fixedquery$")
|
||||
):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str = Query("fixedquery", min_length=3)):
|
||||
async def read_items(q: str = Query(default="fixedquery", min_length=3)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str = Query(..., min_length=3)):
|
||||
async def read_items(q: str = Query(min_length=3)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
from fastapi import FastAPI, Query
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str = Query(default=..., min_length=3)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
return results
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Union[str, None] = Query(default=..., min_length=3)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
return results
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
from fastapi import FastAPI, Query
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str | None = Query(default=..., min_length=3)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
return results
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
from fastapi import FastAPI, Query
|
||||
from pydantic import Required
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str = Query(default=Required, min_length=3)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
return results
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
|
|
@ -7,7 +7,7 @@ app = FastAPI()
|
|||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
q: Optional[str] = Query(None, title="Query string", min_length=3)
|
||||
q: Union[str, None] = Query(default=None, title="Query string", min_length=3)
|
||||
):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str | None = Query(None, title="Query string", min_length=3)):
|
||||
async def read_items(
|
||||
q: str | None = Query(default=None, title="Query string", min_length=3)
|
||||
):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
|
|
@ -7,8 +7,8 @@ app = FastAPI()
|
|||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
q: Optional[str] = Query(
|
||||
None,
|
||||
q: Union[str, None] = Query(
|
||||
default=None,
|
||||
title="Query string",
|
||||
description="Query string for the items to search in the database that have a good match",
|
||||
min_length=3,
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ app = FastAPI()
|
|||
async def read_items(
|
||||
q: str
|
||||
| None = Query(
|
||||
None,
|
||||
default=None,
|
||||
title="Query string",
|
||||
description="Query string for the items to search in the database that have a good match",
|
||||
min_length=3,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
|
|
@ -6,7 +6,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Optional[str] = Query(None, alias="item-query")):
|
||||
async def read_items(q: Union[str, None] = Query(default=None, alias="item-query")):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str | None = Query(None, alias="item-query")):
|
||||
async def read_items(q: str | None = Query(default=None, alias="item-query")):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
|
|
@ -7,8 +7,8 @@ app = FastAPI()
|
|||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
q: Optional[str] = Query(
|
||||
None,
|
||||
q: Union[str, None] = Query(
|
||||
default=None,
|
||||
alias="item-query",
|
||||
title="Query string",
|
||||
description="Query string for the items to search in the database that have a good match",
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ app = FastAPI()
|
|||
async def read_items(
|
||||
q: str
|
||||
| None = Query(
|
||||
None,
|
||||
default=None,
|
||||
alias="item-query",
|
||||
title="Query string",
|
||||
description="Query string for the items to search in the database that have a good match",
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import List, Optional
|
||||
from typing import List, Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
|
|
@ -6,6 +6,6 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Optional[List[str]] = Query(None)):
|
||||
async def read_items(q: Union[List[str], None] = Query(default=None)):
|
||||
query_items = {"q": q}
|
||||
return query_items
|
||||
|
|
|
|||
|
|
@ -4,6 +4,6 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: list[str] | None = Query(None)):
|
||||
async def read_items(q: list[str] | None = Query(default=None)):
|
||||
query_items = {"q": q}
|
||||
return query_items
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
|
|
@ -6,6 +6,6 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Optional[list[str]] = Query(None)):
|
||||
async def read_items(q: Union[list[str], None] = Query(default=None)):
|
||||
query_items = {"q": q}
|
||||
return query_items
|
||||
|
|
|
|||
|
|
@ -6,6 +6,6 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: List[str] = Query(["foo", "bar"])):
|
||||
async def read_items(q: List[str] = Query(default=["foo", "bar"])):
|
||||
query_items = {"q": q}
|
||||
return query_items
|
||||
|
|
|
|||
|
|
@ -4,6 +4,6 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: list[str] = Query(["foo", "bar"])):
|
||||
async def read_items(q: list[str] = Query(default=["foo", "bar"])):
|
||||
query_items = {"q": q}
|
||||
return query_items
|
||||
|
|
|
|||
|
|
@ -4,6 +4,6 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: list = Query([])):
|
||||
async def read_items(q: list = Query(default=[])):
|
||||
query_items = {"q": q}
|
||||
return query_items
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
|
|
@ -7,7 +7,7 @@ app = FastAPI()
|
|||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
hidden_query: Optional[str] = Query(None, include_in_schema=False)
|
||||
hidden_query: Union[str, None] = Query(default=None, include_in_schema=False)
|
||||
):
|
||||
if hidden_query:
|
||||
return {"hidden_query": hidden_query}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(hidden_query: str | None = Query(None, include_in_schema=False)):
|
||||
async def read_items(
|
||||
hidden_query: str | None = Query(default=None, include_in_schema=False)
|
||||
):
|
||||
if hidden_query:
|
||||
return {"hidden_query": hidden_query}
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.post("/files/")
|
||||
async def create_file(file: bytes = File(...)):
|
||||
async def create_file(file: bytes = File()):
|
||||
return {"file_size": len(file)}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.post("/files/")
|
||||
async def create_file(file: Optional[bytes] = File(None)):
|
||||
async def create_file(file: Optional[bytes] = File(default=None)):
|
||||
if not file:
|
||||
return {"message": "No file sent"}
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.post("/files/")
|
||||
async def create_file(file: bytes | None = File(None)):
|
||||
async def create_file(file: bytes | None = File(default=None)):
|
||||
if not file:
|
||||
return {"message": "No file sent"}
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -4,12 +4,12 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.post("/files/")
|
||||
async def create_file(file: bytes = File(..., description="A file read as bytes")):
|
||||
async def create_file(file: bytes = File(description="A file read as bytes")):
|
||||
return {"file_size": len(file)}
|
||||
|
||||
|
||||
@app.post("/uploadfile/")
|
||||
async def create_upload_file(
|
||||
file: UploadFile = File(..., description="A file read as UploadFile")
|
||||
file: UploadFile = File(description="A file read as UploadFile"),
|
||||
):
|
||||
return {"filename": file.filename}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.post("/files/")
|
||||
async def create_files(files: List[bytes] = File(...)):
|
||||
async def create_files(files: List[bytes] = File()):
|
||||
return {"file_sizes": [len(file) for file in files]}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.post("/files/")
|
||||
async def create_files(files: list[bytes] = File(...)):
|
||||
async def create_files(files: list[bytes] = File()):
|
||||
return {"file_sizes": [len(file) for file in files]}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -8,14 +8,14 @@ app = FastAPI()
|
|||
|
||||
@app.post("/files/")
|
||||
async def create_files(
|
||||
files: List[bytes] = File(..., description="Multiple files as bytes")
|
||||
files: List[bytes] = File(description="Multiple files as bytes"),
|
||||
):
|
||||
return {"file_sizes": [len(file) for file in files]}
|
||||
|
||||
|
||||
@app.post("/uploadfiles/")
|
||||
async def create_upload_files(
|
||||
files: List[UploadFile] = File(..., description="Multiple files as UploadFile")
|
||||
files: List[UploadFile] = File(description="Multiple files as UploadFile"),
|
||||
):
|
||||
return {"filenames": [file.filename for file in files]}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,14 +6,14 @@ app = FastAPI()
|
|||
|
||||
@app.post("/files/")
|
||||
async def create_files(
|
||||
files: list[bytes] = File(..., description="Multiple files as bytes")
|
||||
files: list[bytes] = File(description="Multiple files as bytes"),
|
||||
):
|
||||
return {"file_sizes": [len(file) for file in files]}
|
||||
|
||||
|
||||
@app.post("/uploadfiles/")
|
||||
async def create_upload_files(
|
||||
files: list[UploadFile] = File(..., description="Multiple files as UploadFile")
|
||||
files: list[UploadFile] = File(description="Multiple files as UploadFile"),
|
||||
):
|
||||
return {"filenames": [file.filename for file in files]}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,5 +4,5 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.post("/login/")
|
||||
async def login(username: str = Form(...), password: str = Form(...)):
|
||||
async def login(username: str = Form(), password: str = Form()):
|
||||
return {"username": username}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ app = FastAPI()
|
|||
|
||||
@app.post("/files/")
|
||||
async def create_file(
|
||||
file: bytes = File(...), fileb: UploadFile = File(...), token: str = Form(...)
|
||||
file: bytes = File(), fileb: UploadFile = File(), token: str = Form()
|
||||
):
|
||||
return {
|
||||
"file_size": len(file),
|
||||
|
|
|
|||
|
|
@ -7,10 +7,10 @@ app = FastAPI()
|
|||
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str = Field(..., example="Foo")
|
||||
description: Optional[str] = Field(None, example="A very nice Item")
|
||||
price: float = Field(..., example=35.4)
|
||||
tax: Optional[float] = Field(None, example=3.2)
|
||||
name: str = Field(example="Foo")
|
||||
description: Optional[str] = Field(default=None, example="A very nice Item")
|
||||
price: float = Field(example=35.4)
|
||||
tax: Optional[float] = Field(default=None, example=3.2)
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@ app = FastAPI()
|
|||
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str = Field(..., example="Foo")
|
||||
description: str | None = Field(None, example="A very nice Item")
|
||||
price: float = Field(..., example=35.4)
|
||||
tax: float | None = Field(None, example=3.2)
|
||||
name: str = Field(example="Foo")
|
||||
description: str | None = Field(default=None, example="A very nice Item")
|
||||
price: float = Field(example=35.4)
|
||||
tax: float | None = Field(default=None, example=3.2)
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ class Item(BaseModel):
|
|||
async def update_item(
|
||||
item_id: int,
|
||||
item: Item = Body(
|
||||
...,
|
||||
example={
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@ class Item(BaseModel):
|
|||
async def update_item(
|
||||
item_id: int,
|
||||
item: Item = Body(
|
||||
...,
|
||||
example={
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ async def update_item(
|
|||
*,
|
||||
item_id: int,
|
||||
item: Item = Body(
|
||||
...,
|
||||
examples={
|
||||
"normal": {
|
||||
"summary": "A normal example",
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ async def update_item(
|
|||
*,
|
||||
item_id: int,
|
||||
item: Item = Body(
|
||||
...,
|
||||
examples={
|
||||
"normal": {
|
||||
"summary": "A normal example",
|
||||
|
|
|
|||
|
|
@ -57,8 +57,8 @@ async def get():
|
|||
|
||||
async def get_cookie_or_token(
|
||||
websocket: WebSocket,
|
||||
session: Optional[str] = Cookie(None),
|
||||
token: Optional[str] = Query(None),
|
||||
session: Optional[str] = Cookie(default=None),
|
||||
token: Optional[str] = Query(default=None),
|
||||
):
|
||||
if session is None and token is None:
|
||||
await websocket.close(code=status.WS_1008_POLICY_VIOLATION)
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ from pydantic.fields import (
|
|||
FieldInfo,
|
||||
ModelField,
|
||||
Required,
|
||||
Undefined,
|
||||
)
|
||||
from pydantic.schema import get_annotation_from_field_info
|
||||
from pydantic.typing import ForwardRef, evaluate_forwardref
|
||||
|
|
@ -316,7 +317,7 @@ def get_dependant(
|
|||
field_info = param_field.field_info
|
||||
assert isinstance(
|
||||
field_info, params.Body
|
||||
), f"Param: {param_field.name} can only be a request body, using Body(...)"
|
||||
), f"Param: {param_field.name} can only be a request body, using Body()"
|
||||
dependant.body_params.append(param_field)
|
||||
return dependant
|
||||
|
||||
|
|
@ -353,7 +354,7 @@ def get_param_field(
|
|||
force_type: Optional[params.ParamTypes] = None,
|
||||
ignore_default: bool = False,
|
||||
) -> ModelField:
|
||||
default_value = Required
|
||||
default_value: Any = Undefined
|
||||
had_schema = False
|
||||
if not param.default == param.empty and ignore_default is False:
|
||||
default_value = param.default
|
||||
|
|
@ -369,8 +370,13 @@ def get_param_field(
|
|||
if force_type:
|
||||
field_info.in_ = force_type # type: ignore
|
||||
else:
|
||||
field_info = default_field_info(default_value)
|
||||
required = default_value == Required
|
||||
field_info = default_field_info(default=default_value)
|
||||
required = True
|
||||
if default_value is Required or ignore_default:
|
||||
required = True
|
||||
default_value = None
|
||||
elif default_value is not Undefined:
|
||||
required = False
|
||||
annotation: Any = Any
|
||||
if not param.annotation == param.empty:
|
||||
annotation = param.annotation
|
||||
|
|
@ -382,12 +388,11 @@ def get_param_field(
|
|||
field = create_response_field(
|
||||
name=param.name,
|
||||
type_=annotation,
|
||||
default=None if required else default_value,
|
||||
default=default_value,
|
||||
alias=alias,
|
||||
required=required,
|
||||
field_info=field_info,
|
||||
)
|
||||
field.required = required
|
||||
if not had_schema and not is_scalar_field(field=field):
|
||||
field.field_info = params.Body(field_info.default)
|
||||
if not had_schema and lenient_issubclass(field.type_, UploadFile):
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ class Server(BaseModel):
|
|||
|
||||
|
||||
class Reference(BaseModel):
|
||||
ref: str = Field(..., alias="$ref")
|
||||
ref: str = Field(alias="$ref")
|
||||
|
||||
|
||||
class Discriminator(BaseModel):
|
||||
|
|
@ -101,28 +101,28 @@ class ExternalDocumentation(BaseModel):
|
|||
|
||||
|
||||
class Schema(BaseModel):
|
||||
ref: Optional[str] = Field(None, alias="$ref")
|
||||
ref: Optional[str] = Field(default=None, alias="$ref")
|
||||
title: Optional[str] = None
|
||||
multipleOf: Optional[float] = None
|
||||
maximum: Optional[float] = None
|
||||
exclusiveMaximum: Optional[float] = None
|
||||
minimum: Optional[float] = None
|
||||
exclusiveMinimum: Optional[float] = None
|
||||
maxLength: Optional[int] = Field(None, gte=0)
|
||||
minLength: Optional[int] = Field(None, gte=0)
|
||||
maxLength: Optional[int] = Field(default=None, gte=0)
|
||||
minLength: Optional[int] = Field(default=None, gte=0)
|
||||
pattern: Optional[str] = None
|
||||
maxItems: Optional[int] = Field(None, gte=0)
|
||||
minItems: Optional[int] = Field(None, gte=0)
|
||||
maxItems: Optional[int] = Field(default=None, gte=0)
|
||||
minItems: Optional[int] = Field(default=None, gte=0)
|
||||
uniqueItems: Optional[bool] = None
|
||||
maxProperties: Optional[int] = Field(None, gte=0)
|
||||
minProperties: Optional[int] = Field(None, gte=0)
|
||||
maxProperties: Optional[int] = Field(default=None, gte=0)
|
||||
minProperties: Optional[int] = Field(default=None, gte=0)
|
||||
required: Optional[List[str]] = None
|
||||
enum: Optional[List[Any]] = None
|
||||
type: Optional[str] = None
|
||||
allOf: Optional[List["Schema"]] = None
|
||||
oneOf: Optional[List["Schema"]] = None
|
||||
anyOf: Optional[List["Schema"]] = None
|
||||
not_: Optional["Schema"] = Field(None, alias="not")
|
||||
not_: Optional["Schema"] = Field(default=None, alias="not")
|
||||
items: Optional[Union["Schema", List["Schema"]]] = None
|
||||
properties: Optional[Dict[str, "Schema"]] = None
|
||||
additionalProperties: Optional[Union["Schema", Reference, bool]] = None
|
||||
|
|
@ -171,7 +171,7 @@ class Encoding(BaseModel):
|
|||
|
||||
|
||||
class MediaType(BaseModel):
|
||||
schema_: Optional[Union[Schema, Reference]] = Field(None, alias="schema")
|
||||
schema_: Optional[Union[Schema, Reference]] = Field(default=None, alias="schema")
|
||||
example: Optional[Any] = None
|
||||
examples: Optional[Dict[str, Union[Example, Reference]]] = None
|
||||
encoding: Optional[Dict[str, Encoding]] = None
|
||||
|
|
@ -188,7 +188,7 @@ class ParameterBase(BaseModel):
|
|||
style: Optional[str] = None
|
||||
explode: Optional[bool] = None
|
||||
allowReserved: Optional[bool] = None
|
||||
schema_: Optional[Union[Schema, Reference]] = Field(None, alias="schema")
|
||||
schema_: Optional[Union[Schema, Reference]] = Field(default=None, alias="schema")
|
||||
example: Optional[Any] = None
|
||||
examples: Optional[Dict[str, Union[Example, Reference]]] = None
|
||||
# Serialization rules for more complex scenarios
|
||||
|
|
@ -200,7 +200,7 @@ class ParameterBase(BaseModel):
|
|||
|
||||
class Parameter(ParameterBase):
|
||||
name: str
|
||||
in_: ParameterInType = Field(..., alias="in")
|
||||
in_: ParameterInType = Field(alias="in")
|
||||
|
||||
|
||||
class Header(ParameterBase):
|
||||
|
|
@ -258,7 +258,7 @@ class Operation(BaseModel):
|
|||
|
||||
|
||||
class PathItem(BaseModel):
|
||||
ref: Optional[str] = Field(None, alias="$ref")
|
||||
ref: Optional[str] = Field(default=None, alias="$ref")
|
||||
summary: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
get: Optional[Operation] = None
|
||||
|
|
@ -284,7 +284,7 @@ class SecuritySchemeType(Enum):
|
|||
|
||||
|
||||
class SecurityBase(BaseModel):
|
||||
type_: SecuritySchemeType = Field(..., alias="type")
|
||||
type_: SecuritySchemeType = Field(alias="type")
|
||||
description: Optional[str] = None
|
||||
|
||||
class Config:
|
||||
|
|
@ -299,7 +299,7 @@ class APIKeyIn(Enum):
|
|||
|
||||
class APIKey(SecurityBase):
|
||||
type_ = Field(SecuritySchemeType.apiKey, alias="type")
|
||||
in_: APIKeyIn = Field(..., alias="in")
|
||||
in_: APIKeyIn = Field(alias="in")
|
||||
name: str
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ from pydantic.fields import Undefined
|
|||
|
||||
|
||||
def Path( # noqa: N802
|
||||
default: Any,
|
||||
default: Any = Undefined,
|
||||
*,
|
||||
alias: Optional[str] = None,
|
||||
title: Optional[str] = None,
|
||||
|
|
@ -44,7 +44,7 @@ def Path( # noqa: N802
|
|||
|
||||
|
||||
def Query( # noqa: N802
|
||||
default: Any,
|
||||
default: Any = Undefined,
|
||||
*,
|
||||
alias: Optional[str] = None,
|
||||
title: Optional[str] = None,
|
||||
|
|
@ -63,7 +63,7 @@ def Query( # noqa: N802
|
|||
**extra: Any,
|
||||
) -> Any:
|
||||
return params.Query(
|
||||
default,
|
||||
default=default,
|
||||
alias=alias,
|
||||
title=title,
|
||||
description=description,
|
||||
|
|
@ -83,7 +83,7 @@ def Query( # noqa: N802
|
|||
|
||||
|
||||
def Header( # noqa: N802
|
||||
default: Any,
|
||||
default: Any = Undefined,
|
||||
*,
|
||||
alias: Optional[str] = None,
|
||||
convert_underscores: bool = True,
|
||||
|
|
@ -103,7 +103,7 @@ def Header( # noqa: N802
|
|||
**extra: Any,
|
||||
) -> Any:
|
||||
return params.Header(
|
||||
default,
|
||||
default=default,
|
||||
alias=alias,
|
||||
convert_underscores=convert_underscores,
|
||||
title=title,
|
||||
|
|
@ -124,7 +124,7 @@ def Header( # noqa: N802
|
|||
|
||||
|
||||
def Cookie( # noqa: N802
|
||||
default: Any,
|
||||
default: Any = Undefined,
|
||||
*,
|
||||
alias: Optional[str] = None,
|
||||
title: Optional[str] = None,
|
||||
|
|
@ -143,7 +143,7 @@ def Cookie( # noqa: N802
|
|||
**extra: Any,
|
||||
) -> Any:
|
||||
return params.Cookie(
|
||||
default,
|
||||
default=default,
|
||||
alias=alias,
|
||||
title=title,
|
||||
description=description,
|
||||
|
|
@ -163,7 +163,7 @@ def Cookie( # noqa: N802
|
|||
|
||||
|
||||
def Body( # noqa: N802
|
||||
default: Any,
|
||||
default: Any = Undefined,
|
||||
*,
|
||||
embed: bool = False,
|
||||
media_type: str = "application/json",
|
||||
|
|
@ -182,7 +182,7 @@ def Body( # noqa: N802
|
|||
**extra: Any,
|
||||
) -> Any:
|
||||
return params.Body(
|
||||
default,
|
||||
default=default,
|
||||
embed=embed,
|
||||
media_type=media_type,
|
||||
alias=alias,
|
||||
|
|
@ -202,7 +202,7 @@ def Body( # noqa: N802
|
|||
|
||||
|
||||
def Form( # noqa: N802
|
||||
default: Any,
|
||||
default: Any = Undefined,
|
||||
*,
|
||||
media_type: str = "application/x-www-form-urlencoded",
|
||||
alias: Optional[str] = None,
|
||||
|
|
@ -220,7 +220,7 @@ def Form( # noqa: N802
|
|||
**extra: Any,
|
||||
) -> Any:
|
||||
return params.Form(
|
||||
default,
|
||||
default=default,
|
||||
media_type=media_type,
|
||||
alias=alias,
|
||||
title=title,
|
||||
|
|
@ -239,7 +239,7 @@ def Form( # noqa: N802
|
|||
|
||||
|
||||
def File( # noqa: N802
|
||||
default: Any,
|
||||
default: Any = Undefined,
|
||||
*,
|
||||
media_type: str = "multipart/form-data",
|
||||
alias: Optional[str] = None,
|
||||
|
|
@ -257,7 +257,7 @@ def File( # noqa: N802
|
|||
**extra: Any,
|
||||
) -> Any:
|
||||
return params.File(
|
||||
default,
|
||||
default=default,
|
||||
media_type=media_type,
|
||||
alias=alias,
|
||||
title=title,
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ class Param(FieldInfo):
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
default: Any,
|
||||
default: Any = Undefined,
|
||||
*,
|
||||
alias: Optional[str] = None,
|
||||
title: Optional[str] = None,
|
||||
|
|
@ -39,7 +39,7 @@ class Param(FieldInfo):
|
|||
self.examples = examples
|
||||
self.include_in_schema = include_in_schema
|
||||
super().__init__(
|
||||
default,
|
||||
default=default,
|
||||
alias=alias,
|
||||
title=title,
|
||||
description=description,
|
||||
|
|
@ -62,7 +62,7 @@ class Path(Param):
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
default: Any,
|
||||
default: Any = Undefined,
|
||||
*,
|
||||
alias: Optional[str] = None,
|
||||
title: Optional[str] = None,
|
||||
|
|
@ -82,7 +82,7 @@ class Path(Param):
|
|||
):
|
||||
self.in_ = self.in_
|
||||
super().__init__(
|
||||
...,
|
||||
default=...,
|
||||
alias=alias,
|
||||
title=title,
|
||||
description=description,
|
||||
|
|
@ -106,7 +106,7 @@ class Query(Param):
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
default: Any,
|
||||
default: Any = Undefined,
|
||||
*,
|
||||
alias: Optional[str] = None,
|
||||
title: Optional[str] = None,
|
||||
|
|
@ -125,7 +125,7 @@ class Query(Param):
|
|||
**extra: Any,
|
||||
):
|
||||
super().__init__(
|
||||
default,
|
||||
default=default,
|
||||
alias=alias,
|
||||
title=title,
|
||||
description=description,
|
||||
|
|
@ -149,7 +149,7 @@ class Header(Param):
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
default: Any,
|
||||
default: Any = Undefined,
|
||||
*,
|
||||
alias: Optional[str] = None,
|
||||
convert_underscores: bool = True,
|
||||
|
|
@ -170,7 +170,7 @@ class Header(Param):
|
|||
):
|
||||
self.convert_underscores = convert_underscores
|
||||
super().__init__(
|
||||
default,
|
||||
default=default,
|
||||
alias=alias,
|
||||
title=title,
|
||||
description=description,
|
||||
|
|
@ -194,7 +194,7 @@ class Cookie(Param):
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
default: Any,
|
||||
default: Any = Undefined,
|
||||
*,
|
||||
alias: Optional[str] = None,
|
||||
title: Optional[str] = None,
|
||||
|
|
@ -213,7 +213,7 @@ class Cookie(Param):
|
|||
**extra: Any,
|
||||
):
|
||||
super().__init__(
|
||||
default,
|
||||
default=default,
|
||||
alias=alias,
|
||||
title=title,
|
||||
description=description,
|
||||
|
|
@ -235,7 +235,7 @@ class Cookie(Param):
|
|||
class Body(FieldInfo):
|
||||
def __init__(
|
||||
self,
|
||||
default: Any,
|
||||
default: Any = Undefined,
|
||||
*,
|
||||
embed: bool = False,
|
||||
media_type: str = "application/json",
|
||||
|
|
@ -258,7 +258,7 @@ class Body(FieldInfo):
|
|||
self.example = example
|
||||
self.examples = examples
|
||||
super().__init__(
|
||||
default,
|
||||
default=default,
|
||||
alias=alias,
|
||||
title=title,
|
||||
description=description,
|
||||
|
|
@ -297,7 +297,7 @@ class Form(Body):
|
|||
**extra: Any,
|
||||
):
|
||||
super().__init__(
|
||||
default,
|
||||
default=default,
|
||||
embed=True,
|
||||
media_type=media_type,
|
||||
alias=alias,
|
||||
|
|
@ -337,7 +337,7 @@ class File(Form):
|
|||
**extra: Any,
|
||||
):
|
||||
super().__init__(
|
||||
default,
|
||||
default=default,
|
||||
media_type=media_type,
|
||||
alias=alias,
|
||||
title=title,
|
||||
|
|
|
|||
|
|
@ -45,12 +45,12 @@ class OAuth2PasswordRequestForm:
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
grant_type: str = Form(None, regex="password"),
|
||||
username: str = Form(...),
|
||||
password: str = Form(...),
|
||||
scope: str = Form(""),
|
||||
client_id: Optional[str] = Form(None),
|
||||
client_secret: Optional[str] = Form(None),
|
||||
grant_type: str = Form(default=None, regex="password"),
|
||||
username: str = Form(),
|
||||
password: str = Form(),
|
||||
scope: str = Form(default=""),
|
||||
client_id: Optional[str] = Form(default=None),
|
||||
client_secret: Optional[str] = Form(default=None),
|
||||
):
|
||||
self.grant_type = grant_type
|
||||
self.username = username
|
||||
|
|
@ -95,12 +95,12 @@ class OAuth2PasswordRequestFormStrict(OAuth2PasswordRequestForm):
|
|||
|
||||
def __init__(
|
||||
self,
|
||||
grant_type: str = Form(..., regex="password"),
|
||||
username: str = Form(...),
|
||||
password: str = Form(...),
|
||||
scope: str = Form(""),
|
||||
client_id: Optional[str] = Form(None),
|
||||
client_secret: Optional[str] = Form(None),
|
||||
grant_type: str = Form(regex="password"),
|
||||
username: str = Form(),
|
||||
password: str = Form(),
|
||||
scope: str = Form(default=""),
|
||||
client_id: Optional[str] = Form(default=None),
|
||||
client_secret: Optional[str] = Form(default=None),
|
||||
):
|
||||
super().__init__(
|
||||
grant_type=grant_type,
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ def create_response_field(
|
|||
Create a new response field. Raises if type_ is invalid.
|
||||
"""
|
||||
class_validators = class_validators or {}
|
||||
field_info = field_info or FieldInfo(None)
|
||||
field_info = field_info or FieldInfo()
|
||||
|
||||
response_field = functools.partial(
|
||||
ModelField,
|
||||
|
|
|
|||
|
|
@ -49,97 +49,97 @@ def get_bool_id(item_id: bool):
|
|||
|
||||
|
||||
@app.get("/path/param/{item_id}")
|
||||
def get_path_param_id(item_id: Optional[str] = Path(None)):
|
||||
def get_path_param_id(item_id: str = Path()):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-required/{item_id}")
|
||||
def get_path_param_required_id(item_id: str = Path(...)):
|
||||
def get_path_param_required_id(item_id: str = Path()):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-minlength/{item_id}")
|
||||
def get_path_param_min_length(item_id: str = Path(..., min_length=3)):
|
||||
def get_path_param_min_length(item_id: str = Path(min_length=3)):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-maxlength/{item_id}")
|
||||
def get_path_param_max_length(item_id: str = Path(..., max_length=3)):
|
||||
def get_path_param_max_length(item_id: str = Path(max_length=3)):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-min_maxlength/{item_id}")
|
||||
def get_path_param_min_max_length(item_id: str = Path(..., max_length=3, min_length=2)):
|
||||
def get_path_param_min_max_length(item_id: str = Path(max_length=3, min_length=2)):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-gt/{item_id}")
|
||||
def get_path_param_gt(item_id: float = Path(..., gt=3)):
|
||||
def get_path_param_gt(item_id: float = Path(gt=3)):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-gt0/{item_id}")
|
||||
def get_path_param_gt0(item_id: float = Path(..., gt=0)):
|
||||
def get_path_param_gt0(item_id: float = Path(gt=0)):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-ge/{item_id}")
|
||||
def get_path_param_ge(item_id: float = Path(..., ge=3)):
|
||||
def get_path_param_ge(item_id: float = Path(ge=3)):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-lt/{item_id}")
|
||||
def get_path_param_lt(item_id: float = Path(..., lt=3)):
|
||||
def get_path_param_lt(item_id: float = Path(lt=3)):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-lt0/{item_id}")
|
||||
def get_path_param_lt0(item_id: float = Path(..., lt=0)):
|
||||
def get_path_param_lt0(item_id: float = Path(lt=0)):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-le/{item_id}")
|
||||
def get_path_param_le(item_id: float = Path(..., le=3)):
|
||||
def get_path_param_le(item_id: float = Path(le=3)):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-lt-gt/{item_id}")
|
||||
def get_path_param_lt_gt(item_id: float = Path(..., lt=3, gt=1)):
|
||||
def get_path_param_lt_gt(item_id: float = Path(lt=3, gt=1)):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-le-ge/{item_id}")
|
||||
def get_path_param_le_ge(item_id: float = Path(..., le=3, ge=1)):
|
||||
def get_path_param_le_ge(item_id: float = Path(le=3, ge=1)):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-lt-int/{item_id}")
|
||||
def get_path_param_lt_int(item_id: int = Path(..., lt=3)):
|
||||
def get_path_param_lt_int(item_id: int = Path(lt=3)):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-gt-int/{item_id}")
|
||||
def get_path_param_gt_int(item_id: int = Path(..., gt=3)):
|
||||
def get_path_param_gt_int(item_id: int = Path(gt=3)):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-le-int/{item_id}")
|
||||
def get_path_param_le_int(item_id: int = Path(..., le=3)):
|
||||
def get_path_param_le_int(item_id: int = Path(le=3)):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-ge-int/{item_id}")
|
||||
def get_path_param_ge_int(item_id: int = Path(..., ge=3)):
|
||||
def get_path_param_ge_int(item_id: int = Path(ge=3)):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-lt-gt-int/{item_id}")
|
||||
def get_path_param_lt_gt_int(item_id: int = Path(..., lt=3, gt=1)):
|
||||
def get_path_param_lt_gt_int(item_id: int = Path(lt=3, gt=1)):
|
||||
return item_id
|
||||
|
||||
|
||||
@app.get("/path/param-le-ge-int/{item_id}")
|
||||
def get_path_param_le_ge_int(item_id: int = Path(..., le=3, ge=1)):
|
||||
def get_path_param_le_ge_int(item_id: int = Path(le=3, ge=1)):
|
||||
return item_id
|
||||
|
||||
|
||||
|
|
@ -173,19 +173,19 @@ def get_query_type_int_default(query: int = 10):
|
|||
|
||||
|
||||
@app.get("/query/param")
|
||||
def get_query_param(query=Query(None)):
|
||||
def get_query_param(query=Query(default=None)):
|
||||
if query is None:
|
||||
return "foo bar"
|
||||
return f"foo bar {query}"
|
||||
|
||||
|
||||
@app.get("/query/param-required")
|
||||
def get_query_param_required(query=Query(...)):
|
||||
def get_query_param_required(query=Query()):
|
||||
return f"foo bar {query}"
|
||||
|
||||
|
||||
@app.get("/query/param-required/int")
|
||||
def get_query_param_required_type(query: int = Query(...)):
|
||||
def get_query_param_required_type(query: int = Query()):
|
||||
return f"foo bar {query}"
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -26,14 +26,14 @@ async def get_database():
|
|||
|
||||
@app.put("/invalid-user/{user_id}")
|
||||
def put_invalid_user(
|
||||
user_id: str, name: str = Body(...), db: dict = Depends(get_database)
|
||||
user_id: str, name: str = Body(), db: dict = Depends(get_database)
|
||||
):
|
||||
db[user_id] = name
|
||||
raise HTTPException(status_code=400, detail="Invalid user")
|
||||
|
||||
|
||||
@app.put("/user/{user_id}")
|
||||
def put_user(user_id: str, name: str = Body(...), db: dict = Depends(get_database)):
|
||||
def put_user(user_id: str, name: str = Body(), db: dict = Depends(get_database)):
|
||||
db[user_id] = name
|
||||
return {"message": "OK"}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,17 +5,17 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.post("/form/python-list")
|
||||
def post_form_param_list(items: list = Form(...)):
|
||||
def post_form_param_list(items: list = Form()):
|
||||
return items
|
||||
|
||||
|
||||
@app.post("/form/python-set")
|
||||
def post_form_param_set(items: set = Form(...)):
|
||||
def post_form_param_set(items: set = Form()):
|
||||
return items
|
||||
|
||||
|
||||
@app.post("/form/python-tuple")
|
||||
def post_form_param_tuple(items: tuple = Form(...)):
|
||||
def post_form_param_tuple(items: tuple = Form()):
|
||||
return items
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ def test_invalid_sequence():
|
|||
title: str
|
||||
|
||||
@app.get("/items/")
|
||||
def read_items(q: List[Item] = Query(None)):
|
||||
def read_items(q: List[Item] = Query(default=None)):
|
||||
pass # pragma: no cover
|
||||
|
||||
|
||||
|
|
@ -25,7 +25,7 @@ def test_invalid_tuple():
|
|||
title: str
|
||||
|
||||
@app.get("/items/")
|
||||
def read_items(q: Tuple[Item, Item] = Query(None)):
|
||||
def read_items(q: Tuple[Item, Item] = Query(default=None)):
|
||||
pass # pragma: no cover
|
||||
|
||||
|
||||
|
|
@ -37,7 +37,7 @@ def test_invalid_dict():
|
|||
title: str
|
||||
|
||||
@app.get("/items/")
|
||||
def read_items(q: Dict[str, Item] = Query(None)):
|
||||
def read_items(q: Dict[str, Item] = Query(default=None)):
|
||||
pass # pragma: no cover
|
||||
|
||||
|
||||
|
|
@ -49,5 +49,5 @@ def test_invalid_simple_dict():
|
|||
title: str
|
||||
|
||||
@app.get("/items/")
|
||||
def read_items(q: Optional[dict] = Query(None)):
|
||||
def read_items(q: Optional[dict] = Query(default=None)):
|
||||
pass # pragma: no cover
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ class ModelWithConfig(BaseModel):
|
|||
|
||||
|
||||
class ModelWithAlias(BaseModel):
|
||||
foo: str = Field(..., alias="Foo")
|
||||
foo: str = Field(alias="Foo")
|
||||
|
||||
|
||||
class ModelWithDefault(BaseModel):
|
||||
|
|
|
|||
|
|
@ -4,5 +4,5 @@ router = APIRouter()
|
|||
|
||||
|
||||
@router.post("/compute")
|
||||
def compute(a: int = Body(...), b: str = Body(...)):
|
||||
def compute(a: int = Body(), b: str = Body()):
|
||||
return {"a": a, "b": b}
|
||||
|
|
|
|||
|
|
@ -4,5 +4,5 @@ router = APIRouter()
|
|||
|
||||
|
||||
@router.post("/compute/")
|
||||
def compute(a: int = Body(...), b: str = Body(...)):
|
||||
def compute(a: int = Body(), b: str = Body()):
|
||||
return {"a": a, "b": b}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
def read_items(q: List[int] = Query(None)):
|
||||
def read_items(q: List[int] = Query(default=None)):
|
||||
return {"q": q}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ def test_incorrect_multipart_installed_form(monkeypatch):
|
|||
app = FastAPI()
|
||||
|
||||
@app.post("/")
|
||||
async def root(username: str = Form(...)):
|
||||
async def root(username: str = Form()):
|
||||
return username # pragma: nocover
|
||||
|
||||
|
||||
|
|
@ -22,7 +22,7 @@ def test_incorrect_multipart_installed_file_upload(monkeypatch):
|
|||
app = FastAPI()
|
||||
|
||||
@app.post("/")
|
||||
async def root(f: UploadFile = File(...)):
|
||||
async def root(f: UploadFile = File()):
|
||||
return f # pragma: nocover
|
||||
|
||||
|
||||
|
|
@ -32,7 +32,7 @@ def test_incorrect_multipart_installed_file_bytes(monkeypatch):
|
|||
app = FastAPI()
|
||||
|
||||
@app.post("/")
|
||||
async def root(f: bytes = File(...)):
|
||||
async def root(f: bytes = File()):
|
||||
return f # pragma: nocover
|
||||
|
||||
|
||||
|
|
@ -42,7 +42,7 @@ def test_incorrect_multipart_installed_multi_form(monkeypatch):
|
|||
app = FastAPI()
|
||||
|
||||
@app.post("/")
|
||||
async def root(username: str = Form(...), password: str = Form(...)):
|
||||
async def root(username: str = Form(), password: str = Form()):
|
||||
return username # pragma: nocover
|
||||
|
||||
|
||||
|
|
@ -52,7 +52,7 @@ def test_incorrect_multipart_installed_form_file(monkeypatch):
|
|||
app = FastAPI()
|
||||
|
||||
@app.post("/")
|
||||
async def root(username: str = Form(...), f: UploadFile = File(...)):
|
||||
async def root(username: str = Form(), f: UploadFile = File()):
|
||||
return username # pragma: nocover
|
||||
|
||||
|
||||
|
|
@ -62,7 +62,7 @@ def test_no_multipart_installed(monkeypatch):
|
|||
app = FastAPI()
|
||||
|
||||
@app.post("/")
|
||||
async def root(username: str = Form(...)):
|
||||
async def root(username: str = Form()):
|
||||
return username # pragma: nocover
|
||||
|
||||
|
||||
|
|
@ -72,7 +72,7 @@ def test_no_multipart_installed_file(monkeypatch):
|
|||
app = FastAPI()
|
||||
|
||||
@app.post("/")
|
||||
async def root(f: UploadFile = File(...)):
|
||||
async def root(f: UploadFile = File()):
|
||||
return f # pragma: nocover
|
||||
|
||||
|
||||
|
|
@ -82,7 +82,7 @@ def test_no_multipart_installed_file_bytes(monkeypatch):
|
|||
app = FastAPI()
|
||||
|
||||
@app.post("/")
|
||||
async def root(f: bytes = File(...)):
|
||||
async def root(f: bytes = File()):
|
||||
return f # pragma: nocover
|
||||
|
||||
|
||||
|
|
@ -92,7 +92,7 @@ def test_no_multipart_installed_multi_form(monkeypatch):
|
|||
app = FastAPI()
|
||||
|
||||
@app.post("/")
|
||||
async def root(username: str = Form(...), password: str = Form(...)):
|
||||
async def root(username: str = Form(), password: str = Form()):
|
||||
return username # pragma: nocover
|
||||
|
||||
|
||||
|
|
@ -102,5 +102,5 @@ def test_no_multipart_installed_form_file(monkeypatch):
|
|||
app = FastAPI()
|
||||
|
||||
@app.post("/")
|
||||
async def root(username: str = Form(...), f: UploadFile = File(...)):
|
||||
async def root(username: str = Form(), f: UploadFile = File()):
|
||||
return username # pragma: nocover
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ app = FastAPI()
|
|||
|
||||
|
||||
@app.get("/items/")
|
||||
def read_items(q: Optional[str] = Param(None)): # type: ignore
|
||||
def read_items(q: Optional[str] = Param(default=None)): # type: ignore
|
||||
return {"q": q}
|
||||
|
||||
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue