Coverage for tests/test_request_body_parameters_media_type.py: 100%

20 statements  

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

1import typing 1abcde

2 

3from fastapi import Body, FastAPI 1abcde

4from fastapi.testclient import TestClient 1abcde

5from pydantic import BaseModel 1abcde

6 

7app = FastAPI() 1abcde

8 

9media_type = "application/vnd.api+json" 1abcde

10 

11 

12# NOTE: These are not valid JSON:API resources 

13# but they are fine for testing requestBody with custom media_type 

14class Product(BaseModel): 1abcde

15 name: str 1abcde

16 price: float 1abcde

17 

18 

19class Shop(BaseModel): 1abcde

20 name: str 1abcde

21 

22 

23@app.post("/products") 1abcde

24async def create_product(data: Product = Body(media_type=media_type, embed=True)): 1abcde

25 pass # pragma: no cover 

26 

27 

28@app.post("/shops") 1abcde

29async def create_shop( 1abcde

30 data: Shop = Body(media_type=media_type), 

31 included: typing.List[Product] = Body(default=[], media_type=media_type), 

32): 

33 pass # pragma: no cover 

34 

35 

36client = TestClient(app) 1abcde

37 

38 

39def test_openapi_schema(): 1abcde

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

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

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

43 "openapi": "3.1.0", 

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

45 "paths": { 

46 "/products": { 

47 "post": { 

48 "summary": "Create Product", 

49 "operationId": "create_product_products_post", 

50 "requestBody": { 

51 "content": { 

52 "application/vnd.api+json": { 

53 "schema": { 

54 "$ref": "#/components/schemas/Body_create_product_products_post" 

55 } 

56 } 

57 }, 

58 "required": True, 

59 }, 

60 "responses": { 

61 "200": { 

62 "description": "Successful Response", 

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

64 }, 

65 "422": { 

66 "description": "Validation Error", 

67 "content": { 

68 "application/json": { 

69 "schema": { 

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

71 } 

72 } 

73 }, 

74 }, 

75 }, 

76 } 

77 }, 

78 "/shops": { 

79 "post": { 

80 "summary": "Create Shop", 

81 "operationId": "create_shop_shops_post", 

82 "requestBody": { 

83 "content": { 

84 "application/vnd.api+json": { 

85 "schema": { 

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

87 } 

88 } 

89 }, 

90 "required": True, 

91 }, 

92 "responses": { 

93 "200": { 

94 "description": "Successful Response", 

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

96 }, 

97 "422": { 

98 "description": "Validation Error", 

99 "content": { 

100 "application/json": { 

101 "schema": { 

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

103 } 

104 } 

105 }, 

106 }, 

107 }, 

108 } 

109 }, 

110 }, 

111 "components": { 

112 "schemas": { 

113 "Body_create_product_products_post": { 

114 "title": "Body_create_product_products_post", 

115 "required": ["data"], 

116 "type": "object", 

117 "properties": {"data": {"$ref": "#/components/schemas/Product"}}, 

118 }, 

119 "Body_create_shop_shops_post": { 

120 "title": "Body_create_shop_shops_post", 

121 "required": ["data"], 

122 "type": "object", 

123 "properties": { 

124 "data": {"$ref": "#/components/schemas/Shop"}, 

125 "included": { 

126 "title": "Included", 

127 "type": "array", 

128 "items": {"$ref": "#/components/schemas/Product"}, 

129 "default": [], 

130 }, 

131 }, 

132 }, 

133 "HTTPValidationError": { 

134 "title": "HTTPValidationError", 

135 "type": "object", 

136 "properties": { 

137 "detail": { 

138 "title": "Detail", 

139 "type": "array", 

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

141 } 

142 }, 

143 }, 

144 "Product": { 

145 "title": "Product", 

146 "required": ["name", "price"], 

147 "type": "object", 

148 "properties": { 

149 "name": {"title": "Name", "type": "string"}, 

150 "price": {"title": "Price", "type": "number"}, 

151 }, 

152 }, 

153 "Shop": { 

154 "title": "Shop", 

155 "required": ["name"], 

156 "type": "object", 

157 "properties": {"name": {"title": "Name", "type": "string"}}, 

158 }, 

159 "ValidationError": { 

160 "title": "ValidationError", 

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

162 "type": "object", 

163 "properties": { 

164 "loc": { 

165 "title": "Location", 

166 "type": "array", 

167 "items": { 

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

169 }, 

170 }, 

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

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

173 }, 

174 }, 

175 } 

176 }, 

177 }