Skip to content

trackProperties method in immutableStateInvariantMiddleware.ts is not correctly managing the circularity #4843

Open
@paztis

Description

@paztis

https://github.com/reduxjs/redux-toolkit/blob/master/packages/toolkit/src/immutableStateInvariantMiddleware.ts#L57

function trackProperties(
  isImmutable: IsImmutableFunc,
  ignorePaths: IgnorePaths = [],
  obj: Record<string, any>,
  path: string = '',
  checkedObjects: Set<Record<string, any>> = new Set(),
) {
  const tracked: Partial<TrackedProperty> = { value: obj }

  if (!isImmutable(obj) && !checkedObjects.has(obj)) {
    checkedObjects.add(obj)
    tracked.children = {}

    for (const key in obj) {
      const childPath = path ? path + '.' + key : key
      if (ignorePaths.length && ignorePaths.indexOf(childPath) !== -1) {
        continue
      }

      tracked.children[key] = trackProperties(
        isImmutable,
        ignorePaths,
        obj[key],
        childPath,
      )
    }
  }
  return tracked as TrackedProperty
}

recursive call to trackProperties (line 57) is not passing down the checkedObjects argument.
It means all the circularity check you do in your code is totally bypass.

That's why immutable is not working with circular references

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions