Fix Filter decorator with nested key paths of null #64

Closed
dbachrach wants to merge 1 commit from b-filter-decorated-null-nested-paths into master
dbachrach commented 2015-12-18 23:50:18 +00:00 (Migrated from github.com)

There is a bug in the filter decorator when using nested key paths. If the nested key path value is null. The key path one level higher is written. As an example:

const simpleEngine = {
  save(state) {
    console.log('final state:', state);
  }
};

const basicFilteredEngine = storage.decorators.filter(simpleEngine, [
  ['a']
]);

const nestedFilteredEngine = storage.decorators.filter(simpleEngine, [
  ['a', 'first']
]);

const data = {
  a: {
    first: null,
    second: 2
  }
};

console.log('Simple Engine');
simpleEngine.save(data);

console.log('Basic Filtered Engine');
basicFilteredEngine.save(data);

console.log('Nested Filtered Engine');
nestedFilteredEngine.save(data);

This prints out:

Simple Engine
final state: {
    a: {
        first: null,
        second: 2
    }
}
Basic Filtered Engine
final state: {
    a: {
        first: null,
        second: 2
    }
}
Nested Filtered Engine
final state: {
    a: {
        first: {
            first: null,
            second: 2
        }
    }
}

As you can see, when the value of a.first is null, the saved data structure is corrupted.

This PR ensures that a null or undefined value will properly set the state tree.

There is a bug in the filter decorator when using nested key paths. If the nested key path value is `null`. The key path one level higher is written. As an example: ``` js const simpleEngine = { save(state) { console.log('final state:', state); } }; const basicFilteredEngine = storage.decorators.filter(simpleEngine, [ ['a'] ]); const nestedFilteredEngine = storage.decorators.filter(simpleEngine, [ ['a', 'first'] ]); const data = { a: { first: null, second: 2 } }; console.log('Simple Engine'); simpleEngine.save(data); console.log('Basic Filtered Engine'); basicFilteredEngine.save(data); console.log('Nested Filtered Engine'); nestedFilteredEngine.save(data); ``` This prints out: ``` Simple Engine final state: { a: { first: null, second: 2 } } Basic Filtered Engine final state: { a: { first: null, second: 2 } } Nested Filtered Engine final state: { a: { first: { first: null, second: 2 } } } ``` As you can see, when the value of `a.first` is `null`, the saved data structure is corrupted. This PR ensures that a null or undefined value will properly set the state tree.
michaelcontento commented 2016-01-04 10:09:02 +00:00 (Migrated from github.com)

Fixed with working tests in b7bf6e5

Fixed with working tests in b7bf6e5

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MichaelContento/redux-storage!64
No description provided.