Skip to content

Commit

Permalink
Updated spec_url to allow for file in root, without a path prefix, wh…
Browse files Browse the repository at this point in the history
…en Configuration path set to None.
  • Loading branch information
gdub committed Apr 12, 2023
1 parent 6af3ce7 commit 3f60506
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 4 additions & 2 deletions spectree/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3f60506

Please sign in to comment.