adobe / adobe/target-python-sdk
get_attributes crashes with TypeError: 'NoneType' object is not iterable
- Ngôn ngữ chính
- Python
- Star
- 4
- Fork
- 7
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
Hello,
I'm using version 1.1.0 of the `target-python-sdk` and some calls to `get_attributes` raises the following error:
```
Traceback (most recent call last):
File "/app/.venv/lib/python3.11/site-packages/target_python_sdk/__init__.py", line 268, in get_attributes
return AttributesProvider(response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/target_tools/attributes_provider.py", line 46, in __init__
self.indexed = create_indexed(offers_response.get("response"))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.11/site-packages/target_tools/attributes_provider.py", line 28, in create_indexed
for option in mbox.options:
TypeError: 'NoneType' object is not iterable
```
The error happens when an mbox name is wrong or even when the request doesn't match the audience rule for the given mbox.
The following code snippet would help to reproduce the problem:
```
target_client = TargetClient.create({
"client": "Client",
"organization_id": "Org-ID",
"events": {
"client_ready": client_ready_callback,
},
})
def get_attrs():
delivery_request = DeliveryRequest(
id=VisitorId(third_party_id="some-user-id"),
context=Context(
channel=ChannelType.WEB,
),
execute=ExecuteRequest(mboxes=[MboxRequest(name="invalid-mbox-name", index=1)]),
)
# target_client was correctly created and initialized
attrs = target_client.get_attributes(flag_names, {"request": delivery_request}) # Code crashes at this point
return attrs
```
Notice that you can pass a valid mbox name for an activity with a custom audience filter that doesn't match with the request context and the code will crash. For example, you could limite the activity with geolocation as this:
The problem is with this part of the target-python-sdk's code:
```
def create_indexed(response):
"""
:param response: (delivery_api_client.Model.delivery_response.DeliveryResponse)
Target View Delivery API response
:return (dict) returns a map of mbox names to content
"""
result = {}
for request_type in REQUEST_TYPES:
if getattr(response, request_type) and getattr(response, request_type).mboxes:
for mbox in getattr(response, request_type).mboxes:
name = mbox.name
for option in mbox.options: # <---- mbox.options is None as returned by Adobe Target
if option.type == OptionType.JSON and option.content:
result[name] = {} if not result or not result.get(
name) else result[name]
result[name].update(option.content)
return result
```
Is there something I can do to prevent sdk from crashing?
Hướng dẫn đóng góp
Hướng nghiên cứu
Bắt đầu trong target_tools/attributes_provider.py tại create_indexed, sử dụng bản tái hiện get_attributes đã được báo cáo với một mbox không hợp lệ hoặc audience không khớp. Kiểm tra cách xử lý phản hồi có mbox.options được đặt thành None và xác nhận rằng hành vi sau khi hoàn tất không còn phát sinh TypeError nữa, đồng thời trả về các thuộc tính rỗng hoặc khả dụng như mong đợi.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- api
- Loại issue
- Lỗi
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 45/100