0b01001001 / spectree

API spec validator and OpenAPI document generator for Python web frameworks.
https://0b01001001.github.io/spectree/
Apache License 2.0
316 stars 74 forks source link

Fix Flask status code processing after release 1.2.2 #341

Closed AndreiDrang closed 12 months ago

AndreiDrang commented 12 months ago

Following the release of version 1.2.2, the server response's status code generated through "make_response" is no longer being processed accurately. Instead, it is being overwritten with the status code 200.

AndreiDrang commented 12 months ago

Demo example:

from uuid import uuid4
from datetime import timedelta

from flask import Flask, make_response
from pydantic import Field, BaseModel, constr
from spectree import Response, SpecTree

AUTH_COOKIES_CONFIG = {
    "domain": None,
    "secure": False,
    "httponly": False,
    "samesite": None,
    "max_age": timedelta(days=10),
}

class Profile(BaseModel):
    name: constr(min_length=2, max_length=40)  # constrained str
    age: int = Field(..., gt=0, lt=150, description="user age(Human)")

    class Config:
        schema_extra = {
            # provide an example
            "example": {
                "name": "very_important_user",
                "age": 42,
            }
        }

class Message(BaseModel):
    text: str

app = Flask(__name__)
spec = SpecTree(path="docs", backend_name="flask")

@app.route("/api/user", methods=["POST"])
@spec.validate(json=Profile, resp=Response(HTTP_201=Message, HTTP_403=None), tags=["api"])
def user_profile():
    """
    verify user profile (summary of this endpoint)

    user's name, user's age, ... (long description)
    """
    response = make_response(Message(text="it works!").dict(), 201)
    response.set_cookie(
        key="auth_token",
        value=str(uuid4()),
        **AUTH_COOKIES_CONFIG,
    )
    return response

if __name__ == "__main__":
    spec.register(app)  # if you don't register in api init step
    app.run(port=8000)
AndreiDrang commented 12 months ago

@kemingy Hi I think we need rollback to 1.2.1 version or smth like this. Cause after 1.2.2 release make_response additional logic(like set_cookies\headers and etc.) stop working and need some time to fix it.

kemingy commented 12 months ago

@kemingy Hi I think we need rollback to 1.2.1 version or smth like this. Cause after 1.2.2 release make_response additional logic(like set_cookies\headers and etc.) stop working and need some time to fix it.

cc @jean-edouard-boulanger

Let me figure out. Thanks for your feedback.