Add variants for `path-operation-advanced-configuration/tutorial004`

This commit is contained in:
Yurii Motov 2025-11-27 09:24:49 +01:00
parent c23c072572
commit f13f460d9b
4 changed files with 84 additions and 9 deletions

View File

@ -50,7 +50,7 @@ Adding an `\f` (an escaped "form feed" character) causes **FastAPI** to truncate
It won't show up in the documentation, but other tools (such as Sphinx) will be able to use the rest.
{* ../../docs_src/path_operation_advanced_configuration/tutorial004.py hl[19:29] *}
{* ../../docs_src/path_operation_advanced_configuration/tutorial004_py310.py hl[17:27] *}
## Additional Responses { #additional-responses }

View File

@ -0,0 +1,28 @@
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
tags: set[str] = set()
@app.post("/items/", response_model=Item, summary="Create an item")
async def create_item(item: Item):
"""
Create an item with all the information:
- **name**: each item must have a name
- **description**: a long description
- **price**: required
- **tax**: if the item doesn't have tax, you can omit this
- **tags**: a set of unique tag strings for this item
\f
:param item: User input.
"""
return item

View File

@ -0,0 +1,30 @@
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
tags: set[str] = set()
@app.post("/items/", response_model=Item, summary="Create an item")
async def create_item(item: Item):
"""
Create an item with all the information:
- **name**: each item must have a name
- **description**: a long description
- **price**: required
- **tax**: if the item doesn't have tax, you can omit this
- **tags**: a set of unique tag strings for this item
\f
:param item: User input.
"""
return item

View File

@ -1,13 +1,30 @@
import importlib
import pytest
from fastapi.testclient import TestClient
from docs_src.path_operation_advanced_configuration.tutorial004 import app
from ...utils import needs_pydanticv1, needs_pydanticv2
client = TestClient(app)
from ...utils import needs_py39, needs_py310, needs_pydanticv1, needs_pydanticv2
def test_query_params_str_validations():
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial004"),
pytest.param("tutorial004_py39", marks=needs_py39),
pytest.param("tutorial004_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(
f"docs_src.path_operation_advanced_configuration.{request.param}"
)
client = TestClient(mod.app)
client.headers.clear()
return client
def test_query_params_str_validations(client: TestClient):
response = client.post("/items/", json={"name": "Foo", "price": 42})
assert response.status_code == 200, response.text
assert response.json() == {
@ -20,7 +37,7 @@ def test_query_params_str_validations():
@needs_pydanticv2
def test_openapi_schema():
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == {
@ -123,7 +140,7 @@ def test_openapi_schema():
# TODO: remove when deprecating Pydantic v1
@needs_pydanticv1
def test_openapi_schema_pv1():
def test_openapi_schema_pv1(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == {