From 3f605062ed38bd2ae222fe2a4c0ad47e5078ff29 Mon Sep 17 00:00:00 2001 From: Gary Wilson Jr Date: Wed, 12 Apr 2023 12:54:12 -0500 Subject: [PATCH] Updated spec_url to allow for file in root, without a path prefix, when Configuration path set to None. --- spectree/config.py | 6 ++++-- tests/test_config.py | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/spectree/config.py b/spectree/config.py index 0e0bfd06..58aa5444 100644 --- a/spectree/config.py +++ b/spectree/config.py @@ -65,8 +65,8 @@ class Configuration(BaseSettings): license: Optional[License] = None # SpecTree configurations - #: OpenAPI doc route path prefix (i.e. /apidoc/) - path: str = "apidoc" + #: OpenAPI doc route path prefix (i.e. /apidoc/) or `None` for no path prefix. + path: Optional[str] = "apidoc" #: OpenAPI file route path suffix (i.e. /apidoc/openapi.json) filename: str = "openapi.json" #: OpenAPI version (doesn't affect anything) @@ -117,6 +117,8 @@ def convert_to_lower_case(cls, values: Mapping[str, Any]) -> Dict[str, Any]: @property def spec_url(self) -> str: + if self.path is None: + return f"/{self.filename}" return f"/{self.path}/{self.filename}" def swagger_oauth2_config(self) -> Dict[str, str]: diff --git a/tests/test_config.py b/tests/test_config.py index 17cfb4f8..0bd5c8be 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -60,6 +60,15 @@ def test_config_case(): assert config.title == "Demo" +@pytest.mark.parametrize( + ("path", "expected_url"), + [("prefix", "/prefix/openapi.json"), (None, "/openapi.json")], +) +def test_config_path(path, expected_url): + config = Configuration(path=path) + assert config.spec_url == expected_url + + @pytest.mark.parametrize(("secure_item"), SECURITY_SCHEMAS) def test_update_security_scheme(secure_item: Type[SecurityScheme]): # update and validate each schema type