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

[BUG] Date validation #23

Open
paulgirard opened this issue Jan 31, 2022 · 0 comments · May be fixed by #24
Open

[BUG] Date validation #23

paulgirard opened this issue Jan 31, 2022 · 0 comments · May be fixed by #24

Comments

@paulgirard
Copy link

paulgirard commented Jan 31, 2022

Describe the bug
Can't validate a response whose pydantic validation model contains a date.

To Reproduce

  • Add a model with a date:
class HistoryDate(BaseModel):
    date: date
  • use it in a GET route
import datetime
@app.route("/api/date_naive_not_working", methods=["GET"])
@api.validate(resp=Response(HTTP_200=HistoryDate))
def not_working_date_naive():
    historyDate = HistoryDate(date=datetime.date.today())
    # will throw not serializable cause flask default encoders are used
    return jsonify(historyDate)
  • use the route => 500
resp = client.get(
        "/api/date_naive_not_working"
    )
assert resp.status_code == 500

Expected behavior
The response is valid and should be validated.

Error Message
The pydantic model is not JSON serializable

Desktop (please complete the following information):

  • OS: Linux
  • Version Mint, docker debian

Python Information (please complete the following information):

  • Python Version 3.10.1
  • Library Version 0.2.1
  • Other dependencies 2.0.2

Additional context

I am quite new in the pydantic world. Very pleased so far and thank you for this great addition which completely meets my need.

I turned around this as I had the impression to take the lib the wrong way.
Turns out the only workaround, I found is:

@app.route("/api/date_cumbersome", methods=["GET"])
@api.validate(resp=Response(HTTP_200=HistoryDate))
def working_date_validation_cumbersome():
    historyDate = HistoryDate(date=API_DATE_TODAY)
    return jsonify(json.loads(historyDate.json()))

It's actually a pydantic issue in some ways.
There is no other ways (yet) to get a json ready dict (pydantic/pydantic#951 (comment)) to feed the Flask serializer with.

Nevertheless to stumble upon this is quite confusing and frustrating.
I propose a way that does not change anything to this lib but avoid this frustration: allow to pass by flask jsonify method to use pydantic json method instead. I am not very happy with this but let's consider it as a starter for discussion.

I am submitting a PR to expose the problem and my modest proposition.
The PR will have tests which will expose the issue and the workaround of serializing/deserializing.

paulgirard added a commit to paulgirard/flask-pydantic-spec that referenced this issue Jan 31, 2022
- pydantic model is not json serializable through Flask jsonify
- but it's possible to chain pydnatic json -> json loads -> jsonify
- proposition: allow views to return pydantic json directly

fixes turner-townsend#23
@paulgirard paulgirard linked a pull request Jan 31, 2022 that will close this issue
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.

1 participant