Skip to content

Commit 353509a

Browse files
author
Marco Müllner
committed
feat: Using operation and path name for operation id if no operation id is passed
1 parent f79bae1 commit 353509a

File tree

4 files changed

+512
-4
lines changed

4 files changed

+512
-4
lines changed

src/openapi_python_generator/language_converters/python/service_generator.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,13 @@ def _generate_params_from_content(content: Union[Reference, Schema]):
154154
return params + default_params
155155

156156

157-
def generate_operation_id(operation: Operation, http_op: str) -> str:
157+
def generate_operation_id(operation: Operation, http_op: str, path_name: str | None = None) -> str:
158158
if operation.operationId is not None:
159159
return common.normalize_symbol(operation.operationId)
160+
elif path_name is not None:
161+
return common.normalize_symbol(f"{http_op}_{path_name}")
160162
else:
161-
raise Exception(f"OperationId is not defined for {http_op}") # pragma: no cover
163+
raise Exception(f"OperationId is not defined for {http_op} of path_name {path_name} --> {operation.summary}") # pragma: no cover
162164

163165

164166
def _generate_params(
@@ -267,7 +269,7 @@ def generate_service_operation(
267269
op: Operation, path_name: str, async_type: bool
268270
) -> ServiceOperation:
269271
params = generate_params(op)
270-
operation_id = generate_operation_id(op, http_operation)
272+
operation_id = generate_operation_id(op, http_operation,path_name)
271273
query_params = generate_query_params(op)
272274
header_params = generate_header_params(op)
273275
return_type = generate_return_type(op)

tests/regression/test_issue_17.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pytest
22
from click.testing import CliRunner
33

4-
from openapi_python_generator.__main__ import main
54
from openapi_python_generator.common import HTTPLibrary
65
from openapi_python_generator.generate_data import generate_data
76
from tests.conftest import test_data_folder

tests/regression/test_issue_51.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
from click.testing import CliRunner
3+
4+
from openapi_python_generator.common import HTTPLibrary
5+
from openapi_python_generator.generate_data import generate_data
6+
from tests.conftest import test_data_folder
7+
from tests.conftest import test_result_path
8+
9+
10+
@pytest.fixture
11+
def runner() -> CliRunner:
12+
"""Fixture for invoking command-line interfaces."""
13+
return CliRunner()
14+
15+
16+
@pytest.mark.parametrize(
17+
"library",
18+
[HTTPLibrary.httpx, HTTPLibrary.aiohttp, HTTPLibrary.requests],
19+
)
20+
def test_issue_11(runner: CliRunner, model_data_with_cleanup, library) -> None:
21+
"""
22+
https://github.com/MarcoMuellner/openapi-python-generator/issues/7
23+
"""
24+
assert (
25+
generate_data(test_data_folder / "issue_51.json", test_result_path, library)
26+
is None
27+
)

0 commit comments

Comments
 (0)