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.

[BUG] openapi generation crashes for Models with nested `None`

See original GitHub issue

First 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

  1. 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 {}
  1. Run it with uvicorn main:app
  2. Open the browser and call the endpoint /openapi.json.
  3. It raises a 500 Internal Server Error.
  4. 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:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dmontagucommented, Dec 7, 2019

By the way, here is a minimal reproduction of the issue:

from pydantic import BaseModel

class Model(BaseModel):
    state = [None]

Model.schema()

@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.)

0reactions
tiangolocommented, Apr 10, 2020

Awesome, thanks for the discussion here and for implementing the fix in Pydantic! 🙇 👏

Read more comments on GitHub >

github_iconTop Results From Across the Web

asp.net core - Swagger crashes with circular model references
This appears to have been a bug related to the Model Substitution feature. The fix has been published in version 3.1.1 of the...
Read more >
Customization - OpenAPI Generator
If the generator's built-in template is model_docs.mustache and you define model-docs.mustache , this will result in duplicated model docs ...
Read more >
Crashes - Android Developers
Crashes on Android produce a stack trace, which is a snapshot of the sequence of nested functions called in your program up to...
Read more >
Troubleshooting CI/CD - GitLab Docs
This message displays when the YAML configuration is too large or nested too deeply. YAML files with a large number of includes, and...
Read more >
drf-yasg Documentation
Generate real Swagger/OpenAPI 2.0 specifications from a Django Rest Framework API. ... None will force the serializer to be generated as an inline...
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