From 7ecd063e6b808d17dea81fa125310c51c486a2b3 Mon Sep 17 00:00:00 2001 From: harabat Date: Sun, 3 Mar 2024 12:46:12 +0100 Subject: [PATCH 1/2] fix: allow hyphens as delimiter in PATH_PARAM_REGEX --- openapi_python_client/parser/openapi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi_python_client/parser/openapi.py b/openapi_python_client/parser/openapi.py index c04cd1e8b..453e9c77a 100644 --- a/openapi_python_client/parser/openapi.py +++ b/openapi_python_client/parser/openapi.py @@ -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: From 7efb2317056d25a0c299914bc04f699d4eec50c4 Mon Sep 17 00:00:00 2001 From: harabat Date: Sun, 3 Mar 2024 13:23:40 +0100 Subject: [PATCH 2/2] fix: add custom filter to add python names to path --- openapi_python_client/__init__.py | 1 + .../templates/endpoint_module.py.jinja | 4 ++-- openapi_python_client/utils.py | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/openapi_python_client/__init__.py b/openapi_python_client/__init__.py index 23d972eac..35b7057a8 100644 --- a/openapi_python_client/__init__.py +++ b/openapi_python_client/__init__.py @@ -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, } diff --git a/openapi_python_client/templates/endpoint_module.py.jinja b/openapi_python_client/templates/endpoint_module.py.jinja index 46f4eb365..fe0f16a8a 100644 --- a/openapi_python_client/templates/endpoint_module.py.jinja +++ b/openapi_python_client/templates/endpoint_module.py.jinja @@ -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 %} diff --git a/openapi_python_client/utils.py b/openapi_python_client/utils.py index 834e2666c..822e3fa49 100644 --- a/openapi_python_client/utils.py +++ b/openapi_python_client/utils.py @@ -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