OpenAPI page cannot load if Schema includes lt=inf [BUG]
See original GitHub issueDescribe the bug
Pydantic allows inf, -inf values to be passed to a model. To exclude these, I used the lt
param:
import math
from pydantic import BaseModel, Field
class MyModel(BaseModel):
myfloat: float = Field(..., lt=math.inf)
The openapi page looks like this:
And on the server side, a runtime error:
...
File "/usr/local/lib/python3.7/site-packages/fastapi/applications.py", line 106, in openapi
return JSONResponse(self.openapi())
File "/usr/local/lib/python3.7/site-packages/starlette/responses.py", line 42, in __init__
self.body = self.render(content)
File "/usr/local/lib/python3.7/site-packages/starlette/responses.py", line 151, in render
separators=(",", ":"),
File "/usr/local/lib/python3.7/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/local/lib/python3.7/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/local/lib/python3.7/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
ValueError: Out of range float values are not JSON compliant
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.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (3 by maintainers)
Top Results From Across the Web
openapi 3.0.0 domains and error given by lack of paths and ...
Problem is that VStudio complains that (among other things) the path and other constraints are not respected. [{ "resource": "/-..-/query-schema ...
Read more >Problem in loading Swagger/OpenAPI Definition
Hi, I'm getting error upon importing Swagger/OpenAPI Definition. Error is "attribute components.schemas.Schema name doesn't adhere to regular.
Read more >Invalid schema causes "should have required property '$ref ...
When an OpenAPI v3 Response Object contains a schema which has invalid schemas, an error is thrown saying: should have a required $ref ......
Read more >OpenAPI Specification - Version 3.0.3 - Swagger
This includes all fields that are used as keys in a map, except where explicitly noted that keys are case insensitive. The schema...
Read more >Using OpenAPI and Swagger UI - Quarkus
Loading OpenAPI Schema From Static Files. Instead of dynamically creating OpenAPI schemas from annotation scanning, Quarkus also supports serving static ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In my case this was because FastAPI was parsing the python object to JSON automatically.
If I set the
response_class
to beJSONResponse
and manually convert the python object to JSON (for example my using Pydantic) then it works fine.I guess FastAPI default parser will not allow illegal JSON values when parsing, who’d a thought it!
7 months later, and this still seems to be a problem, i.e.:
still throws
ValueError: Out of range float values are not JSON compliant
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?