You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Can't validate a response whose pydantic validation model contains a date.
To Reproduce
Add a model with a date:
classHistoryDate(BaseModel):
date: date
use it in a GET route
importdatetime@app.route("/api/date_naive_not_working", methods=["GET"])@api.validate(resp=Response(HTTP_200=HistoryDate))defnot_working_date_naive():
historyDate=HistoryDate(date=datetime.date.today())
# will throw not serializable cause flask default encoders are usedreturnjsonify(historyDate)
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.
The text was updated successfully, but these errors were encountered:
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
fixesturner-townsend#23
Describe the bug
Can't validate a response whose pydantic validation model contains a date.
To Reproduce
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):
Python Information (please complete the following information):
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:
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.
The text was updated successfully, but these errors were encountered: