Skip to content

Commit 7eff32c

Browse files
authored
Issue-117: Fix dashes in brackets of path for f-strings (#118)
* Issue-117: Fix dashes in brackets of path for f-strings * Improve test
1 parent 9abfd63 commit 7eff32c

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

src/openapi_python_generator/language_converters/python/service_generator.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,17 +428,18 @@ def generate_service_operation(
428428
services = []
429429
service_ops = []
430430
for path_name, path in paths.items():
431+
clean_path_name = clean_up_path_name(path_name)
431432
for http_operation in HTTP_OPERATIONS:
432433
op = path.__getattribute__(http_operation)
433434
if op is None:
434435
continue
435436

436437
if library_config.include_sync:
437-
sync_so = generate_service_operation(op, path_name, False)
438+
sync_so = generate_service_operation(op, clean_path_name, False)
438439
service_ops.append(sync_so)
439440

440441
if library_config.include_async:
441-
async_so = generate_service_operation(op, path_name, True)
442+
async_so = generate_service_operation(op, clean_path_name, True)
442443
service_ops.append(async_so)
443444

444445
# Ensure every operation has a tag; fallback to "default" for untagged operations
@@ -489,3 +490,11 @@ def generate_service_operation(
489490
)
490491

491492
return services
493+
494+
495+
def clean_up_path_name(path_name: str) -> str:
496+
# Clean up path name: only replace dashes inside curly brackets for f-string compatibility, keep other dashes
497+
def _replace_bracket_dashes(match):
498+
return "{" + match.group(1).replace("-", "_") + "}"
499+
500+
return re.sub(r"\{([^}/]+)\}", _replace_bracket_dashes, path_name)

tests/regression/test_issue_117.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pytest
2+
from click.testing import CliRunner
3+
4+
from openapi_python_generator.common import HTTPLibrary, library_config_dict
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_117(runner: CliRunner, model_data_with_cleanup, library) -> None:
21+
"""
22+
https://github.com/MarcoMuellner/openapi-python-generator/issues/117
23+
"""
24+
generate_data(test_data_folder / "issue_117.json", test_result_path, library)
25+
26+
if library_config_dict[library].include_sync:
27+
assert (test_result_path / "services" / "default_service.py").exists()
28+
assert (test_result_path / "services" / "default_service.py").read_text().find(
29+
'path = f"/bar-boz/{foo_bar}"'
30+
) != -1
31+
32+
if library_config_dict[library].include_async:
33+
assert (test_result_path / "services" / "async_default_service.py").exists()
34+
assert (
35+
test_result_path / "services" / "async_default_service.py"
36+
).read_text().find('path = f"/bar-boz/{foo_bar}"') != -1

tests/test_data/issue_117.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"openapi": "3.1.0",
3+
"info": {
4+
"title": "New Specs",
5+
"version": "1.0.0"
6+
},
7+
"paths": {
8+
9+
"/bar-boz/{foo-bar}": {
10+
"get": {
11+
12+
"parameters": [
13+
{
14+
"name": "foo-bar",
15+
"in": "path",
16+
"description": "Buzz.",
17+
"required": true,
18+
"schema": {
19+
"type": "string"
20+
}
21+
}
22+
]
23+
}
24+
}
25+
},
26+
"components": {
27+
"schemas": {}
28+
}
29+
}

0 commit comments

Comments
 (0)