mirror of https://github.com/tiangolo/fastapi.git
Add variants for `path-operation-advanced-configuration/tutorial007`
This commit is contained in:
parent
449c18e67e
commit
699cd287cb
|
|
@ -155,7 +155,7 @@ For example, in this application we don't use FastAPI's integrated functionality
|
||||||
|
|
||||||
//// tab | Pydantic v2
|
//// tab | Pydantic v2
|
||||||
|
|
||||||
{* ../../docs_src/path_operation_advanced_configuration/tutorial007.py hl[17:22, 24] *}
|
{* ../../docs_src/path_operation_advanced_configuration/tutorial007_py39.py hl[15:20, 22] *}
|
||||||
|
|
||||||
////
|
////
|
||||||
|
|
||||||
|
|
@ -179,7 +179,7 @@ And then in our code, we parse that YAML content directly, and then we are again
|
||||||
|
|
||||||
//// tab | Pydantic v2
|
//// tab | Pydantic v2
|
||||||
|
|
||||||
{* ../../docs_src/path_operation_advanced_configuration/tutorial007.py hl[26:33] *}
|
{* ../../docs_src/path_operation_advanced_configuration/tutorial007_py39.py hl[24:31] *}
|
||||||
|
|
||||||
////
|
////
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
import yaml
|
||||||
|
from fastapi import FastAPI, HTTPException, Request
|
||||||
|
from pydantic import BaseModel, ValidationError
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
class Item(BaseModel):
|
||||||
|
name: str
|
||||||
|
tags: list[str]
|
||||||
|
|
||||||
|
|
||||||
|
@app.post(
|
||||||
|
"/items/",
|
||||||
|
openapi_extra={
|
||||||
|
"requestBody": {
|
||||||
|
"content": {"application/x-yaml": {"schema": Item.model_json_schema()}},
|
||||||
|
"required": True,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
async def create_item(request: Request):
|
||||||
|
raw_body = await request.body()
|
||||||
|
try:
|
||||||
|
data = yaml.safe_load(raw_body)
|
||||||
|
except yaml.YAMLError:
|
||||||
|
raise HTTPException(status_code=422, detail="Invalid YAML")
|
||||||
|
try:
|
||||||
|
item = Item.model_validate(data)
|
||||||
|
except ValidationError as e:
|
||||||
|
raise HTTPException(status_code=422, detail=e.errors(include_url=False))
|
||||||
|
return item
|
||||||
|
|
@ -1,14 +1,24 @@
|
||||||
|
import importlib
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from ...utils import needs_pydanticv2
|
from ...utils import needs_py39, needs_pydanticv2
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
@pytest.fixture(
|
||||||
def get_client():
|
name="client",
|
||||||
from docs_src.path_operation_advanced_configuration.tutorial007 import app
|
params=[
|
||||||
|
pytest.param("tutorial007"),
|
||||||
|
pytest.param("tutorial007_py39", marks=needs_py39),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def get_client(request: pytest.FixtureRequest):
|
||||||
|
mod = importlib.import_module(
|
||||||
|
f"docs_src.path_operation_advanced_configuration.{request.param}"
|
||||||
|
)
|
||||||
|
|
||||||
client = TestClient(app)
|
client = TestClient(mod.app)
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue