Coverage for tests/test_openapi_query_parameter_extension.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from typing import Optional 1abcde

2 

3from dirty_equals import IsDict 1abcde

4from fastapi import FastAPI 1abcde

5from fastapi.testclient import TestClient 1abcde

6 

7app = FastAPI() 1abcde

8 

9 

10@app.get( 1abcde

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): 1abcde

30 return {} 1abcde

31 

32 

33client = TestClient(app) 1abcde

34 

35 

36def test_get_route(): 1abcde

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

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

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

40 

41 

42def test_openapi(): 1abcde

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

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

45 assert response.json() == { 1abcde

46 "openapi": "3.1.0", 

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

48 "paths": { 

49 "/": { 

50 "get": { 

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

52 "operationId": "route_with_extra_query_parameters__get", 

53 "parameters": [ 

54 { 

55 "required": False, 

56 "schema": IsDict( 

57 { 

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

59 "default": 50, 

60 "title": "Standard Query Param", 

61 } 

62 ) 

63 | IsDict( 

64 # TODO: remove when deprecating Pydantic v1 

65 { 

66 "title": "Standard Query Param", 

67 "type": "integer", 

68 "default": 50, 

69 } 

70 ), 

71 "name": "standard_query_param", 

72 "in": "query", 

73 }, 

74 { 

75 "required": False, 

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

77 "name": "extra_param_1", 

78 "in": "query", 

79 }, 

80 { 

81 "required": True, 

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

83 "name": "extra_param_2", 

84 "in": "query", 

85 }, 

86 ], 

87 "responses": { 

88 "200": { 

89 "description": "Successful Response", 

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

91 }, 

92 "422": { 

93 "description": "Validation Error", 

94 "content": { 

95 "application/json": { 

96 "schema": { 

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

98 } 

99 } 

100 }, 

101 }, 

102 }, 

103 } 

104 } 

105 }, 

106 "components": { 

107 "schemas": { 

108 "HTTPValidationError": { 

109 "title": "HTTPValidationError", 

110 "type": "object", 

111 "properties": { 

112 "detail": { 

113 "title": "Detail", 

114 "type": "array", 

115 "items": {"$ref": "#/components/schemas/ValidationError"}, 

116 } 

117 }, 

118 }, 

119 "ValidationError": { 

120 "title": "ValidationError", 

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

122 "type": "object", 

123 "properties": { 

124 "loc": { 

125 "title": "Location", 

126 "type": "array", 

127 "items": { 

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

129 }, 

130 }, 

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

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

133 }, 

134 }, 

135 } 

136 }, 

137 }