Browse Source

fixed "'Server' object has no attribute 'get'" exception

pull/5917/head
Zakharov Denis 3 years ago
parent
commit
a9fed27694
  1. 5
      fastapi/applications.py
  2. 4
      fastapi/openapi/utils.py
  3. 9
      tests/test_openapi_servers.py

5
fastapi/applications.py

@ -27,6 +27,7 @@ from fastapi.openapi.docs import (
get_swagger_ui_html,
get_swagger_ui_oauth2_redirect_html,
)
from fastapi.openapi.models import Server
from fastapi.openapi.utils import get_openapi
from fastapi.params import Depends
from fastapi.types import DecoratedCallable
@ -54,7 +55,7 @@ class FastAPI(Starlette):
version: str = "0.1.0",
openapi_url: Optional[str] = "/openapi.json",
openapi_tags: Optional[List[Dict[str, Any]]] = None,
servers: Optional[List[Dict[str, Union[str, Any]]]] = None,
servers: Optional[List[Union[Dict[str, Union[str, Any]], Server]]] = None,
dependencies: Optional[Sequence[Depends]] = None,
default_response_class: Type[Response] = Default(JSONResponse),
docs_url: Optional[str] = "/docs",
@ -213,7 +214,7 @@ class FastAPI(Starlette):
def setup(self) -> None:
if self.openapi_url:
urls = (server_data.get("url") for server_data in self.servers)
urls = (dict(server_data).get("url") for server_data in self.servers)
server_urls = {url for url in urls if url}
async def openapi(req: Request) -> JSONResponse:

4
fastapi/openapi/utils.py

@ -10,7 +10,7 @@ from fastapi.dependencies.models import Dependant
from fastapi.dependencies.utils import get_flat_dependant, get_flat_params
from fastapi.encoders import jsonable_encoder
from fastapi.openapi.constants import METHODS_WITH_BODY, REF_PREFIX
from fastapi.openapi.models import OpenAPI
from fastapi.openapi.models import OpenAPI, Server
from fastapi.params import Body, Param
from fastapi.responses import Response
from fastapi.utils import (
@ -398,7 +398,7 @@ def get_openapi(
description: Optional[str] = None,
routes: Sequence[BaseRoute],
tags: Optional[List[Dict[str, Any]]] = None,
servers: Optional[List[Dict[str, Union[str, Any]]]] = None,
servers: Optional[List[Union[Dict[str, Union[str, Any]], Server]]] = None,
terms_of_service: Optional[str] = None,
contact: Optional[Dict[str, Union[str, Any]]] = None,
license_info: Optional[Dict[str, Union[str, Any]]] = None,

9
tests/test_openapi_servers.py

@ -1,13 +1,14 @@
from fastapi import FastAPI
from fastapi.openapi.models import Server
from fastapi.testclient import TestClient
app = FastAPI(
servers=[
{"url": "/", "description": "Default, relative server"},
{
"url": "http://staging.localhost.tiangolo.com:8000",
"description": "Staging but actually localhost still",
},
Server(
url="http://staging.localhost.tiangolo.com:8000",
description="Staging but actually localhost still",
),
{"url": "https://prod.example.com"},
]
)

Loading…
Cancel
Save