Skip to content

replaceNumberToString used for custom option fails for empty #4217

@carlokok

Description

@carlokok

Current behavior

When having a checkbox custom option and not selecting any option, the value is null, but:

const replaceNumberToString = obj => {
  Object.keys(obj).forEach(key => {
    if (typeof obj[key] === 'object') {
      return replaceNumberToString(obj[key]);
    }
    obj[key] = String(obj[key]);
  });
  return obj;
}

fails because obj[key] == null and typeof null = 'object'. A simple fix would be to do:
if (obj[key] && typeof obj[key] === 'object') {

However the return under it makes no sense either.

I can't find what the original intent was of the code, but if it's to turn all integer keys in the object into a string recursively then something like:

const replaceNumberToString = obj => {
  Object.keys(obj).forEach(key => {
    if (obj[key]) {
      if (typeof obj[key] === 'object') {
        obj[key] = replaceNumberToString(obj[key]);
      } else 
        obj[key] = String(obj[key]);
    }
  });
  return obj;
}

would work a lot better.

Expected behavior

No exception due to a null.

Steps to reproduce the issue

Create a product custom options, use option type "checkbox" and add 1 or more values. Open the product page, add it to the cart without checking any boxes.

Repository

https://github.com/DivanteLtd/vue-storefront/blob/master/core/modules/cart/helpers/productChecksum.ts#L10

Can you handle fixing this bug by yourself?

  • YES
  • [ x] NO

Which Release Cycle state this refers to? Info for developer.

Pick one option.

  • This is a bug report for test version on https://test.storefrontcloud.io - In this case Developer should create branch from develop branch and create Pull Request 2. Feature / Improvement back to develop.
  • This is a bug report for current Release Candidate version on https://next.storefrontcloud.io - In this case Developer should create branch from release branch and create Pull Request 3. Stabilisation fix back to release.
  • This is a bug report for current Stable version on https://demo.storefrontcloud.io and should be placed in next stable version hotfix - In this case Developer should create branch from hotfix or master branch and create Pull Request 4. Hotfix back to hotfix.

Environment details

  • Browser: Chrome
  • OS: Windows 10
  • Node: v13.7.0
  • Code Version: HEAD of develop

Metadata

Metadata

Assignees

Labels

P3: NormalPriority mark - normal prioritybugBug reports

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions