Coverage for docs_src / additional_responses / tutorial003_py310.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from fastapi import FastAPI 1abcd

2from fastapi.responses import JSONResponse 1abcd

3from pydantic import BaseModel 1abcd

4 

5 

6class Item(BaseModel): 1abcd

7 id: str 1abcd

8 value: str 1abcd

9 

10 

11class Message(BaseModel): 1abcd

12 message: str 1abcd

13 

14 

15app = FastAPI() 1abcd

16 

17 

18@app.get( 1abcd

19 "/items/{item_id}", 

20 response_model=Item, 

21 responses={ 

22 404: {"model": Message, "description": "The item was not found"}, 

23 200: { 

24 "description": "Item requested by ID", 

25 "content": { 

26 "application/json": { 

27 "example": {"id": "bar", "value": "The bar tenders"} 

28 } 

29 }, 

30 }, 

31 }, 

32) 

33async def read_item(item_id: str): 1abcd

34 if item_id == "foo": 1efghij

35 return {"id": "foo", "value": "there goes my hero"} 1egi

36 else: 

37 return JSONResponse(status_code=404, content={"message": "Item not found"}) 1fhj