Skip to content

Commit

Permalink
Fix Flask status code processing after release 1.2.2 (0b01001001#341)
Browse files Browse the repository at this point in the history
* Update flask_plugin.py

* Update flask_plugin.py

* Update __init__.py

* Update dry_plugin_flask.py

* Update test_plugin_flask.py

* Update test_plugin_flask_blueprint.py

* Update test_plugin_flask_view.py

* upd schemas

* fix lint
  • Loading branch information
AndreiDrang authored Sep 22, 2023
1 parent 1228c46 commit 0977c39
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 4 deletions.
2 changes: 2 additions & 0 deletions spectree/plugins/flask_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ def validate(
response_payload, status, *rest = result
else:
response_payload = result[0]
elif isinstance(result, flask.Response):
response_payload, status = result, result.status_code
else:
response_payload = result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,96 @@
"tags": []
}
},
"/api/return_make_response": {
"get": {
"description": "",
"operationId": "get__api_return_make_response",
"parameters": [
{
"description": "",
"in": "query",
"name": "name",
"required": true,
"schema": {
"title": "Name",
"type": "string"
}
},
{
"description": "",
"in": "query",
"name": "limit",
"required": true,
"schema": {
"title": "Limit",
"type": "integer"
}
}
],
"responses": {
"201": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Resp.7068f62"
}
}
},
"description": "Created"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "return_make_response_get <GET>",
"tags": []
},
"post": {
"description": "",
"operationId": "post__api_return_make_response",
"parameters": [],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/JSON.7068f62"
}
}
}
},
"responses": {
"201": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Resp.7068f62"
}
}
},
"description": "Created"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "return_make_response_post <POST>",
"tags": []
}
},
"/api/return_root": {
"get": {
"description": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,96 @@
"tags": []
}
},
"/api/return_make_response": {
"get": {
"description": "",
"operationId": "get__api_return_make_response",
"parameters": [
{
"description": "",
"in": "query",
"name": "name",
"required": true,
"schema": {
"title": "Name",
"type": "string"
}
},
{
"description": "",
"in": "query",
"name": "limit",
"required": true,
"schema": {
"title": "Limit",
"type": "integer"
}
}
],
"responses": {
"201": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Resp.7068f62"
}
}
},
"description": "Created"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "return_make_response_get <GET>",
"tags": []
},
"post": {
"description": "",
"operationId": "post__api_return_make_response",
"parameters": [],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/JSON.7068f62"
}
}
}
},
"responses": {
"201": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Resp.7068f62"
}
}
},
"description": "Created"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "return_make_response_post <POST>",
"tags": []
}
},
"/api/return_root": {
"get": {
"description": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,96 @@
"tags": []
}
},
"/api/return_make_response": {
"get": {
"description": "",
"operationId": "get__api_return_make_response",
"parameters": [
{
"description": "",
"in": "query",
"name": "name",
"required": true,
"schema": {
"title": "Name",
"type": "string"
}
},
{
"description": "",
"in": "query",
"name": "limit",
"required": true,
"schema": {
"title": "Limit",
"type": "integer"
}
}
],
"responses": {
"201": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Resp.7068f62"
}
}
},
"description": "Created"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "get <GET>",
"tags": []
},
"post": {
"description": "",
"operationId": "post__api_return_make_response",
"parameters": [],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/JSON.7068f62"
}
}
}
},
"responses": {
"201": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Resp.7068f62"
}
}
},
"description": "Created"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "post <POST>",
"tags": []
}
},
"/api/user/{name}": {
"post": {
"description": "",
Expand Down
4 changes: 4 additions & 0 deletions tests/flask_imports/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .dry_plugin_flask import (
test_flask_doc,
test_flask_list_json_request,
test_flask_make_response_get,
test_flask_make_response_post,
test_flask_no_response,
test_flask_return_list_request,
test_flask_return_model,
Expand All @@ -20,4 +22,6 @@
"test_flask_upload_file",
"test_flask_list_json_request",
"test_flask_return_list_request",
"test_flask_make_response_post",
"test_flask_make_response_get",
]
23 changes: 22 additions & 1 deletion tests/flask_imports/dry_plugin_flask.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import io
import random

import pytest

from tests.common import UserXmlData
from tests.common import JSON, UserXmlData


@pytest.mark.parametrize("response_format", ["json", "xml"])
Expand Down Expand Up @@ -181,6 +182,26 @@ def test_flask_return_list_request(client, pre_serialize: bool):
]


def test_flask_make_response_post(client):
payload = JSON(
limit=random.randint(1, 10),
name="user make_response name",
)
resp = client.post("/api/return_make_response", json=payload.dict())
assert resp.status_code == 201
assert resp.json == {"name": payload.name, "score": [payload.limit]}


def test_flask_make_response_get(client):
payload = JSON(
limit=random.randint(1, 10),
name="user make_response name",
)
resp = client.get("/api/return_make_response", query_string=payload.dict())
assert resp.status_code == 201
assert resp.json == {"name": payload.name, "score": [payload.limit]}


@pytest.mark.parametrize("pre_serialize", [False, True])
@pytest.mark.parametrize(
"return_what", ["RootResp_JSON", "RootResp_List", "JSON", "List"]
Expand Down
22 changes: 21 additions & 1 deletion tests/test_plugin_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import List

import pytest
from flask import Flask, jsonify, request
from flask import Flask, jsonify, make_response, request

from spectree import Response, SpecTree

Expand Down Expand Up @@ -191,6 +191,26 @@ def return_list():
return [entry.dict() if pre_serialize else entry for entry in data]


@app.route("/api/return_make_response", methods=["POST"])
@api.validate(json=JSON, resp=Response(HTTP_201=Resp))
def return_make_response_post():
model_data = JSON(**request.json)
response = make_response(
Resp(name=model_data.name, score=[model_data.limit]).dict(), 201
)
return response


@app.route("/api/return_make_response", methods=["GET"])
@api.validate(query=JSON, resp=Response(HTTP_201=Resp))
def return_make_response_get():
model_data = JSON(**request.args)
response = make_response(
Resp(name=model_data.name, score=[model_data.limit]).dict(), 201
)
return response


@app.route("/api/return_root", methods=["GET"])
@api.validate(resp=Response(HTTP_200=RootResp))
def return_root():
Expand Down
Loading

0 comments on commit 0977c39

Please sign in to comment.