Coverage for tests / test_duplicate_models_openapi.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from fastapi import FastAPI 1abcd

2from fastapi.testclient import TestClient 1abcd

3from inline_snapshot import snapshot 1abcd

4from pydantic import BaseModel 1abcd

5 

6app = FastAPI() 1abcd

7 

8 

9class Model(BaseModel): 1abcd

10 pass 1abcd

11 

12 

13class Model2(BaseModel): 1abcd

14 a: Model 1abcd

15 

16 

17class Model3(BaseModel): 1abcd

18 c: Model 1abcd

19 d: Model2 1abcd

20 

21 

22@app.get("/", response_model=Model3) 1abcd

23def f(): 1abcd

24 return {"c": {}, "d": {"a": {}}} 1efg

25 

26 

27client = TestClient(app) 1abcd

28 

29 

30def test_get_api_route(): 1abcd

31 response = client.get("/") 1efg

32 assert response.status_code == 200, response.text 1efg

33 assert response.json() == {"c": {}, "d": {"a": {}}} 1efg

34 

35 

36def test_openapi_schema(): 1abcd

37 response = client.get("/openapi.json") 1hij

38 assert response.status_code == 200, response.text 1hij

39 assert response.json() == snapshot( 1hij

40 { 

41 "openapi": "3.1.0", 

42 "info": {"title": "FastAPI", "version": "0.1.0"}, 

43 "paths": { 

44 "/": { 

45 "get": { 

46 "summary": "F", 

47 "operationId": "f__get", 

48 "responses": { 

49 "200": { 

50 "description": "Successful Response", 

51 "content": { 

52 "application/json": { 

53 "schema": { 

54 "$ref": "#/components/schemas/Model3" 

55 } 

56 } 

57 }, 

58 } 

59 }, 

60 } 

61 } 

62 }, 

63 "components": { 

64 "schemas": { 

65 "Model": {"title": "Model", "type": "object", "properties": {}}, 

66 "Model2": { 

67 "title": "Model2", 

68 "required": ["a"], 

69 "type": "object", 

70 "properties": {"a": {"$ref": "#/components/schemas/Model"}}, 

71 }, 

72 "Model3": { 

73 "title": "Model3", 

74 "required": ["c", "d"], 

75 "type": "object", 

76 "properties": { 

77 "c": {"$ref": "#/components/schemas/Model"}, 

78 "d": {"$ref": "#/components/schemas/Model2"}, 

79 }, 

80 }, 

81 } 

82 }, 

83 } 

84 )