pgjones / quart-schema

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

`response_validation` does not work #90

Open DanielHabenicht opened 2 months ago

DanielHabenicht commented 2 months ago

I don't know if I understand it correctly but this code always returns a (misleading) error if the expected and real status codes of a response match, but allows requests where it does not match:

https://github.com/pgjones/quart-schema/blob/125e218d7a35a2eb9fc8216de2905f5479939dd3/src/quart_schema/validation.py#L225-L228

This however works as expected and validates the body even for the response types:

if isinstance(value, (Response, WerkzeugResponse)):
    value = await value.json

Monkeypatch:

index 3d25e8f..3614ba0 100644
--- a/app/backend/.venv/lib/python3.11/site-packages/quart_schema/validation_old.py
+++ b/app/backend/.venv/lib/python3.11/site-packages/quart_schema/validation.py
@@ -216,10 +216,7 @@ def validate_response(
                 status = value.status_code

             if isinstance(value, (Response, WerkzeugResponse)):
-                if status == status_code:
-                    raise ResponseHeadersValidationError()
-                else:
-                    return result
+                value = await value.json

             if status == status_code:
                 if type(value) == model_class:  # noqa: E721
denaillc commented 3 weeks ago

I'm bumping into the exact same issue and the raise ResponseHeadersValidationError is confusing, for which scenario is it here?

Edit: turns out I was returning a jsonify request to which I attached a cookie, and after checking the docs I don't think quart-schema can validate that. I wanted to both validate the response and set a cookie on the response.