Skip to content

Commit 22275fd

Browse files
committed
Add support for x-enum-varnames to string enums
1 parent 3464f80 commit 22275fd

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
default: minor
3+
---
4+
5+
# Add support for x-enum-varnames to string enums
6+
7+
#1358 by @mbbush
8+
9+
You can now customize the variable names of the generated string enumerations using the x-enum-varnames openapi extension. Previously, this was only possible for integer enumerations.

end_to_end_tests/functional_tests/generated_code_execution/test_enums_and_consts.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,35 @@ def test_invalid_values(self, MyModel):
7171
MyModel.from_dict({"enumProp": 2})
7272

7373

74+
@with_generated_client_fixture(
75+
"""
76+
components:
77+
schemas:
78+
MyStrEnum:
79+
type: string
80+
enum: ["a", "b", "c"]
81+
x-enum-varnames: [
82+
"One",
83+
"More than OnE",
84+
"not_quite_four"
85+
]
86+
""")
87+
@with_generated_code_imports(
88+
".models.MyStrEnum",
89+
)
90+
class TestStrEnumVarNameExtensions:
91+
@pytest.mark.parametrize(
92+
"expected_name,expected_value",
93+
[
94+
("ONE", "a"),
95+
("MORE_THAN_ON_E", "b"),
96+
("NOT_QUITE_FOUR", "c"),
97+
],
98+
)
99+
def test_enum_values(self, MyStrEnum, expected_name, expected_value):
100+
assert getattr(MyStrEnum, expected_name) == MyStrEnum(expected_value)
101+
102+
74103
@with_generated_client_fixture(
75104
"""
76105
components:

openapi_python_client/parser/properties/enum_property.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ def values_from_list(
201201
else:
202202
output[f"VALUE_{value}"] = value
203203
continue
204-
if value and value[0].isalpha():
204+
if use_var_names:
205+
key = var_names[i]
206+
elif value and value[0].isalpha():
205207
key = value.upper()
206208
else:
207209
key = f"VALUE_{i}"

0 commit comments

Comments
 (0)