Skip to content

Commit a50ff1d

Browse files
committed
Fix flask decorator function return other types
1 parent 221d212 commit a50ff1d

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

Diff for: openapi_core/contrib/flask/decorators.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import Type
77

88
from flask.globals import request
9+
from flask.helpers import make_response
910
from flask.wrappers import Request
1011
from flask.wrappers import Response
1112

@@ -74,7 +75,8 @@ def _handle_request_view(
7475
) -> Response:
7576
request = self._get_request()
7677
request.openapi = request_result # type: ignore
77-
return view(*args, **kwargs)
78+
rv = view(*args, **kwargs)
79+
return make_response(rv)
7880

7981
def _handle_request_errors(
8082
self, request_result: RequestValidationResult

Diff for: tests/integration/contrib/flask/data/v3.0/flask_factory.yaml

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@ paths:
1515
type: integer
1616
get:
1717
responses:
18+
404:
19+
description: Return error.
20+
content:
21+
text/html:
22+
schema:
23+
type: string
1824
200:
1925
description: Return the resource.
2026
content:
21-
application/json:
27+
application/json:
2228
schema:
2329
type: object
2430
required:

Diff for: tests/integration/contrib/flask/test_flask_decorator.py

+45-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def test_endpoint_error(self, client):
174174
}
175175
assert result.json == expected_data
176176

177-
def test_valid(self, client):
177+
def test_valid_response_object(self, client):
178178
def view_response_callable(*args, **kwargs):
179179
from flask.globals import request
180180

@@ -197,3 +197,47 @@ def view_response_callable(*args, **kwargs):
197197
assert result.json == {
198198
"data": "data",
199199
}
200+
201+
def test_valid_tuple_str(self, client):
202+
def view_response_callable(*args, **kwargs):
203+
from flask.globals import request
204+
205+
assert request.openapi
206+
assert not request.openapi.errors
207+
assert request.openapi.parameters == Parameters(
208+
path={
209+
"id": 12,
210+
}
211+
)
212+
return ("Not found", 404)
213+
214+
self.view_response_callable = view_response_callable
215+
216+
result = client.get("/browse/12/")
217+
218+
assert result.status_code == 404
219+
assert result.text == "Not found"
220+
221+
def test_valid_tuple_dict(self, client):
222+
def view_response_callable(*args, **kwargs):
223+
from flask.globals import request
224+
225+
assert request.openapi
226+
assert not request.openapi.errors
227+
assert request.openapi.parameters == Parameters(
228+
path={
229+
"id": 12,
230+
}
231+
)
232+
body = dict(data="data")
233+
headers = {"X-Rate-Limit": "12"}
234+
return (body, headers)
235+
236+
self.view_response_callable = view_response_callable
237+
238+
result = client.get("/browse/12/")
239+
240+
assert result.status_code == 200
241+
assert result.json == {
242+
"data": "data",
243+
}

0 commit comments

Comments
 (0)