# Buggy code
def __init__(
    self,
    path: str,
    endpoint: Callable,
    *,
    response_model: Type[Any] = None,
    status_code: int = 200,
    tags: List[str] = None,
    dependencies: Sequence[params.Depends] = None,
    summary: str = None,
    description: str = None,
    response_description: str = "Successful Response",
    responses: Dict[Union[int, str], Dict[str, Any]] = None,
    deprecated: bool = None,
    name: str = None,
    methods: Optional[Union[Set[str], List[str]]] = None,
    operation_id: str = None,
    response_model_include: Set[str] = None,
    response_model_exclude: Set[str] = set(),
    response_model_by_alias: bool = True,
    response_model_skip_defaults: bool = False,
    include_in_schema: bool = True,
    response_class: Type[Response] = JSONResponse,
    dependency_overrides_provider: Any = None,
) -> None:
    assert path.startswith("/"), "Routed paths must always start with '/'"
    self.path = path
    self.endpoint = endpoint
    self.name = get_name(endpoint) if name is None else name
    self.response_model = response_model
    if self.response_model:
        assert lenient_issubclass(response_class, JSONResponse), (
            "To declare a type the response must be a JSON response"
        )
        response_name = "Response_" + self.name
        self.response_field: Optional[Field] = Field(
            name=response_name,
            type_=self.response_model,
            class_validators={},
            default=None,
            required=False,
            model_config=BaseConfig,
            schema=Schema(None),
        )
        # Create a clone of the field, so that a Pydantic submodel is not returned
        # as is just because it's an instance of a subclass of a more limited class
        # e.g. UserInDB (containing hashed_password) could be a subclass of User
        # that doesn't have the hashed_password. But because it's a subclass, it
        # would pass the validation and be returned as is.
        # By being a new field, no inheritance will be passed as is. A new model
        # will be always created.
        self.secure_cloned_response_field: Optional[Field] = create_cloned_field(
            self.response_field
        )
    else:
        self.response_field = None
        self.secure_cloned_response_field = None
    self.status_code = status_code
    self.tags = tags or []
    if dependencies:
        self.dependencies = list(dependencies)
    else:
        self.dependencies = []
    self.summary = summary
    self.description = description or inspect.cleandoc(self.endpoint.__doc__ or "")
    self.response_description = response_description
    self.responses = responses or {}
    response_fields = {}
    for additional_status_code, response in self.responses.items():
        assert isinstance(response, dict), "An additional response must be a dict"
        model = response.get("model")
        if model:
            assert lenient_issubclass(model, BaseModel), (
                "A response model must be a Pydantic model"
            )
            response_name = f"Response_{additional_status_code}_{self.name}"
            response_field = Field(
                name=response_name,
                type_=model,
                class_validators=None,
                default=None,
                required=False,
                model_config=BaseConfig,
                schema=Schema(None),
            )
            response_fields[additional_status_code] = response_field
    if response_fields:
        self.response_fields: Dict[Union[int, str], Field] = response_fields
    else:
        self.response_fields = {}
    self.deprecated = deprecated
    if methods is None:
        methods = ["GET"]
    self.methods = set([method.upper() for method in methods])
    self.operation_id = operation_id
    self.response_model_include = response_model_include
    self.response_model_exclude = response_model_exclude
    self.response_model_by_alias = response_model_by_alias
    self.response_model_skip_defaults = response_model_skip_defaults
    self.include_in_schema = include_in_schema
    self.response_class = response_class

    self.path_regex, self.path_format, self.param_convertors = compile_path(path)
    assert inspect.isfunction(endpoint) or inspect.ismethod(endpoint), (
        f"An endpoint must be a function or method"
    )
    self.dependant = get_dependant(path=self.path_format, call=self.endpoint)
    for depends in self.dependencies[::-1]:
        self.dependant.dependencies.insert(
            0,
            get_parameterless_sub_dependant(depends=depends, path=self.path_format),
        )
    self.body_field = get_body_field(dependant=self.dependant, name=self.name)
    self.dependency_overrides_provider = dependency_overrides_provider
    self.app = request_response(
        get_app(
            dependant=self.dependant,
            body_field=self.body_field,
            status_code=self.status_code,
            response_class=self.response_class,
            response_field=self.secure_cloned_response_field,
            response_model_include=self.response_model_include,
            response_model_exclude=self.response_model_exclude,
            response_model_by_alias=self.response_model_by_alias,
            response_model_skip_defaults=self.response_model_skip_defaults,
            dependency_overrides_provider=self.dependency_overrides_provider,
        )
    )


