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/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: 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