Coverage for tests/test_validate_response_recursive/app_pv1.py: 100%

21 statements  

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

1from typing import List 1abcde

2 

3from fastapi import FastAPI 1abcde

4from pydantic import BaseModel 1abcde

5 

6app = FastAPI() 1abcde

7 

8 

9class RecursiveItem(BaseModel): 1abcde

10 sub_items: List["RecursiveItem"] = [] 1abcde

11 name: str 1abcde

12 

13 

14RecursiveItem.update_forward_refs() 1abcde

15 

16 

17class RecursiveSubitemInSubmodel(BaseModel): 1abcde

18 sub_items2: List["RecursiveItemViaSubmodel"] = [] 1abcde

19 name: str 1abcde

20 

21 

22class RecursiveItemViaSubmodel(BaseModel): 1abcde

23 sub_items1: List[RecursiveSubitemInSubmodel] = [] 1abcde

24 name: str 1abcde

25 

26 

27RecursiveSubitemInSubmodel.update_forward_refs() 1abcde

28 

29 

30@app.get("/items/recursive", response_model=RecursiveItem) 1abcde

31def get_recursive(): 1abcde

32 return {"name": "item", "sub_items": [{"name": "subitem", "sub_items": []}]} 1abcde

33 

34 

35@app.get("/items/recursive-submodel", response_model=RecursiveItemViaSubmodel) 1abcde

36def get_recursive_submodel(): 1abcde

37 return { 1abcde

38 "name": "item", 

39 "sub_items1": [ 

40 { 

41 "name": "subitem", 

42 "sub_items2": [ 

43 { 

44 "name": "subsubitem", 

45 "sub_items1": [{"name": "subsubsubitem", "sub_items2": []}], 

46 } 

47 ], 

48 } 

49 ], 

50 }