Coverage for tests / test_repeated_dependency_schema.py: 100%

22 statements  

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

1from fastapi import Depends, FastAPI, Header, status 1abcd

2from fastapi.testclient import TestClient 1abcd

3from inline_snapshot import snapshot 1abcd

4 

5app = FastAPI() 1abcd

6 

7 

8def get_header(*, someheader: str = Header()): 1abcd

9 return someheader 1efg

10 

11 

12def get_something_else(*, someheader: str = Depends(get_header)): 1abcd

13 return f"{someheader}123" 1efg

14 

15 

16@app.get("/") 1abcd

17def get_deps(dep1: str = Depends(get_header), dep2: str = Depends(get_something_else)): 1abcd

18 return {"dep1": dep1, "dep2": dep2} 1efg

19 

20 

21client = TestClient(app) 1abcd

22 

23 

24def test_response(): 1abcd

25 response = client.get("/", headers={"someheader": "hello"}) 1efg

26 assert response.status_code == status.HTTP_200_OK 1efg

27 assert response.json() == {"dep1": "hello", "dep2": "hello123"} 1efg

28 

29 

30def test_openapi_schema(): 1abcd

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

32 assert response.status_code == status.HTTP_200_OK 1hij

33 actual_schema = response.json() 1hij

34 assert ( 1hi

35 len(actual_schema["paths"]["/"]["get"]["parameters"]) == 1 

36 ) # primary goal of this test 

37 assert actual_schema == snapshot( 1hij

38 { 

39 "components": { 

40 "schemas": { 

41 "HTTPValidationError": { 

42 "properties": { 

43 "detail": { 

44 "items": { 

45 "$ref": "#/components/schemas/ValidationError" 

46 }, 

47 "title": "Detail", 

48 "type": "array", 

49 } 

50 }, 

51 "title": "HTTPValidationError", 

52 "type": "object", 

53 }, 

54 "ValidationError": { 

55 "properties": { 

56 "ctx": {"title": "Context", "type": "object"}, 

57 "input": {"title": "Input"}, 

58 "loc": { 

59 "items": { 

60 "anyOf": [{"type": "string"}, {"type": "integer"}] 

61 }, 

62 "title": "Location", 

63 "type": "array", 

64 }, 

65 "msg": {"title": "Message", "type": "string"}, 

66 "type": {"title": "Error Type", "type": "string"}, 

67 }, 

68 "required": ["loc", "msg", "type"], 

69 "title": "ValidationError", 

70 "type": "object", 

71 }, 

72 } 

73 }, 

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

75 "openapi": "3.1.0", 

76 "paths": { 

77 "/": { 

78 "get": { 

79 "operationId": "get_deps__get", 

80 "parameters": [ 

81 { 

82 "in": "header", 

83 "name": "someheader", 

84 "required": True, 

85 "schema": {"title": "Someheader", "type": "string"}, 

86 } 

87 ], 

88 "responses": { 

89 "200": { 

90 "content": {"application/json": {"schema": {}}}, 

91 "description": "Successful Response", 

92 }, 

93 "422": { 

94 "content": { 

95 "application/json": { 

96 "schema": { 

97 "$ref": "#/components/schemas/HTTPValidationError" 

98 } 

99 } 

100 }, 

101 "description": "Validation Error", 

102 }, 

103 }, 

104 "summary": "Get Deps", 

105 } 

106 } 

107 }, 

108 } 

109 )