Coverage for tests / test_duplicate_models_openapi.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1from fastapi import FastAPI 1adbc

2from fastapi.testclient import TestClient 1adbc

3from inline_snapshot import snapshot 1adbc

4from pydantic import BaseModel 1adbc

5 

6app = FastAPI() 1adbc

7 

8 

9class Model(BaseModel): 1adbc

10 pass 1adbc

11 

12 

13class Model2(BaseModel): 1adbc

14 a: Model 1abc

15 

16 

17class Model3(BaseModel): 1adbc

18 c: Model 1abc

19 d: Model2 1abc

20 

21 

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

23def f(): 1adbc

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

25 

26 

27client = TestClient(app) 1adbc

28 

29 

30def test_get_api_route(): 1adbc

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

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

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

34 

35 

36def test_openapi_schema(): 1adbc

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

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

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

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 )