Skip to content

Comments

Fix pullRelatedField returning empty results on last pagination page#34748

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-pullrelatedfield-results
Draft

Fix pullRelatedField returning empty results on last pagination page#34748
Copilot wants to merge 2 commits intomainfrom
copilot/fix-pullrelatedfield-results

Conversation

Copy link
Contributor

Copilot AI commented Feb 24, 2026

pullRelatedField silently returns 0 results on the last page whenever offset + limit exceeds the filtered list size — an IndexOutOfBoundsException is thrown and swallowed, returning an empty list.

Root Cause

In ContentUtils.getPullResults(), the subList end index was computed as offset + limit without clamping to list bounds. The existing guard (limit >= filteredList.size()) only catches the case where limit alone exceeds the list — not when offset shifts the window past the end.

// Before — throws IndexOutOfBoundsException when offset+limit > size (e.g. 4 items, offset=3, limit=3 → end=6)
return filteredList.subList(offset, (limit <= 0 || limit >= filteredList.size()) ? filteredList.size() : offset + limit);

// After — clamps safely
final int safeOffset = Math.max(offset, 0);
final int end = (limit <= 0) ? filteredList.size() : Math.min(safeOffset + limit, filteredList.size());
return filteredList.subList(Math.min(safeOffset, filteredList.size()), end);

Changes

  • ContentUtils.getPullResults() — replace raw offset + limit end index with Math.min(safeOffset + limit, filteredList.size()); guard fromIndex with Math.min(safeOffset, filteredList.size()) to prevent out-of-bounds when offset itself exceeds list size; use immutable safeOffset instead of mutating the parameter
  • ContentUtilsTest — add testPullRelatedField_lastPageReturnsRemainingItems: 4 related items, offset=3 / limit=3 → asserts 1 item returned (the 4th)

Checklist

  • Tests
  • Translations
  • Security Implications Contemplated (add notes if applicable)

Additional Info

The fix is purely arithmetic — no behavior change for any in-bounds pagination call. The limit <= 0 (fetch-all) path is preserved unchanged.

Original prompt

This section details on the original issue you should resolve

<issue_title>[DEFECT] pullRelatedField fails to return last "page" of results</issue_title>
<issue_description>### Problem Statement

I'm getting 0 results when using $dotcontent.pullRelatedField() on the last "page" of results.

Steps to Reproduce

Log into dotAdmin on demo.dotcms.com.

At the time of writing, there are 4 blog contentlets associated with author f851e34a-d128-408e-a75a-4fa1c33201b8. If we set the limit to 3 and offset to 3 we should get 1 result, but get 0 instead.

#set ($blogs = $dotcontent.pullRelatedField("f851e34a-d128-408e-a75a-4fa1c33201b8", "BlogAuthor.blog", "+contentType:Blog ", 3, 3, "modDate"))

$blogs.size()

I noticed that using a higher limit (e.g. 30) does not trigger the bug, allowing us to receive the result set.

Acceptance Criteria

  • $dotcontent.pullRelatedField() returns results as expected

dotCMS Version

  • 23.10.24_lts_v0 (version first noticed on)
  • dotcms-core 25.09.18-1 (demo.dotcms.com at the time of writing)

Severity

Medium - Some functionality impacted</issue_description>

<agent_instructions>Read @fmontes comment.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@fmontes ## AI Triage Summary

Validation: Issue is well-documented with clear reproduction steps and Velocity code snippet. Score: 85/100.

Duplicates: No exact duplicate found. Related issues: #21929, #33478, #17664, #17578.

Root Cause: Bug in ContentUtils.getPullResults() (line 654). When pulling related children with a condition, the code fetches all related content, filters it, then paginates. The subList(offset, offset+limit) calculation does not clamp the end index to the filtered list size, causing IndexOutOfBoundsException when offset + limit > filteredList.size(). The exception is caught and returns an empty list.

Fix: Replace the subList end-index calculation with Math.min(offset + limit, filteredList.size()). Also add a guard for offset >= filteredList.size().

Complexity: Small

Relevant Files:

  • dotCMS/src/main/java/com/dotcms/rendering/velocity/viewtools/content/util/ContentUtils.java (line 654)
  • dotCMS/src/main/java/com/dotcms/rendering/velocity/viewtools/content/ContentTool.java (line 534)
  • dotcms-integration/src/test/java/com/dotcms/rendering/velocity/viewtools/content/util/ContentUtilsTest.java</comment_new>

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…ation

When offset + limit exceeds the filtered list size, the old code threw
IndexOutOfBoundsException which was silently caught, returning an empty
list. Replace with Math.min(safeOffset + limit, filteredList.size()) and
add a guard for offset > list size.

Fixes pullRelatedField failing to return the last page of results.

Co-authored-by: fmontes <751424+fmontes@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix pullRelatedField to return last page of results Fix pullRelatedField returning empty results on last pagination page Feb 24, 2026
Copilot AI requested a review from fmontes February 24, 2026 01:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[DEFECT] pullRelatedField fails to return last "page" of results

2 participants