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

22 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.model_rebuild() 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.model_rebuild() 1abcde

28RecursiveItemViaSubmodel.model_rebuild() 1abcde

29 

30 

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

32def get_recursive(): 1abcde

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

34 

35 

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

37def get_recursive_submodel(): 1abcde

38 return { 1abcde

39 "name": "item", 

40 "sub_items1": [ 

41 { 

42 "name": "subitem", 

43 "sub_items2": [ 

44 { 

45 "name": "subsubitem", 

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

47 } 

48 ], 

49 } 

50 ], 

51 }