Coverage for tests / test_param_include_in_schema.py: 100%
46 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1from typing import Optional 1abcd
3import pytest 1abcd
4from fastapi import Cookie, FastAPI, Header, Path, Query 1abcd
5from fastapi.testclient import TestClient 1abcd
6from inline_snapshot import snapshot 1abcd
8app = FastAPI() 1abcd
11@app.get("/hidden_cookie") 1abcd
12async def hidden_cookie( 1abcd
13 hidden_cookie: Optional[str] = Cookie(default=None, include_in_schema=False),
14):
15 return {"hidden_cookie": hidden_cookie} 1efg
18@app.get("/hidden_header") 1abcd
19async def hidden_header( 1abcd
20 hidden_header: Optional[str] = Header(default=None, include_in_schema=False),
21):
22 return {"hidden_header": hidden_header} 1hij
25@app.get("/hidden_path/{hidden_path}") 1abcd
26async def hidden_path(hidden_path: str = Path(include_in_schema=False)): 1abcd
27 return {"hidden_path": hidden_path} 1klm
30@app.get("/hidden_query") 1abcd
31async def hidden_query( 1abcd
32 hidden_query: Optional[str] = Query(default=None, include_in_schema=False),
33):
34 return {"hidden_query": hidden_query} 1nop
37@pytest.mark.parametrize( 1abcd
38 "path,cookies,expected_status,expected_response",
39 [
40 (
41 "/hidden_cookie",
42 {},
43 200,
44 {"hidden_cookie": None},
45 ),
46 (
47 "/hidden_cookie",
48 {"hidden_cookie": "somevalue"},
49 200,
50 {"hidden_cookie": "somevalue"},
51 ),
52 ],
53)
54def test_hidden_cookie(path, cookies, expected_status, expected_response): 1abcd
55 client = TestClient(app, cookies=cookies) 1efg
56 response = client.get(path) 1efg
57 assert response.status_code == expected_status 1efg
58 assert response.json() == expected_response 1efg
61@pytest.mark.parametrize( 1abcd
62 "path,headers,expected_status,expected_response",
63 [
64 (
65 "/hidden_header",
66 {},
67 200,
68 {"hidden_header": None},
69 ),
70 (
71 "/hidden_header",
72 {"Hidden-Header": "somevalue"},
73 200,
74 {"hidden_header": "somevalue"},
75 ),
76 ],
77)
78def test_hidden_header(path, headers, expected_status, expected_response): 1abcd
79 client = TestClient(app) 1hij
80 response = client.get(path, headers=headers) 1hij
81 assert response.status_code == expected_status 1hij
82 assert response.json() == expected_response 1hij
85def test_hidden_path(): 1abcd
86 client = TestClient(app) 1klm
87 response = client.get("/hidden_path/hidden_path") 1klm
88 assert response.status_code == 200 1klm
89 assert response.json() == {"hidden_path": "hidden_path"} 1klm
92@pytest.mark.parametrize( 1abcd
93 "path,expected_status,expected_response",
94 [
95 (
96 "/hidden_query",
97 200,
98 {"hidden_query": None},
99 ),
100 (
101 "/hidden_query?hidden_query=somevalue",
102 200,
103 {"hidden_query": "somevalue"},
104 ),
105 ],
106)
107def test_hidden_query(path, expected_status, expected_response): 1abcd
108 client = TestClient(app) 1nop
109 response = client.get(path) 1nop
110 assert response.status_code == expected_status 1nop
111 assert response.json() == expected_response 1nop
114def test_openapi_schema(): 1abcd
115 client = TestClient(app) 1qrs
116 response = client.get("/openapi.json") 1qrs
117 assert response.status_code == 200 1qrs
118 assert response.json() == snapshot( 1qrs
119 {
120 "openapi": "3.1.0",
121 "info": {"title": "FastAPI", "version": "0.1.0"},
122 "paths": {
123 "/hidden_cookie": {
124 "get": {
125 "summary": "Hidden Cookie",
126 "operationId": "hidden_cookie_hidden_cookie_get",
127 "responses": {
128 "200": {
129 "description": "Successful Response",
130 "content": {"application/json": {"schema": {}}},
131 },
132 "422": {
133 "description": "Validation Error",
134 "content": {
135 "application/json": {
136 "schema": {
137 "$ref": "#/components/schemas/HTTPValidationError"
138 }
139 }
140 },
141 },
142 },
143 }
144 },
145 "/hidden_header": {
146 "get": {
147 "summary": "Hidden Header",
148 "operationId": "hidden_header_hidden_header_get",
149 "responses": {
150 "200": {
151 "description": "Successful Response",
152 "content": {"application/json": {"schema": {}}},
153 },
154 "422": {
155 "description": "Validation Error",
156 "content": {
157 "application/json": {
158 "schema": {
159 "$ref": "#/components/schemas/HTTPValidationError"
160 }
161 }
162 },
163 },
164 },
165 }
166 },
167 "/hidden_path/{hidden_path}": {
168 "get": {
169 "summary": "Hidden Path",
170 "operationId": "hidden_path_hidden_path__hidden_path__get",
171 "responses": {
172 "200": {
173 "description": "Successful Response",
174 "content": {"application/json": {"schema": {}}},
175 },
176 "422": {
177 "description": "Validation Error",
178 "content": {
179 "application/json": {
180 "schema": {
181 "$ref": "#/components/schemas/HTTPValidationError"
182 }
183 }
184 },
185 },
186 },
187 }
188 },
189 "/hidden_query": {
190 "get": {
191 "summary": "Hidden Query",
192 "operationId": "hidden_query_hidden_query_get",
193 "responses": {
194 "200": {
195 "description": "Successful Response",
196 "content": {"application/json": {"schema": {}}},
197 },
198 "422": {
199 "description": "Validation Error",
200 "content": {
201 "application/json": {
202 "schema": {
203 "$ref": "#/components/schemas/HTTPValidationError"
204 }
205 }
206 },
207 },
208 },
209 }
210 },
211 },
212 "components": {
213 "schemas": {
214 "HTTPValidationError": {
215 "title": "HTTPValidationError",
216 "type": "object",
217 "properties": {
218 "detail": {
219 "title": "Detail",
220 "type": "array",
221 "items": {
222 "$ref": "#/components/schemas/ValidationError"
223 },
224 }
225 },
226 },
227 "ValidationError": {
228 "title": "ValidationError",
229 "required": ["loc", "msg", "type"],
230 "type": "object",
231 "properties": {
232 "loc": {
233 "title": "Location",
234 "type": "array",
235 "items": {
236 "anyOf": [{"type": "string"}, {"type": "integer"}]
237 },
238 },
239 "msg": {"title": "Message", "type": "string"},
240 "type": {"title": "Error Type", "type": "string"},
241 "input": {"title": "Input"},
242 "ctx": {"title": "Context", "type": "object"},
243 },
244 },
245 }
246 },
247 }
248 )