-
Notifications
You must be signed in to change notification settings - Fork 953
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
base: main
Are you sure you want to change the base?
Fix flatten hit modify original array #9347
Conversation
Signed-off-by: Lin Wang <[email protected]>
Codecov ReportAll modified and coverable lines are covered by tests ✅
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Signed-off-by: Lin Wang <[email protected]>
@@ -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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Description
This PR addresses an issue with the
flattenHit
function modifying the original hit object array. Currently, when theflattenHit
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
Testing the changes
Following the reproduce steps in #9318
Changelog
Check List
yarn test:jest
yarn test:jest_integration