Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation for Array of Models fails #20

Closed
ckwojai opened this issue Jan 28, 2021 · 3 comments · Fixed by #24
Closed

Validation for Array of Models fails #20

ckwojai opened this issue Jan 28, 2021 · 3 comments · Fixed by #24

Comments

@ckwojai
Copy link

ckwojai commented Jan 28, 2021

from typing import List
from flask_pydantic import validate

api = Blueprint("lineage-collector", __name__)

class EntityPayload(BaseModel):
    attributes: dict
    typeName: str

class EntityPayloadBulk(BaseModel):
    __root__:  List[EntityPayload]

@api.route('/entity/bulk', methods=["POST"])
@validate()
def entity_bulk(body: EntityPayloadBulk):
    return "Hello World"

Call to this endpoint returns the following error:

` File "/usr/local/lib/python3.9/site-packages/flask_pydantic/core.py", line 158, in wrapper
b = body_model(**body_params)
TypeError: api.routes.EntityPayloadBulk() argument after ** must be a mapping, not list

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2464, in call
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functionsrule.endpoint
File "/usr/local/lib/python3.9/site-packages/flask_pydantic/core.py", line 164, in wrapper
raise JsonBodyParsingError()
flask_pydantic.exceptions.JsonBodyParsingError`

@bauerji
Copy link
Owner

bauerji commented Jan 29, 2021

Hi,

Thank you for pointing out. To be honest I did not know about pydantic's custom root types using __root__ attribute. I will modify functionality to take care of them.

Jirka

@bauerji
Copy link
Owner

bauerji commented Jan 31, 2021

I added this feature to support as many pydantic's feature as possible.

However I strongly encourage you not to use custom root types.

The preferred way of achieving the same result is using validate's request_body_many attribute

from typing import List
from flask_pydantic import validate

api = Blueprint("lineage-collector", __name__)

class EntityPayload(BaseModel):
    attributes: dict
    typeName: str

@api.route('/entity/bulk', methods=["POST"])
@validate(request_body_many=True)
def entity_bulk(body: EntityPayload):
    return "Hello World"

@pdeep95
Copy link

pdeep95 commented Apr 10, 2021

I think using List[EntityPayload] will be better, like

def entity_bulk(body: List[EntityPayload]):

By using request_body_many=True we get an array of EntityPayload in body, not an object of EntityPayload. This messes up autocomplete in IDEs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants