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_html,
get_swagger_ui_oauth2_redirect_html, get_swagger_ui_oauth2_redirect_html,
) )
from fastapi.openapi.models import Server
from fastapi.openapi.utils import get_openapi from fastapi.openapi.utils import get_openapi
from fastapi.params import Depends from fastapi.params import Depends
from fastapi.types import DecoratedCallable from fastapi.types import DecoratedCallable
@ -54,7 +55,7 @@ class FastAPI(Starlette):
version: str = "0.1.0", version: str = "0.1.0",
openapi_url: Optional[str] = "/openapi.json", openapi_url: Optional[str] = "/openapi.json",
openapi_tags: Optional[List[Dict[str, Any]]] = None, 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, dependencies: Optional[Sequence[Depends]] = None,
default_response_class: Type[Response] = Default(JSONResponse), default_response_class: Type[Response] = Default(JSONResponse),
docs_url: Optional[str] = "/docs", docs_url: Optional[str] = "/docs",
@ -213,7 +214,7 @@ class FastAPI(Starlette):
def setup(self) -> None: def setup(self) -> None:
if self.openapi_url: 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} server_urls = {url for url in urls if url}
async def openapi(req: Request) -> JSONResponse: 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.dependencies.utils import get_flat_dependant, get_flat_params
from fastapi.encoders import jsonable_encoder from fastapi.encoders import jsonable_encoder
from fastapi.openapi.constants import METHODS_WITH_BODY, REF_PREFIX 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.params import Body, Param
from fastapi.responses import Response from fastapi.responses import Response
from fastapi.utils import ( from fastapi.utils import (
@ -398,7 +398,7 @@ def get_openapi(
description: Optional[str] = None, description: Optional[str] = None,
routes: Sequence[BaseRoute], routes: Sequence[BaseRoute],
tags: Optional[List[Dict[str, Any]]] = None, 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, terms_of_service: Optional[str] = None,
contact: Optional[Dict[str, Union[str, Any]]] = None, contact: Optional[Dict[str, Union[str, Any]]] = None,
license_info: 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 import FastAPI
from fastapi.openapi.models import Server
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
app = FastAPI( app = FastAPI(
servers=[ servers=[
{"url": "/", "description": "Default, relative server"}, {"url": "/", "description": "Default, relative server"},
{ Server(
"url": "http://staging.localhost.tiangolo.com:8000", url="http://staging.localhost.tiangolo.com:8000",
"description": "Staging but actually localhost still", description="Staging but actually localhost still",
}, ),
{"url": "https://prod.example.com"}, {"url": "https://prod.example.com"},
] ]
) )

Loading…
Cancel
Save