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

13 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1from typing import Union 1abcdef

2 

3from fastapi import FastAPI 1abcdef

4from fastapi.responses import FileResponse 1abcdef

5from pydantic import BaseModel 1abcdef

6 

7 

8class Item(BaseModel): 1abcdef

9 id: str 1abcdef

10 value: str 1abcdef

11 

12 

13app = FastAPI() 1abcdef

14 

15 

16@app.get( 1abcdef

17 "/items/{item_id}", 

18 response_model=Item, 

19 responses={ 

20 200: { 

21 "content": {"image/png": {}}, 

22 "description": "Return the JSON item or an image.", 

23 } 

24 }, 

25) 

26async def read_item(item_id: str, img: Union[bool, None] = None): 1abcdef

27 if img: 1ghijklmnopqr

28 return FileResponse("image.png", media_type="image/png") 1hjlnpr

29 else: 

30 return {"id": "foo", "value": "there goes my hero"} 1gikmoq