Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

OpenAPI page cannot load if Schema includes lt=inf [BUG] #1310

Closed
vdwees opened this issue Apr 22, 2020 · 10 comments
Closed

OpenAPI page cannot load if Schema includes lt=inf [BUG] #1310

vdwees opened this issue Apr 22, 2020 · 10 comments

Comments

@vdwees
Copy link

vdwees commented Apr 22, 2020

Describe 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:

image

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.

@vdwees vdwees added the bug Something isn't working label Apr 22, 2020
@vdwees vdwees changed the title OpenAPI page cannot load if schema includes lt=inf, gt=-inf [BUG] OpenAPI page cannot load if Schema includes lt=inf [BUG] Apr 22, 2020
@vdwees
Copy link
Author

vdwees commented Apr 22, 2020

After further investigation, this could be a pydantic issue, as MyModel.schema() is:

{
  "properties": {
    "myfloat": {
      "exclusiveMaximum": inf,
      "title": "Myfloat",
      "type": "number"
    }
  },
  "required": [
    "myfloat"
  ],
  "title": "MyModel",
  "type": "object"
}

The serialization is failing on the inf in the schema. So the error emerges in fastapi, but might best be handled in pydantic. I raised the issue there as well: pydantic/pydantic#1417

@chris-allnutt
Copy link
Contributor

@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

{ "to_infinity_and_beyond": Infinity}

@retnikt
Copy link
Contributor

retnikt commented Apr 28, 2020

±Infinity (nor NaN) are definitely not valid JSON.

@vdwees
Copy link
Author

vdwees commented Apr 29, 2020

@retnikt take a look at pydantic/pydantic#1417

Hope to have this issue fixed soon

@vdwees
Copy link
Author

vdwees commented May 7, 2020

pydantic/pydantic#1422 was merged, should this issue be closed now, or should it wait for a pydantic release?

@tiangolo
Copy link
Owner

tiangolo commented Jun 5, 2020

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.

@tiangolo tiangolo closed this as completed Jun 5, 2020
@alex-r-bigelow
Copy link

7 months later, and this still seems to be a problem, i.e.:

@router.get('/infinity')
def test_infinity():
    return float('inf')

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?

@danieljfarrell
Copy link

@alex-r-bigelow I have the same problem, did you figure out a work-around?

@alex-r-bigelow
Copy link

Not yet; for now I'm making deep copies of objects and manually converting any nan, inf, and -inf values to strings

@danieljfarrell
Copy link

In my case this was because FastAPI was parsing the python object to JSON automatically.

If I set the response_class to be JSONResponse 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!

@tiangolo tiangolo added question Question or problem answered reviewed and removed bug Something isn't working labels Feb 22, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7550 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

6 participants