From a9fed276948f64789a614f1afa0f4f8ed88ed5a2 Mon Sep 17 00:00:00 2001 From: Zakharov Denis Date: Mon, 23 Jan 2023 17:08:57 +0300 Subject: [PATCH] fixed "'Server' object has no attribute 'get'" exception --- fastapi/applications.py | 5 +++-- fastapi/openapi/utils.py | 4 ++-- tests/test_openapi_servers.py | 9 +++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/fastapi/applications.py b/fastapi/applications.py index 36dc2605d..4a0e7b74b 100644 --- a/fastapi/applications.py +++ b/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: diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index 86e15b46d..19595ce06 100644 --- a/fastapi/openapi/utils.py +++ b/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, diff --git a/tests/test_openapi_servers.py b/tests/test_openapi_servers.py index a210154f6..1361cbf1d 100644 --- a/tests/test_openapi_servers.py +++ b/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"}, ] )