Coverage for tests / test_no_schema_split.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +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 1abcd

6 

7from fastapi import FastAPI 1abcd

8from fastapi.testclient import TestClient 1abcd

9from inline_snapshot import snapshot 1abcd

10from pydantic import BaseModel, Field 1abcd

11 

12 

13class MessageEventType(str, Enum): 1abcd

14 alpha = "alpha" 1abcd

15 beta = "beta" 1abcd

16 

17 

18class MessageEvent(BaseModel): 1abcd

19 event_type: MessageEventType = Field(default=MessageEventType.alpha) 1abcd

20 output: str 1abcd

21 

22 

23class MessageOutput(BaseModel): 1abcd

24 body: str = "" 1abcd

25 events: list[MessageEvent] = [] 1abcd

26 

27 

28class Message(BaseModel): 1abcd

29 input: str 1abcd

30 output: MessageOutput 1abcd

31 

32 

33app = FastAPI(title="Minimal FastAPI App", version="1.0.0") 1abcd

34 

35 

36@app.post("/messages", response_model=Message) 1abcd

37async def create_message(input_message: str) -> Message: 1abcd

38 return Message( 1efg

39 input=input_message, 

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

41 ) 

42 

43 

44client = TestClient(app) 1abcd

45 

46 

47def test_create_message(): 1abcd

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

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

50 assert response.json() == { 1efg

51 "input": "Hello", 

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

53 } 

54 

55 

56def test_openapi_schema(): 1abcd

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

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

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

60 { 

61 "openapi": "3.1.0", 

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

63 "paths": { 

64 "/messages": { 

65 "post": { 

66 "summary": "Create Message", 

67 "operationId": "create_message_messages_post", 

68 "parameters": [ 

69 { 

70 "name": "input_message", 

71 "in": "query", 

72 "required": True, 

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

74 } 

75 ], 

76 "responses": { 

77 "200": { 

78 "description": "Successful Response", 

79 "content": { 

80 "application/json": { 

81 "schema": { 

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

83 } 

84 } 

85 }, 

86 }, 

87 "422": { 

88 "description": "Validation Error", 

89 "content": { 

90 "application/json": { 

91 "schema": { 

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

93 } 

94 } 

95 }, 

96 }, 

97 }, 

98 } 

99 } 

100 }, 

101 "components": { 

102 "schemas": { 

103 "HTTPValidationError": { 

104 "properties": { 

105 "detail": { 

106 "items": { 

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

108 }, 

109 "type": "array", 

110 "title": "Detail", 

111 } 

112 }, 

113 "type": "object", 

114 "title": "HTTPValidationError", 

115 }, 

116 "Message": { 

117 "properties": { 

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

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

120 }, 

121 "type": "object", 

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

123 "title": "Message", 

124 }, 

125 "MessageEvent": { 

126 "properties": { 

127 "event_type": { 

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

129 "default": "alpha", 

130 }, 

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

132 }, 

133 "type": "object", 

134 "required": ["output"], 

135 "title": "MessageEvent", 

136 }, 

137 "MessageEventType": { 

138 "type": "string", 

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

140 "title": "MessageEventType", 

141 }, 

142 "MessageOutput": { 

143 "properties": { 

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

145 "events": { 

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

147 "type": "array", 

148 "title": "Events", 

149 "default": [], 

150 }, 

151 }, 

152 "type": "object", 

153 "title": "MessageOutput", 

154 }, 

155 "ValidationError": { 

156 "properties": { 

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

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

159 "loc": { 

160 "items": { 

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

162 }, 

163 "type": "array", 

164 "title": "Location", 

165 }, 

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

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

168 }, 

169 "type": "object", 

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

171 "title": "ValidationError", 

172 }, 

173 } 

174 }, 

175 } 

176 )