[BUG] openapi generation crashes for Models with nested `None`
See original GitHub issueFirst off I wanted to say thanks for a great project, took it for a spin, and found this tiny thing. Hope I can help to get it fixed correctly upstream so others don’t run into it 😃
Describe the bug
openapi fails to generate in certain cases – where there is a None inside of another structure for the models exposed via openapi
To Reproduce
- Create a file with:
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Model(BaseModel):
state = [None]
@app.post("/")
def post(model: Model):
return {}
- Run it with
uvicorn main:app
- Open the browser and call the endpoint
/openapi.json
. - It raises a 500 Internal Server Error.
- But I expected it to return a valid openapi.json
Expected behavior
Add a clear and concise description of what you expected to happen.
Logs
Traceback (most recent call last):
File "/home/lutostag/.virtualenvs/fastapi/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 385, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/home/lutostag/.virtualenvs/fastapi/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
return await self.app(scope, receive, send)
File "/home/lutostag/.virtualenvs/fastapi/lib/python3.7/site-packages/fastapi/applications.py", line 140, in __call__
await super().__call__(scope, receive, send)
File "/home/lutostag/.virtualenvs/fastapi/lib/python3.7/site-packages/starlette/applications.py", line 134, in __call__
await self.error_middleware(scope, receive, send)
File "/home/lutostag/.virtualenvs/fastapi/lib/python3.7/site-packages/starlette/middleware/errors.py", line 178, in __call__
raise exc from None
File "/home/lutostag/.virtualenvs/fastapi/lib/python3.7/site-packages/starlette/middleware/errors.py", line 156, in __call__
await self.app(scope, receive, _send)
File "/home/lutostag/.virtualenvs/fastapi/lib/python3.7/site-packages/starlette/exceptions.py", line 73, in __call__
raise exc from None
File "/home/lutostag/.virtualenvs/fastapi/lib/python3.7/site-packages/starlette/exceptions.py", line 62, in __call__
await self.app(scope, receive, sender)
File "/home/lutostag/.virtualenvs/fastapi/lib/python3.7/site-packages/starlette/routing.py", line 590, in __call__
await route(scope, receive, send)
File "/home/lutostag/.virtualenvs/fastapi/lib/python3.7/site-packages/starlette/routing.py", line 208, in __call__
await self.app(scope, receive, send)
File "/home/lutostag/.virtualenvs/fastapi/lib/python3.7/site-packages/starlette/routing.py", line 41, in app
response = await func(request)
File "/home/lutostag/.virtualenvs/fastapi/lib/python3.7/site-packages/fastapi/applications.py", line 97, in openapi
return JSONResponse(self.openapi())
File "/home/lutostag/.virtualenvs/fastapi/lib/python3.7/site-packages/fastapi/applications.py", line 89, in openapi
openapi_prefix=self.openapi_prefix,
File "/home/lutostag/.virtualenvs/fastapi/lib/python3.7/site-packages/fastapi/openapi/utils.py", line 281, in get_openapi
flat_models=flat_models, model_name_map=model_name_map
File "/home/lutostag/.virtualenvs/fastapi/lib/python3.7/site-packages/fastapi/utils.py", line 76, in get_model_definitions
model, model_name_map=model_name_map, ref_prefix=REF_PREFIX
File "pydantic/schema.py", line 465, in pydantic.schema.model_process_schema
File "pydantic/schema.py", line 494, in pydantic.schema.model_type_schema
File "pydantic/schema.py", line 191, in pydantic.schema.field_schema
File "pydantic/schema.py", line 745, in genexpr
File "pydantic/schema.py", line 745, in genexpr
File "pydantic/schema.py", line 749, in pydantic.schema.encode_default
File "pydantic/json.py", line 59, in pydantic.json.pydantic_encoder
TypeError: Object of type 'NoneType' is not JSON serializable
Environment
- OS: Ubuntu 18.04.3
- fastapi version = 0.44.1
- Python 3.7.4
pip freeze
Click==7.0
fastapi==0.44.1
h11==0.8.1
httptools==0.0.13
pydantic==1.2
starlette==0.12.9
uvicorn==0.10.8
uvloop==0.14.0
websockets==8.1
Additional context
I can fix the bug by making the following changes internal to pydantic – https://github.com/samuelcolvin/pydantic/compare/master...lutostag:json_None
I wanted to drop you a line first in case they do not want to extend the code that way, seems like the json_encoder
is supposed to be used as a fallback rather than a first round of coercion. Do you think it is better fixed in this repo, because it is a pydantic internal method being used, and I cannot reproduce the issue with just pydantic on its own?
Thanks again for a great project!
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
By the way, here is a minimal reproduction of the issue:
@lutostag if you’d like to submit a PR to pydantic making the change to
pydantic.schema.encode_default
(and adding a test that the above snippet stops raising an error), that would be great! Otherwise, let us know and I’ll take care of it at some point soon.(If you don’t want to submit a PR it would be great if you could at least copy this issue into a new pydantic issue for tracking.)
Awesome, thanks for the discussion here and for implementing the fix in Pydantic! 🙇 👏