question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
danieljfarrellcommented, Feb 18, 2021

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!

1reaction
alex-r-bigelowcommented, Feb 4, 2021

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found