Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions modules/weko-items-ui/weko_items_ui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2148,8 +2148,13 @@ def get_subs_item(self,
key_label.append(new_label.format('0'))
key_data.append('')
elif properties[key]['type'] in ['array', 'object']:
if data and idx < len(data) and data[idx].get(key):
m_data = data[idx][key]
if data and idx < len(data):
if isinstance(data, list) and data[idx].get(key):
m_data = data[idx][key]
elif isinstance(data, dict) and data.get(key):
m_data = data[key]
else:
m_data = None
else:
m_data = None

Comment on lines +2151 to 2160
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition idx < len(data) on line 2151 is problematic when data is a dict. For dictionaries, len(data) returns the number of keys, not a valid indexing constraint. This will cause incorrect behavior when data is a dictionary.

Looking at the pattern used elsewhere in this function (lines 2139-2140 and 2178-2179), dictionaries are converted to lists before being indexed. The new logic should follow a similar pattern:

  1. Check if is_object is True (which sets max_items = 1 on line 2120, meaning idx will only be 0)
  2. Or convert dict to list before the outer condition check

The current implementation will fail or produce incorrect results when data is a dict with multiple keys and idx is 0, since 0 < len(data) might be True even though dict indexing with data[0] is invalid.

Suggested change
if data and idx < len(data):
if isinstance(data, list) and data[idx].get(key):
m_data = data[idx][key]
elif isinstance(data, dict) and data.get(key):
m_data = data[key]
else:
m_data = None
else:
m_data = None
if is_object:
# For object types, max_items is 1 and idx is 0.
if isinstance(data, dict) and data.get(key):
m_data = data[key]
elif isinstance(data, list) and data and data[0].get(key):
m_data = data[0][key]
else:
m_data = None
else:
# For array types, ensure we only index into list-like data.
if isinstance(data, dict):
data_list = [data]
else:
data_list = data
if isinstance(data_list, list) and data_list and idx < len(data_list) and data_list[idx].get(key):
m_data = data_list[idx][key]
else:
m_data = None

Copilot uses AI. Check for mistakes.
Expand Down Expand Up @@ -4730,8 +4735,13 @@ def get_subs_item(self,
key_label.append(new_label.format('0'))
key_data.append('')
elif properties[key]['type'] in ['array', 'object']:
if data and idx < len(data) and data[idx].get(key):
m_data = data[idx][key]
if data and idx < len(data):
if isinstance(data, list) and data[idx].get(key):
m_data = data[idx][key]
elif isinstance(data, dict) and data.get(key):
m_data = data[key]
else:
m_data = None
else:
m_data = None

Expand Down
Loading