Skip to content

Commit bc37409

Browse files
committed
feat: Expose python_identifier and class_name functions to custom templates to rename with the same behavior as the parser.
1 parent 9556ec9 commit bc37409

File tree

7 files changed

+14
-94
lines changed

7 files changed

+14
-94
lines changed

end_to_end_tests/custom-templates-golden-record/my_test_api_client/api/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
from .default import DefaultEndpoints
66
from .location import LocationEndpoints
7-
from .naming import NamingEndpoints
87
from .parameters import ParametersEndpoints
98
from .tag1 import Tag1Endpoints
109
from .tests import TestsEndpoints
10+
from .true_ import True_Endpoints
1111

1212

1313
class MyTestApiClientApi:
@@ -32,5 +32,5 @@ def location(cls) -> Type[LocationEndpoints]:
3232
return LocationEndpoints
3333

3434
@classmethod
35-
def naming(cls) -> Type[NamingEndpoints]:
36-
return NamingEndpoints
35+
def true_(cls) -> Type[True_Endpoints]:
36+
return True_Endpoints

end_to_end_tests/custom-templates-golden-record/my_test_api_client/api/naming/__init__.py

-11
This file was deleted.

end_to_end_tests/golden-record/my_test_api_client/api/naming/__init__.py

Whitespace-only changes.

end_to_end_tests/golden-record/my_test_api_client/api/naming/get_naming_keywords.py

-72
This file was deleted.

end_to_end_tests/test_custom_templates/api_init.py.jinja

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
from typing import Type
44
{% for tag in endpoint_collections_by_tag.keys() %}
5-
from .{{ tag }} import {{ utils.pascal_case(tag) }}Endpoints
5+
from .{{ tag }} import {{ class_name(tag) }}Endpoints
66
{% endfor %}
77

8-
class {{ utils.pascal_case(package_name) }}Api:
8+
class {{ class_name(package_name) }}Api:
99
{% for tag in endpoint_collections_by_tag.keys() %}
1010
@classmethod
11-
def {{ tag }}(cls) -> Type[{{ utils.pascal_case(tag) }}Endpoints]:
12-
return {{ utils.pascal_case(tag) }}Endpoints
11+
def {{ tag }}(cls) -> Type[{{ class_name(tag) }}Endpoints]:
12+
return {{ class_name(tag) }}Endpoints
1313
{% endfor %}

end_to_end_tests/test_custom_templates/endpoint_init.py.jinja

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
import types
44
{% for endpoint in endpoint_collection.endpoints %}
5-
from . import {{ utils.snake_case(endpoint.name) }}
5+
from . import {{ python_identifier(endpoint.name) }}
66
{% endfor %}
77

8-
class {{ utils.pascal_case(endpoint_collection.tag) }}Endpoints:
8+
class {{ class_name(endpoint_collection.tag) }}Endpoints:
99

1010
{% for endpoint in endpoint_collection.endpoints %}
1111

1212
@classmethod
13-
def {{ utils.snake_case(endpoint.name) }}(cls) -> types.ModuleType:
13+
def {{ python_identifier(endpoint.name) }}(cls) -> types.ModuleType:
1414
{% if endpoint.description %}
1515
"""
1616
{{ endpoint.description }}
@@ -20,5 +20,5 @@ class {{ utils.pascal_case(endpoint_collection.tag) }}Endpoints:
2020
{{ endpoint.summary }}
2121
"""
2222
{% endif %}
23-
return {{ utils.snake_case(endpoint.name) }}
23+
return {{ python_identifier(endpoint.name) }}
2424
{% endfor %}

openapi_python_client/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def __init__(
5858
self.openapi: GeneratorData = openapi
5959
self.meta: MetaType = meta
6060
self.file_encoding = file_encoding
61+
self.config = config
6162

6263
package_loader = PackageLoader(__package__)
6364
loader: BaseLoader
@@ -87,6 +88,8 @@ def __init__(
8788
self.env.filters.update(TEMPLATE_FILTERS)
8889
self.env.globals.update(
8990
utils=utils,
91+
python_identifier=lambda x: utils.PythonIdentifier(x, config.field_prefix),
92+
class_name=lambda x: utils.ClassName(x, config.field_prefix),
9093
package_name=self.package_name,
9194
package_dir=self.package_dir,
9295
package_description=self.package_description,

0 commit comments

Comments
 (0)