Skip to content

Commit f0c8b78

Browse files
committed
pass models & enums as lists so custom templates can use them
1 parent 718e9aa commit f0c8b78

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

.changeset/fix-models-enums-lists.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
default: patch
3+
---
4+
5+
# Make lists of models and enums work correctly in custom templates
6+
7+
Lists of model and enum classes should be available to custom templates via the Jinja
8+
variables `openapi.models` and `openapi.enums`, but these were being passed in a way that made
9+
them always appear empty. This has been fixed so a custom template can now iterate over them.
10+
11+
Closes #1188.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Testing that we can access model-related information via Jinja variables.
2+
3+
# To avoid having to update this file in the golden record every time the test specs are changed,
4+
# we won't include all the classes in this output - we'll just look for one of them.
5+
6+
# Using "alls"
7+
# AModel
8+
9+
# Using "imports"
10+
# from .a_model import AModel
11+
12+
# Using "openapi.models"
13+
# AModel (a_model)
14+
15+
# Using "openapi.enums"
16+
# AnEnum (an_enum)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
# Testing that we can access model-related information via Jinja variables.
3+
4+
# To avoid having to update this file in the golden record every time the test specs are changed,
5+
# we won't include all the classes in this output - we'll just look for one of them.
6+
7+
# Using "alls"
8+
{% for name in alls %}
9+
{% if name == "AModel" %}
10+
# {{ name }}
11+
{% endif %}
12+
{% endfor %}
13+
14+
# Using "imports"
15+
{% for import in imports %}
16+
{% if import.endswith("import AModel") %}
17+
# {{ import }}
18+
{% endif %}
19+
{% endfor %}
20+
21+
# Using "openapi.models"
22+
{% for model in openapi.models %}
23+
{% if model.class_info.name == "AModel" %}
24+
# {{ model.class_info.name }} ({{ model.class_info.module_name }})
25+
{% endif %}
26+
{% endfor %}
27+
28+
# Using "openapi.enums"
29+
{% for enum in openapi.enums %}
30+
{% if enum.class_info.name == "AnEnum" %}
31+
# {{ enum.class_info.name }} ({{ enum.class_info.module_name }})
32+
{% endif %}
33+
{% endfor %}

openapi_python_client/parser/openapi.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,10 @@ class GeneratorData:
493493
title: str
494494
description: Optional[str]
495495
version: str
496-
models: Iterator[ModelProperty]
496+
models: list[ModelProperty]
497497
errors: list[ParseError]
498498
endpoint_collections_by_tag: dict[utils.PythonIdentifier, EndpointCollection]
499-
enums: Iterator[Union[EnumProperty, LiteralEnumProperty]]
499+
enums: list[Union[EnumProperty, LiteralEnumProperty]]
500500

501501
@staticmethod
502502
def from_dict(data: dict[str, Any], *, config: Config) -> Union["GeneratorData", GeneratorError]:
@@ -525,10 +525,10 @@ def from_dict(data: dict[str, Any], *, config: Config) -> Union["GeneratorData",
525525
data=openapi.paths, schemas=schemas, parameters=parameters, request_bodies=request_bodies, config=config
526526
)
527527

528-
enums = (
528+
enums = [
529529
prop for prop in schemas.classes_by_name.values() if isinstance(prop, (EnumProperty, LiteralEnumProperty))
530-
)
531-
models = (prop for prop in schemas.classes_by_name.values() if isinstance(prop, ModelProperty))
530+
]
531+
models = [prop for prop in schemas.classes_by_name.values() if isinstance(prop, ModelProperty)]
532532

533533
return GeneratorData(
534534
title=openapi.info.title,

0 commit comments

Comments
 (0)