mirror of https://github.com/tiangolo/fastapi.git
Allow custom openapi() to be async
This commit is contained in:
parent
480620372a
commit
079e601db5
|
|
@ -1,3 +1,4 @@
|
|||
import asyncio
|
||||
from enum import Enum
|
||||
from typing import (
|
||||
Any,
|
||||
|
|
@ -1058,7 +1059,10 @@ class FastAPI(Starlette):
|
|||
if root_path and self.root_path_in_servers:
|
||||
self.servers.insert(0, {"url": root_path})
|
||||
server_urls.add(root_path)
|
||||
return JSONResponse(self.openapi())
|
||||
if asyncio.iscoroutinefunction(self.openapi):
|
||||
return JSONResponse(await self.openapi())
|
||||
else:
|
||||
return JSONResponse(self.openapi())
|
||||
|
||||
self.add_route(self.openapi_url, openapi, include_in_schema=False)
|
||||
if self.openapi_url and self.docs_url:
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import pytest
|
||||
from dirty_equals import IsDict
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from .main import app
|
||||
|
|
@ -52,6 +53,18 @@ def test_enum_status_code_response():
|
|||
assert response.json() == "foo bar"
|
||||
|
||||
|
||||
def test_allow_async_openapi():
|
||||
async def async_openapi():
|
||||
return {"foo": "bar"}
|
||||
|
||||
mod_app = FastAPI() # use fresh instance to not affect other tests
|
||||
mod_app.openapi = async_openapi
|
||||
mod_client = TestClient(mod_app)
|
||||
response = mod_client.get("/openapi.json")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"foo": "bar"}
|
||||
|
||||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200, response.text
|
||||
|
|
|
|||
Loading…
Reference in New Issue