fastapi: Error generating OpenAPI with generated scheme

Hello!

I apologize in advance for my “English”

When trying to see “swagger” when the server is running without “–reload” parameter, an error appears “Fetch error: Internal Server Error /openapi.json”

It appears only if the documentation contains generated schemes (for example, single parameter with embed=True (as in the example below), or OAuth2PasswordRequestForm dependency).

If you add the option “–reload” to the uvicorn server launch command, the error disappears.

Traceback:

ERROR: Exception in ASGI application
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 375, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/usr/local/lib/python3.7/site-packages/starlette/applications.py", line 133, in __call__
    await self.error_middleware(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/middleware/errors.py", line 177, in __call__
    raise exc from None
  File "/usr/local/lib/python3.7/site-packages/starlette/middleware/errors.py", line 155, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.7/site-packages/starlette/middleware/cors.py", line 76, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/middleware/cors.py", line 76, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/exceptions.py", line 73, in __call__
    raise exc from None
  File "/usr/local/lib/python3.7/site-packages/starlette/exceptions.py", line 62, in __call__
    await self.app(scope, receive, sender)
  File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 590, in __call__
    await route(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 208, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 41, in app
    response = await func(request)
  File "/usr/local/lib/python3.7/site-packages/fastapi/applications.py", line 88, in openapi
    return JSONResponse(self.openapi())
  File "/usr/local/lib/python3.7/site-packages/fastapi/applications.py", line 80, in openapi
    openapi_prefix=self.openapi_prefix,
  File "/usr/local/lib/python3.7/site-packages/fastapi/openapi/utils.py", line 264, in get_openapi
    flat_models=flat_models, model_name_map=model_name_map
  File "/usr/local/lib/python3.7/site-packages/fastapi/utils.py", line 45, in get_model_definitions
    model_name = model_name_map[model]
KeyError: <class 'Body_refresh_access_token_users_refresh_token_post'>

Endpoint view function:

@auth_routes.post(
    '/refresh_token',
    status_code=HTTP_200_OK,
    response_model=AuthTokens
)
def refresh_access_token(
        *,
        refresh_token: str = Body(..., title='Refresh token', embed=True)
):

Response model scheme:

class AuthTokens(BaseModel):
    access_token: str = Schema(..., title='authorization_token')
    token_type: str = 'bearer'
    refresh_token: str = Schema(..., title='refresh token')

Environment: Docker: 19.03.2, build 6a30dfc Docker-compose: 1.23.1, build b02f1306 Used docker image: python:3.7-alpine Python: 3.7.4 FastAPI: 0.38.0

P.S.: I understand that perhaps the swagger is not needed in the production version, but the presence of “–reload” in the docker in any version of the image, except for the development version, in my opinion is bad.

Thank you in advance!

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (6 by maintainers)

Most upvoted comments

@theoturner your issue does not sound like it is related, can you please open a new issue with a reproduction?

@tiangolo, yes, let’s close the issue and if I encounter it later again, I will complement it.

Thank you for participating in my problem.