Debian Codegraph @

tests/test_no_schema_split.py

raw
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
5 from enum import Enum
6
7 from fastapi import FastAPI
8 from fastapi.testclient import TestClient
9 from inline_snapshot import snapshot
10 from pydantic import BaseModel, Field
11
12
13 class MessageEventType(str, Enum):
14 alpha = "alpha"
15 beta = "beta"
16
17
18 class MessageEvent(BaseModel):
19 event_type: MessageEventType = Field(default=MessageEventType.alpha)
20 output: str
21
22
23 class MessageOutput(BaseModel):
24 body: str = ""
25 events: list[MessageEvent] = []
26
27
28 class Message(BaseModel):
29 input: str
30 output: MessageOutput
31
32
33 app = FastAPI(title="Minimal FastAPI App", version="1.0.0")
34
35
36 @app.post("/messages", response_model=Message)
37 async def create_message(input_message: str) -> Message:
38 return Message(
39 input=input_message,
40 output=MessageOutput(body=f"Processed: {input_message}"),
41 )
42
43
44 client = TestClient(app)
45
46
47 def test_create_message():
48 response = client.post("/messages", params={"input_message": "Hello"})
49 assert response.status_code == 200, response.text
50 assert response.json() == {
51 "input": "Hello",
52 "output": {"body": "Processed: Hello", "events": []},
53 }
54
55
56 def test_openapi_schema():
57 response = client.get("/openapi.json")
58 assert response.status_code == 200, response.text
59 assert response.json() == snapshot(
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 )