Coverage for docs_src/dataclasses/tutorial003.py: 100%

19 statements  

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

1from dataclasses import field # (1) 1abcdef

2from typing import List, Union 1abcdef

3 

4from fastapi import FastAPI 1abcdef

5from pydantic.dataclasses import dataclass # (2) 1abcdef

6 

7 

8@dataclass 1abcdef

9class Item: 1abcdef

10 name: str 1abcdef

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

12 

13 

14@dataclass 1abcdef

15class Author: 1abcdef

16 name: str 1abcdef

17 items: List[Item] = field(default_factory=list) # (3) 1abcdef

18 

19 

20app = FastAPI() 1abcdef

21 

22 

23@app.post("/authors/{author_id}/items/", response_model=Author) # (4) 1abcdef

24async def create_author_items(author_id: str, items: List[Item]): # (5) 1abcdef

25 return {"name": author_id, "items": items} # (6) 1ghijkl

26 

27 

28@app.get("/authors/", response_model=List[Author]) # (7) 1abcdef

29def get_authors(): # (8) 1abcdef

30 return [ # (9) 1mnopqr

31 { 

32 "name": "Breaters", 

33 "items": [ 

34 { 

35 "name": "Island In The Moon", 

36 "description": "A place to be playin' and havin' fun", 

37 }, 

38 {"name": "Holy Buddies"}, 

39 ], 

40 }, 

41 { 

42 "name": "System of an Up", 

43 "items": [ 

44 { 

45 "name": "Salt", 

46 "description": "The kombucha mushroom people's favorite", 

47 }, 

48 {"name": "Pad Thai"}, 

49 { 

50 "name": "Lonely Night", 

51 "description": "The mostests lonliest nightiest of allest", 

52 }, 

53 ], 

54 }, 

55 ]