Proposed a fix in PR: https://github.com/xwiki/xwiki-platform/pull/6385. Looking forward to review/feedback.