hos
2 days ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with
20 additions and
0 deletions
-
docs/en/docs/tutorial/query-params.md
-
tests/test_tutorial/test_query_params/test_tutorial001.py
|
|
|
@ -20,6 +20,11 @@ http://127.0.0.1:8000/items/?skip=0&limit=10 |
|
|
|
As they are part of the URL, they are "naturally" strings. |
|
|
|
|
|
|
|
But when you declare them with Python types (in the example above, as `int`), they are converted to that type and validated against it. |
|
|
|
For example, if `skip` is declared as an `int`, sending a value that cannot be converted to an integer: |
|
|
|
``` |
|
|
|
http://127.0.0.1:8000/items/?skip=abc |
|
|
|
``` |
|
|
|
will result in a validation error because `"abc"` cannot be converted to an integer. |
|
|
|
|
|
|
|
All the same processes that apply to path parameters also apply to query parameters: |
|
|
|
|
|
|
|
|
|
|
|
@ -131,3 +131,18 @@ def test_openapi_schema(client: TestClient): |
|
|
|
}, |
|
|
|
} |
|
|
|
) |
|
|
|
|
|
|
|
def test_read_user_item(client: TestClient, path, expected_json): |
|
|
|
response = client.get(path) |
|
|
|
assert response.status_code == 200 |
|
|
|
assert response.json() == expected_json |
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_query_parameter(client: TestClient): |
|
|
|
response = client.get("/items/?skip=invalid") |
|
|
|
|
|
|
|
assert response.status_code == 422 |
|
|
|
assert response.json()["detail"][0]["loc"] == ["query", "skip"] |
|
|
|
|
|
|
|
|
|
|
|
def test_openapi_schema(client: TestClient): |
|
|
|
|