Skip to content

Commit e03be4c

Browse files
author
Barry Barrette
committed
Adding support for named enums
1 parent d46544c commit e03be4c

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

openapi_python_client/parser/properties/enum_property.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ def build( # noqa: PLR0911
121121
if parent_name:
122122
class_name = f"{utils.pascal_case(parent_name)}{utils.pascal_case(class_name)}"
123123
class_info = Class.from_string(string=class_name, config=config)
124-
values = EnumProperty.values_from_list(value_list, class_info)
124+
var_names = data.model_extra.get("x-enum-varnames", [])
125+
values = EnumProperty.values_from_list(value_list, class_info, var_names)
125126

126127
if class_info.name in schemas.classes_by_name:
127128
existing = schemas.classes_by_name[class_info.name]
@@ -183,14 +184,17 @@ def get_imports(self, *, prefix: str) -> set[str]:
183184
return imports
184185

185186
@staticmethod
186-
def values_from_list(values: list[str] | list[int], class_info: Class) -> dict[str, ValueType]:
187+
def values_from_list(values: list[str] | list[int], class_info: Class, varnames: list[str]) -> dict[str, ValueType]:
187188
"""Convert a list of values into dict of {name: value}, where value can sometimes be None"""
188189
output: dict[str, ValueType] = {}
189190

190191
for i, value in enumerate(values):
191192
value = cast(Union[str, int], value)
192193
if isinstance(value, int):
193-
if value < 0:
194+
if varnames:
195+
key = varnames[i].upper()
196+
output[key] = value
197+
elif value < 0:
194198
output[f"VALUE_NEGATIVE_{-value}"] = value
195199
else:
196200
output[f"VALUE_{value}"] = value

0 commit comments

Comments
 (0)