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-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import yaml 1abc
2from fastapi import FastAPI, HTTPException, Request 1abc
3from pydantic import BaseModel, ValidationError 1abc
5app = FastAPI() 1abc
8class Item(BaseModel): 1abc
9 name: str 1abc
10 tags: list[str] 1abc
13@app.post( 1abc
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): 1abc
23 raw_body = await request.body() 1gjdhkeilf
24 try: 1gjdhkeilf
25 data = yaml.safe_load(raw_body) 1gjdhkeilf
26 except yaml.YAMLError: 1jkl
27 raise HTTPException(status_code=422, detail="Invalid YAML") 1jkl
28 try: 1gdheif
29 item = Item.model_validate(data) 1gdheif
30 except ValidationError as e: 1def
31 raise HTTPException(status_code=422, detail=e.errors(include_url=False)) 1def
32 return item 1ghi