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

Do not use JSON stringify/parse functions in Helpers.getUnbindValue f… #6479

Merged
merged 1 commit into from
Jul 5, 2023

Conversation

andrewtelnov
Copy link
Member

…ix #6478

@tsv2013 tsv2013 merged commit 0be44ae into master Jul 5, 2023
10 checks passed
@tsv2013 tsv2013 deleted the bug/6478-getunbindvalue-remove-json branch July 5, 2023 13:40
@@ -162,13 +162,27 @@ export class Helpers {
return array;
}
public static getUnbindValue(value: any): any {
Copy link

@salargl salargl Jul 6, 2023

Choose a reason for hiding this comment

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

Hey @andrewtelnov thanks for the fix. But I wonder what this function tries to achieve? and what happens if we just return the value when it is an instance of Array e.g.:

public static getUnbindValue(value: any): any {
  if(Array.isArray(value)) {
    return value;
  }
  if (!!value && value instanceof Object && !(value instanceof Date)) {
    const res: any = {};
    const keys = Object.keys(value);
    keys.forEach(key => {
      const val = Helpers.getUnbindValue(value[key]);
      if(val !== undefined) {
        res[key] = val;
      }
    });
    return res;
  }
  return value;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

@salargl If you run unit tests with your code then 68 unit tests will be failed.
Here is the reason:

const val = matrixQustion.value;
val.push(newValue); //this line will modify matrixQustion.value becaue the instance is the same.

I had to modify the code to the following to fix unit tests in SurveyJS Creator V1&V2:

  public static getUnbindValue(value: any): any {
    if(Array.isArray(value)) {
      const res = [];
      for(let i = 0; i < value.length; i ++) {
        res.push(Helpers.getUnbindValue(value[i]));
      }
      return res;
    }
    if (!!value && value instanceof Object && !(value instanceof Date)) {
      return JSON.parse(JSON.stringify(value));
    }
    return value;
  }

Thank you,
Andrew

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

Successfully merging this pull request may close these issues.

3 participants