Skip to content

fix: add custom filter to enable adding python names to path #987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
1 change: 1 addition & 0 deletions openapi_python_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"snakecase": utils.snake_case,
"kebabcase": utils.kebab_case,
"pascalcase": utils.pascal_case,
"convert_endpoint_path": utils.convert_endpoint_path,
"any": any,
}

Expand Down
2 changes: 1 addition & 1 deletion openapi_python_client/parser/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from .properties.schemas import parameter_from_reference
from .responses import Response, response_from_data

_PATH_PARAM_REGEX = re.compile("{([a-zA-Z_][a-zA-Z0-9_]*)}")
_PATH_PARAM_REGEX = re.compile("{([a-zA-Z_-][a-zA-Z0-9_-]*)}")


def import_string_from_class(class_: Class, prefix: str = "") -> str:
Expand Down
4 changes: 2 additions & 2 deletions openapi_python_client/templates/endpoint_module.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def _get_kwargs(
_kwargs: Dict[str, Any] = {
"method": "{{ endpoint.method }}",
{% if endpoint.path_parameters %}
"url": "{{ endpoint.path }}".format(
"url": "{{ endpoint | convert_endpoint_path }}".format(
{%- for parameter in endpoint.path_parameters -%}
{{parameter.name}}={{parameter.python_name}},
{{parameter.python_name}}={{parameter.python_name}},
{%- endfor -%}
),
{% else %}
Expand Down
14 changes: 14 additions & 0 deletions openapi_python_client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,17 @@ def get_content_type(content_type: str) -> str | None:
return None

return parsed_content_type


def convert_endpoint_path(endpoint) -> str:
"""
Converts parameter names within the endpoint path to their python names.
"""

endpoint_path_python_names = endpoint.path
for parameter in endpoint.path_parameters:
endpoint_path_python_names = endpoint_path_python_names.replace(
f'{{{parameter.name}}}',
f'{{{parameter.python_name}}}'
)
return endpoint_path_python_names