Coverage for tests/test_no_schema_split.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1# Test with parts from, and to verify the report in: 

2# https://github.com/fastapi/fastapi/discussions/14177 

3# Made an issue in: 

4# https://github.com/fastapi/fastapi/issues/14247 

5from enum import Enum 1abcdefg

6from typing import List 1abcdefg

7 

8from fastapi import FastAPI 1abcdefg

9from fastapi.testclient import TestClient 1abcdefg

10from inline_snapshot import snapshot 1abcdefg

11from pydantic import BaseModel, Field 1abcdefg

12 

13from tests.utils import pydantic_snapshot 1abcdefg

14 

15 

16class MessageEventType(str, Enum): 1abcdefg

17 alpha = "alpha" 1abcdefg

18 beta = "beta" 1abcdefg

19 

20 

21class MessageEvent(BaseModel): 1abcdefg

22 event_type: MessageEventType = Field(default=MessageEventType.alpha) 1abcdefg

23 output: str 1abcdefg

24 

25 

26class MessageOutput(BaseModel): 1abcdefg

27 body: str = "" 1abcdefg

28 events: List[MessageEvent] = [] 1abcdefg

29 

30 

31class Message(BaseModel): 1abcdefg

32 input: str 1abcdefg

33 output: MessageOutput 1abcdefg

34 

35 

36app = FastAPI(title="Minimal FastAPI App", version="1.0.0") 1abcdefg

37 

38 

39@app.post("/messages", response_model=Message) 1abcdefg

40async def create_message(input_message: str) -> Message: 1abcdefg

41 return Message( 1hijklmn

42 input=input_message, 

43 output=MessageOutput(body=f"Processed: {input_message}"), 

44 ) 

45 

46 

47client = TestClient(app) 1abcdefg

48 

49 

50def test_create_message(): 1abcdefg

51 response = client.post("/messages", params={"input_message": "Hello"}) 1hijklmn

52 assert response.status_code == 200, response.text 1hijklmn

53 assert response.json() == { 1hijklmn

54 "input": "Hello", 

55 "output": {"body": "Processed: Hello", "events": []}, 

56 } 

57 

58 

59def test_openapi_schema(): 1abcdefg

60 response = client.get("/openapi.json") 1opqrstu

61 assert response.status_code == 200, response.text 1opqrstu

62 assert response.json() == snapshot( 1opqrstu

63 { 

64 "openapi": "3.1.0", 

65 "info": {"title": "Minimal FastAPI App", "version": "1.0.0"}, 

66 "paths": { 

67 "/messages": { 

68 "post": { 

69 "summary": "Create Message", 

70 "operationId": "create_message_messages_post", 

71 "parameters": [ 

72 { 

73 "name": "input_message", 

74 "in": "query", 

75 "required": True, 

76 "schema": {"type": "string", "title": "Input Message"}, 

77 } 

78 ], 

79 "responses": { 

80 "200": { 

81 "description": "Successful Response", 

82 "content": { 

83 "application/json": { 

84 "schema": { 

85 "$ref": "#/components/schemas/Message" 

86 } 

87 } 

88 }, 

89 }, 

90 "422": { 

91 "description": "Validation Error", 

92 "content": { 

93 "application/json": { 

94 "schema": { 

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

96 } 

97 } 

98 }, 

99 }, 

100 }, 

101 } 

102 } 

103 }, 

104 "components": { 

105 "schemas": { 

106 "HTTPValidationError": { 

107 "properties": { 

108 "detail": { 

109 "items": { 

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

111 }, 

112 "type": "array", 

113 "title": "Detail", 

114 } 

115 }, 

116 "type": "object", 

117 "title": "HTTPValidationError", 

118 }, 

119 "Message": { 

120 "properties": { 

121 "input": {"type": "string", "title": "Input"}, 

122 "output": {"$ref": "#/components/schemas/MessageOutput"}, 

123 }, 

124 "type": "object", 

125 "required": ["input", "output"], 

126 "title": "Message", 

127 }, 

128 "MessageEvent": { 

129 "properties": { 

130 "event_type": pydantic_snapshot( 

131 v2=snapshot( 

132 { 

133 "$ref": "#/components/schemas/MessageEventType", 

134 "default": "alpha", 

135 } 

136 ), 

137 v1=snapshot( 

138 { 

139 "allOf": [ 

140 { 

141 "$ref": "#/components/schemas/MessageEventType" 

142 } 

143 ], 

144 "default": "alpha", 

145 } 

146 ), 

147 ), 

148 "output": {"type": "string", "title": "Output"}, 

149 }, 

150 "type": "object", 

151 "required": ["output"], 

152 "title": "MessageEvent", 

153 }, 

154 "MessageEventType": pydantic_snapshot( 

155 v2=snapshot( 

156 { 

157 "type": "string", 

158 "enum": ["alpha", "beta"], 

159 "title": "MessageEventType", 

160 } 

161 ), 

162 v1=snapshot( 

163 { 

164 "type": "string", 

165 "enum": ["alpha", "beta"], 

166 "title": "MessageEventType", 

167 "description": "An enumeration.", 

168 } 

169 ), 

170 ), 

171 "MessageOutput": { 

172 "properties": { 

173 "body": {"type": "string", "title": "Body", "default": ""}, 

174 "events": { 

175 "items": {"$ref": "#/components/schemas/MessageEvent"}, 

176 "type": "array", 

177 "title": "Events", 

178 "default": [], 

179 }, 

180 }, 

181 "type": "object", 

182 "title": "MessageOutput", 

183 }, 

184 "ValidationError": { 

185 "properties": { 

186 "loc": { 

187 "items": { 

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

189 }, 

190 "type": "array", 

191 "title": "Location", 

192 }, 

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

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

195 }, 

196 "type": "object", 

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

198 "title": "ValidationError", 

199 }, 

200 } 

201 }, 

202 } 

203 )