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

Flask Get Request validate failed #18

Closed
CharmingZhou opened this issue Jan 5, 2021 · 10 comments · Fixed by #25
Closed

Flask Get Request validate failed #18

CharmingZhou opened this issue Jan 5, 2021 · 10 comments · Fixed by #25

Comments

@CharmingZhou
Copy link

CharmingZhou commented Jan 5, 2021

from pydantic import BaseModel
from typing import List
class Query(BaseModel):
    query: str
    
@test_bp.route("/test_route")
@validate(query=Query)
def test_route(query:Query):
    return {}

my code is very simple, but when i send 'http://127.0.0.1:8800/test_route?query=1',I will receive

{
  "validation_error": {
    "query_params": [
      {
        "loc": [
          "query"
        ], 
        "msg": "str type expected", 
        "type": "type_error.str"
      }
    ], 
  }
}

I try add more detail in core.py

try:
                    q = query_model(**query_params)
                except ValidationError as ve:
                    err["query_params"] = ve.errors()
                    err["value"] = query_params

I will get

"value": {
      "query": [
        "1"
      ]
    }

So I'm very doubt why query becomes list rather than str.

My python requrement is

python3.6
pydantic==1.7.3
flask-pydantic-spec==0.1.3
Flask==1.1.2
dataclasses==0.8

Looking forward to a reply!

@bauerji
Copy link
Owner

bauerji commented Jan 5, 2021

Hello,

this is interesting. When I add your code into the example app provided in the repository it works fine.

Could you post your whole code? Also in your requirements there is flask-pydantic-spec==0.1.3 and no flask-pydantic. Maybe there could be some kind of interference with these two libraries.

Jirka

@CharmingZhou
Copy link
Author

sorry,flask-pydantic-spec==0.1.3 is other test library. Right requirement is Flask-Pydantic==0.4.0

@bauerji
Copy link
Owner

bauerji commented Jan 12, 2021

I am not able to reproduce your problem. Could you please share a whole minimal example that still produces the error?

@CharmingZhou
Copy link
Author

I am not able to reproduce your problem. Could you please share a whole minimal example that still produces the error?
Here is a minimal example:

from flask import Flask
from flask import Blueprint

app = Flask(__name__)

from pydantic import BaseModel
from flask_pydantic import validate
from typing import List
class Query(BaseModel):
    query: str
    
test_bp = Blueprint("test",__name__)
@test_bp.route("/test_route", methods=["GET"])
@validate(query=Query)
def test_route(query:Query):
    return {}

if __name__ == "__main__":
    app.register_blueprint(test_bp)
    app.run(host="0.0.0.0", port="8888")

@yasinyildiza
Copy link

yasinyildiza commented Feb 18, 2021

Same thing happened to me.

With a little investigation, I think, I found the root cause.

In convert_query_params, this line

    return {
        **query_params,

should be

    return {
        **query_params.to_dict(flat=True), # flat=True is the default

because when request.args is unpacked, dict values become list rather than str; in other words, explicit cast is done with to_dict(flat=False).

@bauerji
Copy link
Owner

bauerji commented Feb 18, 2021

I am sorry guys, I am running the exact same code as @CharmingZhou posted and still no error (tried both python 3.6 and 3.9). Could You please @yasinyildiza share your minimal example together with your environment setup?

@yasinyildiza
Copy link

yasinyildiza commented Feb 18, 2021

@bauerji

Python
3.6.2

pip freeze

click==7.1.2
dataclasses==0.8
Flask==1.1.2
Flask-Pydantic==0.6.0
itsdangerous==1.1.0
Jinja2==2.11.3
MarkupSafe==1.1.1
pydantic==1.7.3
Werkzeug==1.0.1

code

import flask
import flask_pydantic
import pydantic


class Query(pydantic.BaseModel):
    query: str


app = flask.Flask(__name__)


@app.route('/test')
@flask_pydantic.validate(query=Query)
def test(query:Query):
    return flask.jsonify({'query': query.dict()})


if __name__ == '__main__':
    app.run(port=5000, debug=True)

I also tried to run the tests after setting up the dev environment (following Contributing instructions). This is the result:

18 failed, 55 passed, 1 warning

@bauerji
Copy link
Owner

bauerji commented Feb 18, 2021

@yasinyildiza @CharmingZhou
It seems that there is a problem with specifically python 3.6.2 version. Unfortunately github won't let me to run tests in github actions using this version see this.

I will try to solve this issue, however I would recommend You to upgrade to at least the newest 3.6 version (3.6.13 at the moment).

@bauerji
Copy link
Owner

bauerji commented Feb 18, 2021

@yasinyildiza @CharmingZhou

this issue should not occur anymore since release 0.6.1

Feel free to reopen or create new issue if the problem persists.

Jirka

@yasinyildiza
Copy link

I tested with 0.6.1, and it is working very well.
Thanks a lot @bauerji

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