Skip to content

Commit

Permalink
fix: KeyError instead of OperationNotFound when the operation ID …
Browse files Browse the repository at this point in the history
…is not found in Open API 3.1 without path entries

Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
  • Loading branch information
Stranger6667 committed May 15, 2024
1 parent 972cb6a commit 4913d6b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Changelog
- Not serializing shared parameters for an API operation.
- ``OperationNotFound`` raised in ``schema.get_operation_by_id`` if the relevant path item is behind a reference.
- Missing parameters shared under the same path in stateful testing if the path is behind a reference.
- ``KeyError`` instead of ``OperationNotFound`` when the operation ID is not found in Open API 3.1 without path entries.

.. _v3.28.1:

Expand Down
2 changes: 1 addition & 1 deletion src/schemathesis/specs/openapi/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def get_operation_by_id(self, operation_id: str) -> APIOperation:
self._on_missing_operation(operation_id, exc, matches)

def _group_operations_by_id(self) -> Generator[tuple[str, APIOperation], None, None]:
for path, methods in self.raw_schema["paths"].items():
for path, methods in self.raw_schema.get("paths", {}).items():
scope, methods = self._resolve_methods(methods)
common_parameters = self.resolver.resolve_all(methods.get("parameters", []), RECURSION_DEPTH_LIMIT - 8)
for method, definition in methods.items():
Expand Down
15 changes: 13 additions & 2 deletions test/test_schemas.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytest

import schemathesis
from schemathesis.exceptions import OperationSchemaError, SchemaError
from schemathesis.exceptions import OperationNotFound, OperationSchemaError, SchemaError
from schemathesis.experimental import OPEN_API_3_1
from schemathesis.internal.result import Err, Ok
from schemathesis.specs.openapi.parameters import OpenAPI20Body
from schemathesis.specs.openapi.schemas import InliningResolver
from schemathesis.internal.result import Err, Ok


@pytest.mark.parametrize("base_path", ("/v1", "/v1/"))
Expand Down Expand Up @@ -229,6 +229,17 @@ def test_get_operation_by_id_in_referenced_path_shared_parameters(empty_open_api
assert operation.query.get("foo").definition == parameter


def test_get_operation_by_id_no_paths_on_openapi_3_1():
raw_schema = {
"openapi": "3.1.0",
"info": {"title": "Test", "version": "0.1.0"},
}
OPEN_API_3_1.enable()
schema = schemathesis.from_dict(raw_schema)
with pytest.raises(OperationNotFound):
schema.get_operation_by_id("getFoo")


@pytest.mark.parametrize(
"fixture, path",
(
Expand Down

0 comments on commit 4913d6b

Please sign in to comment.