Skip to content

Path finder returns default server #648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion openapi_core/templating/paths/finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ def _get_servers_iter(
servers = (
path.get("servers", None)
or operation.get("servers", None)
or self.spec.get("servers", [{"url": "/"}])
or self.spec.get("servers", None)
)
if not servers:
servers = [SchemaPath.from_dict({"url": "/"})]
for server in servers:
server_url_pattern = name.rsplit(path_result.resolved, 1)[0]
server_url = server["url"]
Expand Down
90 changes: 85 additions & 5 deletions tests/unit/templating/test_paths_finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,45 @@ def spec(self, info, paths):
class BaseTestServerNotFound:
@pytest.fixture
def servers(self):
return []
return [
SchemaPath.from_dict(
{"url": "http://petstore.swagger.io/resource"}
)
]

@pytest.mark.xfail(
reason="returns default server",
)
def test_raises(self, finder):
method = "get"
full_url = "http://petstore.swagger.io/resource"
full_url = "http://invalidserver/resource"

with pytest.raises(ServerNotFound):
finder.find(method, full_url)


class BaseTestDefaultServer:
@pytest.fixture
def servers(self):
return []

def test_returns_default_server(self, finder, spec):
method = "get"
full_url = "http://petstore.swagger.io/resource"

result = finder.find(method, full_url)

path = spec / "paths" / self.path_name
operation = spec / "paths" / self.path_name / method
server = SchemaPath.from_dict({"url": "/"})
path_result = TemplateResult(self.path_name, {})
server_result = TemplateResult("/", {})
assert result == (
path,
operation,
server,
path_result,
server_result,
)


class BaseTestOperationNotFound:
@pytest.fixture
def operations(self):
Expand Down Expand Up @@ -290,6 +316,15 @@ def test_raises(self, finder):
finder.find(method, full_url)


class TestSpecSimpleServerDefaultServer(
BaseTestDefaultServer,
BaseTestSpecServer,
BaseTestSimplePath,
BaseTestSimpleServer,
):
pass


class TestSpecSimpleServerServerNotFound(
BaseTestServerNotFound,
BaseTestSpecServer,
Expand Down Expand Up @@ -325,6 +360,15 @@ class TestSpecSimpleServerPathsNotFound(
pass


class TestOperationSimpleServerDefaultServer(
BaseTestDefaultServer,
BaseTestOperationServer,
BaseTestSimplePath,
BaseTestSimpleServer,
):
pass


class TestOperationSimpleServerServerNotFound(
BaseTestServerNotFound,
BaseTestOperationServer,
Expand Down Expand Up @@ -360,6 +404,15 @@ class TestOperationSimpleServerPathsNotFound(
pass


class TestPathSimpleServerDefaultServer(
BaseTestDefaultServer,
BaseTestPathServer,
BaseTestSimplePath,
BaseTestSimpleServer,
):
pass


class TestPathSimpleServerServerNotFound(
BaseTestServerNotFound,
BaseTestPathServer,
Expand Down Expand Up @@ -443,6 +496,15 @@ class TestPathSimpleServerVariablePathValid(
pass


class TestSpecVariableServerDefaultServer(
BaseTestDefaultServer,
BaseTestSpecServer,
BaseTestSimplePath,
BaseTestVariableServer,
):
pass


class TestSpecVariableServerServerNotFound(
BaseTestServerNotFound,
BaseTestSpecServer,
Expand Down Expand Up @@ -478,6 +540,15 @@ class TestSpecVariableServerPathsNotFound(
pass


class TestOperationVariableServerDefaultServer(
BaseTestDefaultServer,
BaseTestOperationServer,
BaseTestSimplePath,
BaseTestVariableServer,
):
pass


class TestOperationVariableServerServerNotFound(
BaseTestServerNotFound,
BaseTestOperationServer,
Expand Down Expand Up @@ -513,6 +584,15 @@ class TestOperationVariableServerPathsNotFound(
pass


class TestPathVariableServerDefaultServer(
BaseTestDefaultServer,
BaseTestPathServer,
BaseTestSimplePath,
BaseTestVariableServer,
):
pass


class TestPathVariableServerServerNotFound(
BaseTestServerNotFound,
BaseTestPathServer,
Expand Down