pgjones / quart-schema

Quart-Schema is a Quart extension that provides schema validation and auto-generated API documentation.
MIT License
76 stars 24 forks source link

better handling of status when returning a response #62

Closed catwell closed 10 months ago

catwell commented 11 months ago

This change makes the handling of returned responses work even if an explicit status code is returned as a second argument, i.e. if the return type is tuple[Response, int].

An example of this would be for instance:

@bp.route("/", methods=["PUT"])
@validate_response(MyDataclass, 201)
async def update():
    return jsonify({"status": "created"}), 201

In this case the status code would be mistaken for 200 and the response would be returned as-is without validation.

Moreover, in the case the validator should be transparent because it is a different status code, the current code would ignore the status code and header.

Also (not addressed in this PR but related) in the case where it works, I was surprised that the error returned here is ResponseHeadersValidationError. I could not understand why I got this without reading the source code. In my opinion, since this is a programming error, it would make more sense to use a different error class with a clear error message or even an assertion.

pgjones commented 10 months ago

Thanks