diff --git a/aiopenapi3/extra.py b/aiopenapi3/extra.py index e451a756..a5dc140c 100644 --- a/aiopenapi3/extra.py +++ b/aiopenapi3/extra.py @@ -18,6 +18,7 @@ def __init__( Union[Tuple[Union[re.Pattern, str], Optional[List[Union[re.Pattern, str]]]], Union[re.Pattern, str]] ], ) -> None: + assert isinstance(operations, list), type(operations) self.operations = operations super().__init__() @@ -38,7 +39,7 @@ def _reduced_paths(self, ctx: "Document.Context") -> dict: if path_key not in reduced: reduced[path_key] = {k: v for k, v in path_value.items() if k in keep_keys} reduced[path_key][operation_key] = operation_value - else: + elif isinstance(operation, tuple) and len(operation) == 2: pattern, operation_patterns = operation for path_key in ctx.document["paths"].keys(): if (isinstance(pattern, str) and pattern == path_key) or ( @@ -55,6 +56,8 @@ def _reduced_paths(self, ctx: "Document.Context") -> dict: for op_pattern in operation_patterns ) } + else: + raise TypeError(operation) return reduced def parsed(self, ctx: "Document.Context") -> "Document.Context": diff --git a/tests/extra_test.py b/tests/extra_test.py index 120f7aa6..d0e9b798 100644 --- a/tests/extra_test.py +++ b/tests/extra_test.py @@ -123,11 +123,12 @@ def test_reduced(with_extra_reduced, httpx_mock, compressor): assert "A" in api.components.responses assert "A" in api.components.requestBodies + # (str, methods) api = OpenAPI.load_file( "http://127.0.0.1/api.yaml", with_extra_reduced, session_factory=httpx.Client, - plugins=[compressor({"/A/{Path}": None})], + plugins=[compressor([("/A/{Path}", None)])], loader=FileSystemLoader(Path("tests/fixtures")), ) @@ -151,11 +152,12 @@ def test_reduced(with_extra_reduced, httpx_mock, compressor): assert payload.a == 1 assert headers["X-A"] == "A" + # (re, methods) api = OpenAPI.load_file( "http://127.0.0.1/api.yaml", with_extra_reduced, session_factory=httpx.Client, - plugins=[compressor({re.compile("/B"): None})], + plugins=[compressor([(re.compile("/B"), None)])], loader=FileSystemLoader(Path("tests/fixtures")), ) assert "/A/{Path}" not in api.paths.paths @@ -163,3 +165,39 @@ def test_reduced(with_extra_reduced, httpx_mock, compressor): assert "A" not in api.components.schemas assert "A" not in api.components.responses assert "A" not in api.components.requestBodies + + # operationId str + api = OpenAPI.load_file( + "http://127.0.0.1/api.yaml", + with_extra_reduced, + session_factory=httpx.Client, + plugins=[compressor(["A"])], + loader=FileSystemLoader(Path("tests/fixtures")), + ) + assert "/A/{Path}" in api.paths.paths + assert "A" in api.components.parameters + assert "AA" in api.components.schemas + assert "A" in api.components.schemas + + assert "A0" in api.components.schemas + assert "A1" in api.components.schemas + assert "A" in api.components.responses + assert "A" in api.components.requestBodies + + # operationId re + api = OpenAPI.load_file( + "http://127.0.0.1/api.yaml", + with_extra_reduced, + session_factory=httpx.Client, + plugins=[compressor([re.compile(r"[A]{1}$")])], + loader=FileSystemLoader(Path("tests/fixtures")), + ) + assert "/A/{Path}" in api.paths.paths + assert "A" in api.components.parameters + assert "AA" in api.components.schemas + assert "A" in api.components.schemas + + assert "A0" in api.components.schemas + assert "A1" in api.components.schemas + assert "A" in api.components.responses + assert "A" in api.components.requestBodies diff --git a/tests/fixtures/extra-reduced.yaml b/tests/fixtures/extra-reduced.yaml index b851d3d0..5b54ab87 100644 --- a/tests/fixtures/extra-reduced.yaml +++ b/tests/fixtures/extra-reduced.yaml @@ -85,6 +85,7 @@ paths: /B: get: + operationId: B responses: "200": description: "ok"