Skip to content
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: 2 additions & 2 deletions fastapi_code_generator/modular_template/routers.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ router = APIRouter(

{% for operation in operations %}
{% if operation.tags[0] == tag %}
@router.{{operation.type}}('{{operation.snake_case_path}}', response_model={{operation.response}}
@router.{{operation.type}}('{{operation.path}}', response_model={{operation.response}}
{% if operation.additional_responses %}
, responses={
{% for status_code, models in operation.additional_responses.items() %}
Expand All @@ -33,4 +33,4 @@ def {{operation.function_name}}({{operation.snake_case_arguments}}) -> {{operati
{%- endif %}
pass
{% endif %}
{% endfor %}
{% endfor %}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def create_fat_cats() -> None:
pass


@router.get('/cats/{cat_id}', response_model=Pet, tags=['Fat Cats'])
@router.get('/cats/{catId}', response_model=Pet, tags=['Fat Cats'])
def show_cat_by_id(cat_id: str = Path(..., alias='catId')) -> Pet:
"""
Info For a Specific Cat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def create_wild_boars() -> None:
pass


@router.get('/boars/{boar_id}', response_model=Pet, tags=['Wild Boars'])
@router.get('/boars/{boarId}', response_model=Pet, tags=['Wild Boars'])
def show_boar_by_id(boar_id: str = Path(..., alias='boarId')) -> Pet:
"""
Info For a Specific Boar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def create_fat_cats() -> None:
pass


@router.get('/cats/{cat_id}', response_model=Pet, tags=['Fat Cats'])
@router.get('/cats/{catId}', response_model=Pet, tags=['Fat Cats'])
def show_cat_by_id(cat_id: str = Path(..., alias='catId')) -> Pet:
"""
Info For a Specific Cat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def create_slim_dogs() -> None:
pass


@router.get('/dogs/{dog_id}', response_model=Pet, tags=['Slim Dogs'])
@router.get('/dogs/{dogId}', response_model=Pet, tags=['Slim Dogs'])
def show_dog_by_id(dog_id: str = Path(..., alias='dogId')) -> Pet:
"""
Info For a Specific Dog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def create_wild_boars() -> None:
pass


@router.get('/boars/{boar_id}', response_model=Pet, tags=['Wild Boars'])
@router.get('/boars/{boarId}', response_model=Pet, tags=['Wild Boars'])
def show_boar_by_id(boar_id: str = Path(..., alias='boarId')) -> Pet:
"""
Info For a Specific Boar
Expand Down
4 changes: 2 additions & 2 deletions tests/data/modular_template/routers.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ router = APIRouter(

{% for operation in operations %}
{% if operation.tags[0] == tag %}
@router.{{operation.type}}('{{operation.snake_case_path}}', response_model={{operation.response}}
@router.{{operation.type}}('{{operation.path}}', response_model={{operation.response}}
{% if operation.additional_responses %}
, responses={
{% for status_code, models in operation.additional_responses.items() %}
Expand All @@ -33,4 +33,4 @@ def {{operation.function_name}}({{operation.snake_case_arguments}}) -> {{operati
{%- endif %}
pass
{% endif %}
{% endfor %}
{% endfor %}
44 changes: 44 additions & 0 deletions tests/main/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,50 @@ def test_generate_router_name_from_hyphenated_tag(output_dir: Path) -> None:
validate_generated_code(output_dir)


def test_generate_router_preserves_path_parameter_name(output_dir: Path) -> None:
spec = json.dumps(
{
"openapi": "3.0.0",
"info": {"title": "Example", "version": "1.0.0"},
"paths": {
"/items/{itemId}": {
"get": {
"operationId": "getItem",
"tags": ["Items"],
"parameters": [
{
"name": "itemId",
"in": "path",
"required": True,
"schema": {"type": "string"},
}
],
"responses": {"200": {"description": "OK"}},
}
}
},
}
)

generate_code(
"camel_path_parameter.yaml",
spec,
"utf-8",
output_dir,
BUILTIN_MODULAR_TEMPLATE_DIR,
disable_timestamp=True,
generate_routers=True,
)

router_text = output_dir.joinpath("routers", "items.py").read_text(encoding="utf-8")
main_text = output_dir.joinpath("main.py").read_text(encoding="utf-8")
assert "@router.get('/items/{itemId}', response_model=None" in router_text
assert "item_id: str = Path(..., alias='itemId')" in router_text
assert "from .routers import items" in main_text
assert "app.include_router(items.router)" in main_text
validate_generated_code(output_dir)


@pytest.mark.cli_doc(
options=["--specify-tags"],
option_description="Regenerate only the routers matching a comma-separated tag list.",
Expand Down
Loading