Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flatten hit modify original array #9347

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

wanglam
Copy link
Contributor

@wanglam wanglam commented Feb 7, 2025

Description

This PR addresses an issue with the flattenHit function modifying the original hit object array. Currently, when the flattenHit function encounters an array value within the hit object, it flattens the array in-place, directly modifying the original hit object.

The proposed changes introduce a check to determine if the value is an array. If it is, a new array is constructed with the flattened values, preserving the original hit object array. This ensures that the flattenHit function no longer mutates the original hit object, preventing potential side effects or unintended modifications.

Issues Resolved

#9318

Screenshot

image

Testing the changes

Following the reproduce steps in #9318

Changelog

  • fix: flatten hit modify original array

Check List

  • All tests pass
    • yarn test:jest
    • yarn test:jest_integration
  • New functionality includes testing.
  • New functionality has been documented.
  • Update CHANGELOG.md
  • Commits are signed per the DCO using --signoff

Copy link

codecov bot commented Feb 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 61.71%. Comparing base (21f1f73) to head (8edf8d5).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9347      +/-   ##
==========================================
+ Coverage   61.68%   61.71%   +0.02%     
==========================================
  Files        3816     3816              
  Lines       91829    91829              
  Branches    14543    14544       +1     
==========================================
+ Hits        56649    56671      +22     
+ Misses      31521    31504      -17     
+ Partials     3659     3654       -5     
Flag Coverage Δ
Linux_1 28.99% <0.00%> (-0.01%) ⬇️
Linux_2 56.46% <0.00%> (ø)
Linux_3 ?
Linux_4 28.90% <0.00%> (ø)
Windows_1 29.00% <0.00%> (?)
Windows_2 56.41% <0.00%> (ø)
Windows_3 39.19% <100.00%> (?)
Windows_4 28.90% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@wanglam wanglam requested a review from huyaboo as a code owner February 8, 2025 03:36
@@ -62,7 +62,7 @@ function flattenHit(indexPattern: IndexPattern, hit: Record<string, any>, deep:

if (hasValidMapping || isValue) {
if (!flat[key]) {
flat[key] = val;
flat[key] = Array.isArray(val) ? val.concat([]) : val;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: imo it's more readable by using the spread operator for cloning, or maybe add some context as comment.

Copy link
Member

@d-buckner d-buckner Feb 10, 2025

Choose a reason for hiding this comment

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

Just for awareness (feel free to ignore if this is known): The Array.prototype.concat API creates a shallow copy. If there are modifications that are made in the array item objects themselves, they will impact the original hits object given they share references.

Copy link
Contributor

Choose a reason for hiding this comment

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

If we need deep copy here, we may want to use structuredClone

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching that. I will refactor with the spread operator instead of Array.prototype.concat. Do you think we should use a deep copy here? This PR is for fixing the issue of the original hit being modified inside this flattenHit function. The flat[key].push in line 67 will push elements to the original array. If we want to do a deep copy, we may need to change lines 67 and 69 too.

Copy link
Member

Choose a reason for hiding this comment

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

@wanglam If the shallow copy resolves the issue then I think it makes sense to avoid deep cloning as it's relatively expensive. If in the future we see cases where the items are modified as well we can always move to using deep cloning then. Thanks for taking the time to fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants