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

validate_request with list isn't really working for me #73

Closed rafaelcapucho closed 3 months ago

rafaelcapucho commented 6 months ago

The docs says that it is supported.

Model:

class Position(BaseModel):
    account_number: PositiveInt
    symbol: str
    type: str
    volume: NonNegativeFloat
    profit: float
    swap: NonNegativeFloat
    ticket: PositiveInt

Views:

from quart import Blueprint
from quart_schema import QuartSchema, validate_request, validate_response
from .models import Position

blueprint = Blueprint('positions', __name__, url_prefix='/positions')

@blueprint.route('/add', methods=['POST'])
@validate_request(Position)
async def add(data: Position) -> str:
    return 'add'

@blueprint.route('/add_list', methods=['POST'])
@validate_request(list[Position])
async def add_list(data: list[Position]) -> str:
    return 'add list'

When I test with the /add endpoint, it works: image

But when I test with the /add_list endpoint, it fails: image

I also tested with from typing import List, I got the same result.

Python: 3.12.2 Pip freeze:

/www/site # pip freeze
aiofiles==23.2.1
annotated-types==0.6.0
blinker==1.7.0
certifi==2024.2.2
charset-normalizer==3.3.2
click==8.1.7
Flask==3.0.2
h11==0.14.0
h2==4.1.0
hpack==4.0.0
Hypercorn==0.16.0
hyperframe==6.0.1
idna==3.6
itsdangerous==2.1.2
Jinja2==3.1.3
MarkupSafe==2.1.5
priority==2.0.0
pydantic==2.6.1
pydantic_core==2.16.2
pyhumps==3.8.0
Quart==0.19.4
quart-rate-limiter==0.9.0
quart-redis==2.0.0
quart-schema==0.19.0
redis==4.6.0
requests==2.31.0
setuptools==69.0.3
typing_extensions==4.9.0
urllib3==2.2.0
Werkzeug==3.0.1
wheel==0.42.0
wsproto==1.2.0
pgjones commented 6 months ago

Could you try 9f4455a1363c6edd2b23b898c554e52a9ce6d00f?

denaillc commented 5 months ago

Could you try 9f4455a?

I was not successful using validate_response with a list when the route returned an empty list, until I tried this branch and used List instead of list. Any idea when this will be released? Can I contribute?

rei-ber commented 4 months ago

Same result here with validate_response decorator. That's the traceback I get:

Traceback (most recent call last):                                                                                                                                                                                  
  File "/usr/local/lib/python3.11/site-packages/quart/app.py", line 1376, in handle_request                                                                                                                         
    return await self.full_dispatch_request(request_context)                                                                                                                                                        
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                        
  File "/usr/local/lib/python3.11/site-packages/quart/app.py", line 1414, in full_dispatch_request                                                                                                                  
    result = await self.handle_user_exception(error)                                                                                                                                                                
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                
  File "/usr/local/lib/python3.11/site-packages/quart/app.py", line 1007, in handle_user_exception                                                                                                                  
    raise error                                                                                                                                                                                                     
  File "/usr/local/lib/python3.11/site-packages/quart/app.py", line 1412, in full_dispatch_request                                                                                                                  
    result = await self.dispatch_request(request_context)                                                                                                                                                           
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                           
  File "/usr/local/lib/python3.11/site-packages/quart/app.py", line 1506, in dispatch_request                                                                                                                       
    return await self.ensure_async(handler)(**request_.view_args)                                                                                                                                                   
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                   
  File "/usr/local/lib/python3.11/site-packages/quart_schema/extension.py", line 321, in openapi                                                                                                                    
    return jsonify(_build_openapi_schema(current_app, self))                                                                                                                                                        
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                         
  File "/usr/local/lib/python3.11/site-packages/quart_schema/extension.py", line 657, in _build_openapi_schema                                                        
    built_paths, components = _build_path(func, rule, app)                                                                                                                                                          
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                          
  File "/usr/local/lib/python3.11/site-packages/quart_schema/extension.py", line 490, in _build_path                                                                                                                
    schema = model_schema(                                                                                                                                                                                          
             ^^^^^^^^^^^^^                                                                                                                                                                                          
  File "/usr/local/lib/python3.11/site-packages/quart_schema/conversion.py", line 205, in model_schema                                                                                                              
    or issubclass(model_class, BaseModel)                                                                                                                                                                           
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                           
  File "<frozen abc>", line 123, in __subclasscheck__                                                                                                                                                               
TypeError: issubclass() arg 1 must be a class  

But it worked with your fix and list or List as type; thanks :slightly_smiling_face: Any suggestion of a release with this fix?

pgjones commented 3 months ago

0.20.0 is released