Coverage for docs_src/response_model/tutorial006.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from typing import Union 1abcde

2 

3from fastapi import FastAPI 1abcde

4from pydantic import BaseModel 1abcde

5 

6app = FastAPI() 1abcde

7 

8 

9class Item(BaseModel): 1abcde

10 name: str 1abcde

11 description: Union[str, None] = None 1abcde

12 price: float 1abcde

13 tax: float = 10.5 1abcde

14 

15 

16items = { 1abcde

17 "foo": {"name": "Foo", "price": 50.2}, 

18 "bar": {"name": "Bar", "description": "The Bar fighters", "price": 62, "tax": 20.2}, 

19 "baz": { 

20 "name": "Baz", 

21 "description": "There goes my baz", 

22 "price": 50.2, 

23 "tax": 10.5, 

24 }, 

25} 

26 

27 

28@app.get( 1abcde

29 "/items/{item_id}/name", 

30 response_model=Item, 

31 response_model_include=["name", "description"], 

32) 

33async def read_item_name(item_id: str): 1abcde

34 return items[item_id] 1abcde

35 

36 

37@app.get("/items/{item_id}/public", response_model=Item, response_model_exclude=["tax"]) 1abcde

38async def read_item_public_data(item_id: str): 1abcde

39 return items[item_id] 1abcde