Coverage for tests / test_openapi_query_parameter_extension.py: 100%

17 statements  

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

1from typing import Optional 1abcd

2 

3from fastapi import FastAPI 1abcd

4from fastapi.testclient import TestClient 1abcd

5from inline_snapshot import snapshot 1abcd

6 

7app = FastAPI() 1abcd

8 

9 

10@app.get( 1abcd

11 "/", 

12 openapi_extra={ 

13 "parameters": [ 

14 { 

15 "required": False, 

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

17 "name": "extra_param_1", 

18 "in": "query", 

19 }, 

20 { 

21 "required": True, 

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

23 "name": "extra_param_2", 

24 "in": "query", 

25 }, 

26 ] 

27 }, 

28) 

29def route_with_extra_query_parameters(standard_query_param: Optional[int] = 50): 1abcd

30 return {} 1efg

31 

32 

33client = TestClient(app) 1abcd

34 

35 

36def test_get_route(): 1abcd

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

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

39 assert response.json() == {} 1efg

40 

41 

42def test_openapi(): 1abcd

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

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

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

46 { 

47 "openapi": "3.1.0", 

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

49 "paths": { 

50 "/": { 

51 "get": { 

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

53 "operationId": "route_with_extra_query_parameters__get", 

54 "parameters": [ 

55 { 

56 "required": False, 

57 "schema": { 

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

59 "default": 50, 

60 "title": "Standard Query Param", 

61 }, 

62 "name": "standard_query_param", 

63 "in": "query", 

64 }, 

65 { 

66 "required": False, 

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

68 "name": "extra_param_1", 

69 "in": "query", 

70 }, 

71 { 

72 "required": True, 

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

74 "name": "extra_param_2", 

75 "in": "query", 

76 }, 

77 ], 

78 "responses": { 

79 "200": { 

80 "description": "Successful Response", 

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

82 }, 

83 "422": { 

84 "description": "Validation Error", 

85 "content": { 

86 "application/json": { 

87 "schema": { 

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

89 } 

90 } 

91 }, 

92 }, 

93 }, 

94 } 

95 } 

96 }, 

97 "components": { 

98 "schemas": { 

99 "HTTPValidationError": { 

100 "title": "HTTPValidationError", 

101 "type": "object", 

102 "properties": { 

103 "detail": { 

104 "title": "Detail", 

105 "type": "array", 

106 "items": { 

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

108 }, 

109 } 

110 }, 

111 }, 

112 "ValidationError": { 

113 "title": "ValidationError", 

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

115 "type": "object", 

116 "properties": { 

117 "loc": { 

118 "title": "Location", 

119 "type": "array", 

120 "items": { 

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

122 }, 

123 }, 

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

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

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

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

128 }, 

129 }, 

130 } 

131 }, 

132 } 

133 )