You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
✨ Add support for not needing ... as default value in required Query(), Path(), Header(), etc. (fastapi#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
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.
19
+
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.
20
20
21
21
!!! note
22
22
FastAPI will know that the value of `q` is not required because of the default value `= None`.
23
23
24
-
The `Optional` in `Optional[str]` is not used by FastAPI, but will allow your editor to give you better support and detect errors.
24
+
The `Union` in `Union[str, None]` will allow your editor to give you better support and detect errors.
25
25
26
26
## Additional validation
27
27
@@ -59,24 +59,24 @@ And now use it as the default value of your parameter, setting the parameter `ma
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.
62
+
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.
63
63
64
64
So:
65
65
66
66
```Python
67
-
q: Optional[str] = Query(None)
67
+
q: Union[str, None] = Query(default=None)
68
68
```
69
69
70
70
...makes the parameter optional, the same as:
71
71
72
72
```Python
73
-
q: Optional[str] =None
73
+
q: Union[str, None] =None
74
74
```
75
75
76
76
And in Python 3.10 and above:
77
77
78
78
```Python
79
-
q: str|None= Query(None)
79
+
q: str|None= Query(default=None)
80
80
```
81
81
82
82
...makes the parameter optional, the same as:
@@ -97,17 +97,17 @@ But it declares it explicitly as being a query parameter.
97
97
or the:
98
98
99
99
```Python
100
-
= Query(None)
100
+
= Query(default=None)
101
101
```
102
102
103
103
as it will use that `None` as the default value, and that way make the parameter **not required**.
104
104
105
-
The `Optional` part allows your editor to provide better support, but it is not what tells FastAPI that this parameter is not required.
105
+
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.
106
106
107
107
Then, we can pass more parameters to `Query`. In this case, the `max_length` parameter that applies to strings:
If you hadn't seen that `...` before: it is a special single value, it is <ahref="https://docs.python.org/3/library/constants.html#Ellipsis"class="external-link"target="_blank">part of Python and is called "Ellipsis"</a>.
198
206
207
+
It is used by Pydantic and FastAPI to explicitly declare that a value is required.
208
+
199
209
This will let **FastAPI** know that this parameter is required.
200
210
211
+
### Required with `None`
212
+
213
+
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`.
214
+
215
+
To do that, you can declare that `None` is a valid type but still use `default=...`:
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 <ahref="https://pydantic-docs.helpmanual.io/usage/models/#required-optional-fields"class="external-link"target="_blank">Required Optional fields</a>.
231
+
232
+
### Use Pydantic's `Required` instead of Ellipsis (`...`)
233
+
234
+
If you feel uncomfortable using `...`, you can also import and use `Required` from Pydantic:
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`.
242
+
201
243
## Query parameter list / multiple values
202
244
203
245
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.
0 commit comments