Coverage for tests / test_openapi_query_parameter_extension.py: 100%

16 statements  

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

1from fastapi import FastAPI 1abcd

2from fastapi.testclient import TestClient 1abcd

3from inline_snapshot import snapshot 1abcd

4 

5app = FastAPI() 1abcd

6 

7 

8@app.get( 1abcd

9 "/", 

10 openapi_extra={ 

11 "parameters": [ 

12 { 

13 "required": False, 

14 "schema": {"title": "Extra Param 1"}, 

15 "name": "extra_param_1", 

16 "in": "query", 

17 }, 

18 { 

19 "required": True, 

20 "schema": {"title": "Extra Param 2"}, 

21 "name": "extra_param_2", 

22 "in": "query", 

23 }, 

24 ] 

25 }, 

26) 

27def route_with_extra_query_parameters(standard_query_param: int | None = 50): 1abcd

28 return {} 1efgh

29 

30 

31client = TestClient(app) 1abcd

32 

33 

34def test_get_route(): 1abcd

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

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

37 assert response.json() == {} 1efgh

38 

39 

40def test_openapi(): 1abcd

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

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

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

44 { 

45 "openapi": "3.1.0", 

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

47 "paths": { 

48 "/": { 

49 "get": { 

50 "summary": "Route With Extra Query Parameters", 

51 "operationId": "route_with_extra_query_parameters__get", 

52 "parameters": [ 

53 { 

54 "required": False, 

55 "schema": { 

56 "anyOf": [{"type": "integer"}, {"type": "null"}], 

57 "default": 50, 

58 "title": "Standard Query Param", 

59 }, 

60 "name": "standard_query_param", 

61 "in": "query", 

62 }, 

63 { 

64 "required": False, 

65 "schema": {"title": "Extra Param 1"}, 

66 "name": "extra_param_1", 

67 "in": "query", 

68 }, 

69 { 

70 "required": True, 

71 "schema": {"title": "Extra Param 2"}, 

72 "name": "extra_param_2", 

73 "in": "query", 

74 }, 

75 ], 

76 "responses": { 

77 "200": { 

78 "description": "Successful Response", 

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

80 }, 

81 "422": { 

82 "description": "Validation Error", 

83 "content": { 

84 "application/json": { 

85 "schema": { 

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

87 } 

88 } 

89 }, 

90 }, 

91 }, 

92 } 

93 } 

94 }, 

95 "components": { 

96 "schemas": { 

97 "HTTPValidationError": { 

98 "title": "HTTPValidationError", 

99 "type": "object", 

100 "properties": { 

101 "detail": { 

102 "title": "Detail", 

103 "type": "array", 

104 "items": { 

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

106 }, 

107 } 

108 }, 

109 }, 

110 "ValidationError": { 

111 "title": "ValidationError", 

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

113 "type": "object", 

114 "properties": { 

115 "loc": { 

116 "title": "Location", 

117 "type": "array", 

118 "items": { 

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

120 }, 

121 }, 

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

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

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

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

126 }, 

127 }, 

128 } 

129 }, 

130 } 

131 )