Replies: 10 comments
-
After further investigation, this could be a pydantic issue, as {
"properties": {
"myfloat": {
"exclusiveMaximum": inf,
"title": "Myfloat",
"type": "number"
}
},
"required": [
"myfloat"
],
"title": "MyModel",
"type": "object"
} The serialization is failing on the |
Beta Was this translation helpful? Give feedback.
-
@vdwees i'd agree it's a pydantic issue. JSON does allow Infinity, and -Infinity as values, I don't know if those are swagger compliant tho. This is valid JSON
|
Beta Was this translation helpful? Give feedback.
-
±Infinity (nor NaN) are definitely not valid JSON. |
Beta Was this translation helpful? Give feedback.
-
@retnikt take a look at pydantic/pydantic#1417 Hope to have this issue fixed soon |
Beta Was this translation helpful? Give feedback.
-
pydantic/pydantic#1422 was merged, should this issue be closed now, or should it wait for a pydantic release? |
Beta Was this translation helpful? Give feedback.
-
Thanks for the discussion here everyone! ☕ I think we can close it here as it would be a Pydantic issue more than FastAPI. And whenever there's a new release, it will be available to be used with FastAPI as well. So I'm gonna close it now, but feel free to add more comments or new issues. |
Beta Was this translation helpful? Give feedback.
-
7 months later, and this still seems to be a problem, i.e.: @router.get('/infinity')
def test_infinity():
return float('inf') still throws As far as I can tell, it looks like the pydantic PR was merged and is part of the current version (1.7.3), but I'm not familiar enough with FastAPI's internals to file a sensible bug elsewhere... any hints? |
Beta Was this translation helpful? Give feedback.
-
@alex-r-bigelow I have the same problem, did you figure out a work-around? |
Beta Was this translation helpful? Give feedback.
-
Not yet; for now I'm making deep copies of objects and manually converting any |
Beta Was this translation helpful? Give feedback.
-
In my case this was because FastAPI was parsing the python object to JSON automatically. If I set the I guess FastAPI default parser will not allow illegal JSON values when parsing, who’d a thought it! |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
Pydantic allows inf, -inf values to be passed to a model. To exclude these, I used the
lt
param:The openapi page looks like this:
And on the server side, a runtime error:
This is a similar issue as #459, except that the invalid value is in the schema itself. The FastAPI.default_response_class is not used to serialize the openapi.json, and this IMO the correct behaviour, but it means the solution proposed in #459 does not apply here. As the model is used in other places outside of fastapi, it does not make sense to remove the
lt
kwarg.Beta Was this translation helpful? Give feedback.
All reactions