[DataGrid] Avoid reading type of undefined in gridRowUtils#21808
Conversation
gridRowUtils
|
Deploy preview: https://deploy-preview-21808--material-ui-x.netlify.app/ Bundle size report
|
|
This pull request has been inactive for 30 days. Please remove the stale label or leave a comment to keep it open. Otherwise, it will be closed in 15 days. |
Deploy previewBundle size
Check out the code infra dashboard for more information about this PR. |
|
This pull request has been inactive for 30 days. Please remove the stale label or leave a comment to keep it open. Otherwise, it will be closed in 15 days. |
|
This pull request has been inactive for 30 days. Please remove the stale label or leave a comment to keep it open. Otherwise, it will be closed in 15 days. |
gridRowUtilstype of undefined in gridRowUtils
LukasTy
left a comment
There was a problem hiding this comment.
Thank you for the contribution. 🙏
Could you address these review findings?
Verdict: One half is a correct fix, the other half is a no-op. Request changes (or split).
Findings:
-
getTreeNodeDescendants (
node?.type) - correct and relevant. This is the real fix for the reported "Cannot read properties of undefined (reading 'type')". getRowGroupChildren is public pro/premium API; applySorting is undefined by default, so the default path hits theelsebranch that calls getTreeNodeDescendants directly (useGridRows.ts:397). A stale/invalid groupId makes tree[parentId] undefined and crashes on node.type. The sibling applySorting branch already guards withif (!groupNode) return [](useGridRows.ts:375), so this change just aligns the two paths. Good. -
getTopLevelRowCount (
rootGroupNode?.footerId) - ineffective, does not fix anything. On the same line,rootGroupNode.children.lengthis dereferenced WITHOUT optional chaining and is evaluated first (left-to-right), so if rootGroupNode is undefined it throws "Cannot read properties of undefined (reading 'children')" before?.footerIdever runs. The?.is dead code. It also guards children/footerId, not.type, so it is unrelated to the reported error. The root group node is built by buildRootGroup and is effectively always present, so the real-world risk here is near-zero. Recommendation: drop this hunk (preferred), or guard properly, e.g. early-return when !rootGroupNode.
Test coverage: Yes, for the effective half. Add a regression test in packages/x-data-grid-premium/src/tests/rowGroupingApiRef.DataGridPremium.test.tsx asserting getRowGroupChildren({ groupId: }) returns [] (and the same with applySorting: true for symmetry). Cheap, documents the contract, prevents regression. The getTopLevelRowCount hunk cannot be meaningfully tested because it still throws - which is itself the signal it should be removed or fixed.
|
Thank you @LukasTy, I have made all your suggestions. Please re-review! |
LukasTy
left a comment
There was a problem hiding this comment.
LGTM, thanks for addressing this. 🙏
Could you please run prettier and commit changes?
Graceful failure to prevent `Level: Error Cannot read properties of undefined (reading 'type')` errors Signed-off-by: Tim Buckley <[email protected]>
Signed-off-by: Tim <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Cherry-pick PRs will be created targeting branches: v8.x |
…1808) Co-authored-by: Claude Opus 4.8 <[email protected]> Signed-off-by: Tim Buckley <[email protected]> Signed-off-by: Tim <[email protected]>
Bugfix: Add optional chaining to tree node type checks for graceful failure instead of the
Cannot read properties of undefined (reading 'type')error.