IndexError when printing dashboard response containing empty group_by list
还没有人认领这个 Issue。
评估
调研方向
从 .generator/src/generator/templates/model_utils.j2 开始,重点关注生成 src/datadog_api_client/model_utils.py 的 model_to_dict() 和 get_oneof_instance()。通过 DashboardsApi.get_dashboard() 复现空 group_by 的情况,并执行 print(response) 或 to_dict()。当 _composed_instances 为空时,response 能够序列化并打印且不出现 IndexError,即表示完成。
由索引模型根据 Issue 内容生成。
描述
Describe the bug
Calling print(response) on a dashboard response raises IndexError: list index out of range when the dashboard contains a query_value widget with a logs query that has "group_by": [] (empty list).
Labels: kind/bug, severity/major
To Reproduce
- Create a dashboard with a
query_valuewidget using a logs query withgroup_byset to an empty list. - Call
get_dashboard()using the Python API client. - Call
print(response)on the returned object. - See
IndexError: list index out of range.
Minimal reproduction:
from datadog_api_client import Configuration, ApiClient
from datadog_api_client.v1.api.dashboards_api import DashboardsApi
configuration = Configuration()
with ApiClient(configuration) as api_client:
api = DashboardsApi(api_client)
response = api.get_dashboard(dashboard_id="<dashboard-id>")
print(response) # raises IndexError
Expected behavior
The response should be printed (or serialized via to_dict()) without raising any exception.
Screenshots
Traceback (most recent call last):
File "main.py", line 8, in <module>
print(response)
File ".venv/lib/python3.14/site-packages/datadog_api_client/model_utils.py", line 194, in __repr__
return self.to_str()
File ".venv/lib/python3.14/site-packages/datadog_api_client/model_utils.py", line 422, in to_str
return pprint.pformat(self.to_dict())
File ".venv/lib/python3.14/site-packages/datadog_api_client/model_utils.py", line 418, in to_dict
return model_to_dict(self, serialize=False)
File ".venv/lib/python3.14/site-packages/datadog_api_client/model_utils.py", line 1502, in model_to_dict
model = model.get_oneof_instance()
File ".venv/lib/python3.14/site-packages/datadog_api_client/model_utils.py", line 581, in get_oneof_instance
return self._composed_instances[0]
IndexError: list index out of range
Root cause
group_by is typed as FormulaAndFunctionEventQueryGroupByConfig, a ModelComposed class with a oneOf of two variants: a list of group-by objects and a fields object. When the API returns "group_by": [], the oneOf matcher cannot determine which variant the empty list belongs to, so _composed_instances is left empty.
model_to_dict() in model_utils.py enters its traversal loop based on _composed_schemas being non-empty, without checking whether _composed_instances is populated, and then crashes at get_oneof_instance() which unconditionally accesses self._composed_instances[0].
Workaround
Set configuration.preload_content = False to skip deserialization and parse the raw JSON manually:
import json
from datadog_api_client import Configuration, ApiClient
from datadog_api_client.v1.api.dashboards_api import DashboardsApi
configuration = Configuration()
configuration.preload_content = False
with ApiClient(configuration) as api_client:
api = DashboardsApi(api_client)
response = api.get_dashboard(dashboard_id="<dashboard-id>")
data = json.loads(response.data)
print(json.dumps(data, indent=2))
Environment and Versions (please complete the following information):
datadog-api-clientversion: 2.55.0 (latest)- Python version: 3.14
- OS: macOS
Additional context
The fix requires two changes in .generator/src/generator/templates/model_utils.j2 (the template that generates src/datadog_api_client/model_utils.py):
-
Guard the loop in
model_to_dict()to only enter when_composed_instancesis also non-empty:while model._composed_schemas and model._composed_instances: -
Make
get_oneof_instance()fall back toselfinstead of crashing when_composed_instancesis empty:def get_oneof_instance(self): if not self._composed_instances: return self return self._composed_instances[0]
- 主要语言
- Python
- 星标
- 166
- 派生
- 55
- 平均合并
- 2 天 17 小时
- 30 天内合并 PR
- 73
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
DataDog/datadog-api-client-python 的其他 Issue
-
stale
难度 1/5 1-3 小时 新手友好度 72/100
DataDog/datadog-api-client-python#3535 · 1 条评论 ·
-
kind/bug stale
难度 3/5 1-2 天 新手友好度 66/100
DataDog/datadog-api-client-python#3717 · 3 条评论 ·
-
kind/bug stale
难度 3/5 1-2 天 新手友好度 38/100
DataDog/datadog-api-client-python#3120 · 1 条评论 ·
-
stale
难度 3/5 1-2 天 新手友好度 45/100
DataDog/datadog-api-client-python#2986 · 1 条评论 ·
-
kind/feature-request stale
难度 5/5 一周以上 新手友好度 20/100
DataDog/datadog-api-client-python#2878 · 3 条评论 ·
查看 DataDog/datadog-api-client-python 的全部 Issue
相似的 Issue
-
essnmx good first issue
难度 1/5 1 小时以内 新手友好度 95/100
-
难度 2/5 1-3 小时 新手友好度 65/100
syfoud/Simulated_Scepter#174 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
Giskard-AI/giskard-oss#2840 · 1 条评论 ·
-
A claim comment carrying the issue number is silently declined while the workflow reports success 未关闭area: repo bug perceived difficulty: 2
难度 2/5 1-3 小时 新手友好度 70/100
-
难度 2/5 1-3 小时 新手友好度 75/100
yeti-platform/yeti#1380 ·