# Traceback (KeyError)
ip-10-8-0-198% % uvicorn run:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [77186]
WARNING:root:email-validator not installed, email fields will be treated as str.
To install, run: pip install email-validator
INFO:uvicorn:Started server process [77188]
INFO:uvicorn:Waiting for application startup.
INFO:uvicorn:('127.0.0.1', 54932) - "GET /docs HTTP/1.1" 200
INFO:uvicorn:('127.0.0.1', 54932) - "GET /openapi.json HTTP/1.1" 500
ERROR:uvicorn:Exception in ASGI application
Traceback (most recent call last):
File "/Users/edwardbanner/.anaconda/lib/python3.6/site-packages/uvicorn/protocols/http/httptools_impl.py", line 368, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/Users/edwardbanner/.anaconda/lib/python3.6/site-packages/uvicorn/middleware/asgi2.py", line 7, in __call__
await instance(receive, send)
File "/Users/edwardbanner/.anaconda/lib/python3.6/site-packages/starlette/middleware/errors.py", line 125, in asgi
raise exc from None
File "/Users/edwardbanner/.anaconda/lib/python3.6/site-packages/starlette/middleware/errors.py", line 103, in asgi
await asgi(receive, _send)
File "/Users/edwardbanner/.anaconda/lib/python3.6/site-packages/starlette/exceptions.py", line 74, in app
raise exc from None
File "/Users/edwardbanner/.anaconda/lib/python3.6/site-packages/starlette/exceptions.py", line 63, in app
await instance(receive, sender)
File "/Users/edwardbanner/.anaconda/lib/python3.6/site-packages/starlette/routing.py", line 43, in awaitable
response = await run_in_threadpool(func, request)
File "/Users/edwardbanner/.anaconda/lib/python3.6/site-packages/starlette/concurrency.py", line 24, in run_in_threadpool
return await loop.run_in_executor(None, func, *args)
File "/Users/edwardbanner/.anaconda/lib/python3.6/concurrent/futures/thread.py", line 56, in run
result = self.fn(*self.args, **self.kwargs)
File "/Users/edwardbanner/.anaconda/lib/python3.6/site-packages/fastapi/applications.py", line 83, in <lambda>
lambda req: JSONResponse(self.openapi()),
File "/Users/edwardbanner/.anaconda/lib/python3.6/site-packages/fastapi/applications.py", line 75, in openapi
openapi_prefix=self.openapi_prefix,
File "/Users/edwardbanner/.anaconda/lib/python3.6/site-packages/fastapi/openapi/utils.py", line 248, in get_openapi
flat_models=flat_models, model_name_map=model_name_map
File "/Users/edwardbanner/.anaconda/lib/python3.6/site-packages/fastapi/utils.py", line 45, in get_model_definitions
model_name = model_name_map[model]
KeyError: <class 'Body_compute'>

