IndexError when printing dashboard response containing empty group_by list
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 75/100
Hướng nghiên cứu
Bắt đầu trong .generator/src/generator/templates/model_utils.j2, tập trung vào model_to_dict() và get_oneof_instance(), các hàm tạo ra src/datadog_api_client/model_utils.py. Tái hiện trường hợp group_by rỗng thông qua DashboardsApi.get_dashboard() và thực thi print(response) hoặc to_dict(). Được xem là hoàn tất khi response được tuần tự hóa và in ra mà không có IndexError khi _composed_instances rỗng.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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]
- Ngôn ngữ chính
- Python
- Star
- 166
- Fork
- 55
- Merge trung bình
- 2 ngày 17 giờ
- Pull request đã merge (30 ngày)
- 73
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của DataDog/datadog-api-client-python
-
stale
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 72/100
DataDog/datadog-api-client-python#3535 · 1 bình luận ·
-
kind/bug stale
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 66/100
DataDog/datadog-api-client-python#3717 · 3 bình luận ·
-
kind/bug stale
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 38/100
DataDog/datadog-api-client-python#3120 · 1 bình luận ·
-
stale
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
DataDog/datadog-api-client-python#2986 · 1 bình luận ·
-
kind/feature-request stale
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
DataDog/datadog-api-client-python#2878 · 3 bình luận ·
Tất cả issue của DataDog/datadog-api-client-python
Issue tương tự
-
essnmx good first issue
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 95/100
-
[Feature] 奇物选择添加优先级 Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
syfoud/Simulated_Scepter#174 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Giskard-AI/giskard-oss#2840 · 1 bình luận ·
-
A claim comment carrying the issue number is silently declined while the workflow reports success Đang mởarea: repo bug perceived difficulty: 2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
yeti-platform/yeti#1380 ·