Coverage for tests/test_custom_schema_fields.py: 100%

24 statements  

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

1from fastapi import FastAPI 1abcde

2from fastapi._compat import PYDANTIC_V2 1abcde

3from fastapi.testclient import TestClient 1abcde

4from pydantic import BaseModel 1abcde

5 

6app = FastAPI() 1abcde

7 

8 

9class Item(BaseModel): 1abcde

10 name: str 1abcde

11 

12 if PYDANTIC_V2: 1abcde

13 model_config = { 1abcde

14 "json_schema_extra": { 

15 "x-something-internal": {"level": 4}, 

16 } 

17 } 

18 else: 

19 

20 class Config: 1abcde

21 schema_extra = { 1abcde

22 "x-something-internal": {"level": 4}, 

23 } 

24 

25 

26@app.get("/foo", response_model=Item) 1abcde

27def foo(): 1abcde

28 return {"name": "Foo item"} 1abcde

29 

30 

31client = TestClient(app) 1abcde

32 

33 

34item_schema = { 1abcde

35 "title": "Item", 

36 "required": ["name"], 

37 "type": "object", 

38 "x-something-internal": { 

39 "level": 4, 

40 }, 

41 "properties": { 

42 "name": { 

43 "title": "Name", 

44 "type": "string", 

45 } 

46 }, 

47} 

48 

49 

50def test_custom_response_schema(): 1abcde

51 response = client.get("/openapi.json") 1abcde

52 assert response.status_code == 200, response.text 1abcde

53 assert response.json()["components"]["schemas"]["Item"] == item_schema 1abcde

54 

55 

56def test_response(): 1abcde

57 # For coverage 

58 response = client.get("/foo") 1abcde

59 assert response.status_code == 200, response.text 1abcde

60 assert response.json() == {"name": "Foo item"} 1abcde