Coverage for docs_src / path_operation_advanced_configuration / tutorial007_py310.py: 100%
19 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1import yaml 1abfgcde
2from fastapi import FastAPI, HTTPException, Request 1abfgcde
3from pydantic import BaseModel, ValidationError 1abfgcde
5app = FastAPI() 1abfgcde
8class Item(BaseModel): 1abfgcde
9 name: str 1abcde
10 tags: list[str] 1abcde
13@app.post( 1abfgcde
14 "/items/",
15 openapi_extra={
16 "requestBody": {
17 "content": {"application/x-yaml": {"schema": Item.model_json_schema()}},
18 "required": True,
19 },
20 },
21)
22async def create_item(request: Request): 1abfgcde
23 raw_body = await request.body() 1kohlpmqinrj
24 try: 1kohlpmqinrj
25 data = yaml.safe_load(raw_body) 1kohlpmqinrj
26 except yaml.YAMLError: 1opqr
27 raise HTTPException(status_code=422, detail="Invalid YAML") 1opqr
28 try: 1khlminj
29 item = Item.model_validate(data) 1khlminj
30 except ValidationError as e: 1hsij
31 raise HTTPException(status_code=422, detail=e.errors(include_url=False)) 1hsij
32 return item 1klmn