Skip to content

Commit 548f62d

Browse files
committed
Fix oneOf handling for lists of primitives in model_utils
1 parent c84f7b4 commit 548f62d

File tree

2 files changed

+78
-18
lines changed

2 files changed

+78
-18
lines changed

.generator/src/generator/templates/model_utils.j2

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ def composed_model_input_classes(cls):
7575
This function returns a list of the possible models that can be accepted as
7676
inputs.
7777
"""
78+
# Handle list types (e.g., [str], [float])
79+
if isinstance(cls, list):
80+
return [cls]
7881
if issubclass(cls, ModelSimple) or cls in PRIMITIVE_TYPES:
7982
return [cls]
8083
elif issubclass(cls, ModelNormal):
@@ -1557,17 +1560,44 @@ def get_oneof_instance(cls, model_kwargs, constant_kwargs, model_arg=None):
15571560
# Empty data
15581561
oneof_instances.append(list_oneof_instance)
15591562
continue
1560-
for arg in model_arg:
1561-
if constant_kwargs.get("_spec_property_naming"):
1562-
oneof_instance = oneof_class(
1563-
**change_keys_js_to_python(arg, oneof_class), **constant_kwargs
1563+
1564+
# Check if inner type is primitive - follows same pattern as complex objects:
1565+
# https://github.com/DataDog/datadog-api-client-python/blob/008536d34760ce096e118edc54df613d82194529/.generator/src/generator/templates/model_utils.j2#L1590-L1599
1566+
if oneof_class in PRIMITIVE_TYPES:
1567+
# Handle list of primitives (e.g., [str], [float])
1568+
for arg in model_arg:
1569+
oneof_instance = validate_and_convert_types(
1570+
arg,
1571+
(oneof_class,),
1572+
constant_kwargs.get("_path_to_item", ()),
1573+
constant_kwargs.get("_spec_property_naming", False),
1574+
constant_kwargs.get("_check_type", True),
1575+
configuration=constant_kwargs.get("_configuration"),
15641576
)
1565-
else:
1566-
oneof_instance = oneof_class(**arg, **constant_kwargs)
1567-
if not oneof_instance._unparsed:
15681577
list_oneof_instance.append(oneof_instance)
1569-
if list_oneof_instance:
1570-
oneof_instances.append(list_oneof_instance)
1578+
if list_oneof_instance:
1579+
oneof_instances.append(list_oneof_instance)
1580+
elif inspect.isclass(oneof_class) and issubclass(oneof_class, ModelSimple):
1581+
# Handle list of ModelSimple
1582+
for arg in model_arg:
1583+
oneof_instance = oneof_class(arg, **constant_kwargs)
1584+
if not oneof_instance._unparsed:
1585+
list_oneof_instance.append(oneof_instance)
1586+
if list_oneof_instance:
1587+
oneof_instances.append(list_oneof_instance)
1588+
else:
1589+
# Handle list of complex objects (ModelNormal, ModelComposed)
1590+
for arg in model_arg:
1591+
if constant_kwargs.get("_spec_property_naming"):
1592+
oneof_instance = oneof_class(
1593+
**change_keys_js_to_python(arg, oneof_class), **constant_kwargs
1594+
)
1595+
else:
1596+
oneof_instance = oneof_class(**arg, **constant_kwargs)
1597+
if not oneof_instance._unparsed:
1598+
list_oneof_instance.append(oneof_instance)
1599+
if list_oneof_instance:
1600+
oneof_instances.append(list_oneof_instance)
15711601
elif issubclass(oneof_class, ModelSimple):
15721602
if model_arg is not None:
15731603
oneof_instance = oneof_class(model_arg, **constant_kwargs)

src/datadog_api_client/model_utils.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def composed_model_input_classes(cls):
7777
This function returns a list of the possible models that can be accepted as
7878
inputs.
7979
"""
80+
# Handle list types (e.g., [str], [float])
81+
if isinstance(cls, list):
82+
return [cls]
8083
if issubclass(cls, ModelSimple) or cls in PRIMITIVE_TYPES:
8184
return [cls]
8285
elif issubclass(cls, ModelNormal):
@@ -1571,17 +1574,44 @@ def get_oneof_instance(cls, model_kwargs, constant_kwargs, model_arg=None):
15711574
# Empty data
15721575
oneof_instances.append(list_oneof_instance)
15731576
continue
1574-
for arg in model_arg:
1575-
if constant_kwargs.get("_spec_property_naming"):
1576-
oneof_instance = oneof_class(
1577-
**change_keys_js_to_python(arg, oneof_class), **constant_kwargs
1577+
1578+
# Check if inner type is primitive - follows same pattern as complex objects:
1579+
# https://github.com/DataDog/datadog-api-client-python/blob/008536d34760ce096e118edc54df613d82194529/.generator/src/generator/templates/model_utils.j2#L1590-L1599
1580+
if oneof_class in PRIMITIVE_TYPES:
1581+
# Handle list of primitives (e.g., [str], [float])
1582+
for arg in model_arg:
1583+
oneof_instance = validate_and_convert_types(
1584+
arg,
1585+
(oneof_class,),
1586+
constant_kwargs.get("_path_to_item", ()),
1587+
constant_kwargs.get("_spec_property_naming", False),
1588+
constant_kwargs.get("_check_type", True),
1589+
configuration=constant_kwargs.get("_configuration"),
15781590
)
1579-
else:
1580-
oneof_instance = oneof_class(**arg, **constant_kwargs)
1581-
if not oneof_instance._unparsed:
15821591
list_oneof_instance.append(oneof_instance)
1583-
if list_oneof_instance:
1584-
oneof_instances.append(list_oneof_instance)
1592+
if list_oneof_instance:
1593+
oneof_instances.append(list_oneof_instance)
1594+
elif inspect.isclass(oneof_class) and issubclass(oneof_class, ModelSimple):
1595+
# Handle list of ModelSimple
1596+
for arg in model_arg:
1597+
oneof_instance = oneof_class(arg, **constant_kwargs)
1598+
if not oneof_instance._unparsed:
1599+
list_oneof_instance.append(oneof_instance)
1600+
if list_oneof_instance:
1601+
oneof_instances.append(list_oneof_instance)
1602+
else:
1603+
# Handle list of complex objects (ModelNormal, ModelComposed)
1604+
for arg in model_arg:
1605+
if constant_kwargs.get("_spec_property_naming"):
1606+
oneof_instance = oneof_class(
1607+
**change_keys_js_to_python(arg, oneof_class), **constant_kwargs
1608+
)
1609+
else:
1610+
oneof_instance = oneof_class(**arg, **constant_kwargs)
1611+
if not oneof_instance._unparsed:
1612+
list_oneof_instance.append(oneof_instance)
1613+
if list_oneof_instance:
1614+
oneof_instances.append(list_oneof_instance)
15851615
elif issubclass(oneof_class, ModelSimple):
15861616
if model_arg is not None:
15871617
oneof_instance = oneof_class(model_arg, **constant_kwargs)

0 commit comments

Comments
 (0)