# Fixed code
def __init__(
    self,
    path: str,
    endpoint: Callable,
    *,
    response_model: Type[Any] = None,
    status_code: int = 200,
    tags: List[str] = None,
    dependencies: Sequence[params.Depends] = None,
    summary: str = None,
    description: str = None,
    response_description: str = "Successful Response",
    responses: Dict[Union[int, str], Dict[str, Any]] = None,
    deprecated: bool = None,
    name: str = None,
    methods: Optional[Union[Set[str], List[str]]] = None,
    operation_id: str = None,
    response_model_include: Set[str] = None,
    response_model_exclude: Set[str] = set(),
    response_model_by_alias: bool = True,
    response_model_skip_defaults: bool = False,
    include_in_schema: bool = True,
    response_class: Type[Response] = JSONResponse,
    dependency_overrides_provider: Any = None,
) -> None:
    assert path.startswith("/"), "Routed paths must always start with '/'"
    self.path = path
    self.endpoint = endpoint
    self.name = get_name(endpoint) if name is None else name
    self.path_regex, self.path_format, self.param_convertors = compile_path(path)
    if methods is None:
        methods = ["GET"]
    self.methods = set([method.upper() for method in methods])
    self.unique_id = generate_operation_id_for_path(
        name=self.name, path=self.path_format, method=list(methods)[0]
    )
    self.response_model = response_model
    if self.response_model:
        assert lenient_issubclass(response_class, JSONResponse), (
            "To declare a type the response must be a JSON response"
        )
        response_name = "Response_" + self.unique_id
        self.response_field: Optional[Field] = Field(
            name=response_name,
            type_=self.response_model,
            class_validators={},
            default=None,
            required=False,
            model_config=BaseConfig,
            schema=Schema(None),
        )
        # Create a clone of the field, so that a Pydantic submodel is not returned
        # as is just because it's an instance of a subclass of a more limited class
        # e.g. UserInDB (containing hashed_password) could be a subclass of User
        # that doesn't have the hashed_password. But because it's a subclass, it
        # would pass the validation and be returned as is.
        # By being a new field, no inheritance will be passed as is. A new model
        # will be always created.
        self.secure_cloned_response_field: Optional[Field] = create_cloned_field(
            self.response_field
        )
    else:
        self.response_field = None
        self.secure_cloned_response_field = None
    self.status_code = status_code
    self.tags = tags or []
    if dependencies:
        self.dependencies = list(dependencies)
    else:
        self.dependencies = []
    self.summary = summary
    self.description = description or inspect.cleandoc(self.endpoint.__doc__ or "")
    self.response_description = response_description
    self.responses = responses or {}
    response_fields = {}
    for additional_status_code, response in self.responses.items():
        assert isinstance(response, dict), "An additional response must be a dict"
        model = response.get("model")
        if model:
            assert lenient_issubclass(model, BaseModel), (
                "A response model must be a Pydantic model"
            )
            response_name = f"Response_{additional_status_code}_{self.unique_id}"
            response_field = Field(
                name=response_name,
                type_=model,
                class_validators=None,
                default=None,
                required=False,
                model_config=BaseConfig,
                schema=Schema(None),
            )
            response_fields[additional_status_code] = response_field
    if response_fields:
        self.response_fields: Dict[Union[int, str], Field] = response_fields
    else:
        self.response_fields = {}
    self.deprecated = deprecated
    self.operation_id = operation_id
    self.response_model_include = response_model_include
    self.response_model_exclude = response_model_exclude
    self.response_model_by_alias = response_model_by_alias
    self.response_model_skip_defaults = response_model_skip_defaults
    self.include_in_schema = include_in_schema
    self.response_class = response_class

    assert inspect.isfunction(endpoint) or inspect.ismethod(endpoint), (
        f"An endpoint must be a function or method"
    )
    self.dependant = get_dependant(path=self.path_format, call=self.endpoint)
    for depends in self.dependencies[::-1]:
        self.dependant.dependencies.insert(
            0,
            get_parameterless_sub_dependant(depends=depends, path=self.path_format),
        )
    self.body_field = get_body_field(dependant=self.dependant, name=self.unique_id)
    self.dependency_overrides_provider = dependency_overrides_provider
    self.app = request_response(
        get_app(
            dependant=self.dependant,
            body_field=self.body_field,
            status_code=self.status_code,
            response_class=self.response_class,
            response_field=self.secure_cloned_response_field,
            response_model_include=self.response_model_include,
            response_model_exclude=self.response_model_exclude,
            response_model_by_alias=self.response_model_by_alias,
            response_model_skip_defaults=self.response_model_skip_defaults,
            dependency_overrides_provider=self.dependency_overrides_provider,
        )
    )