Skip to content

Commit 5259dee

Browse files
wmsMethuselah96
andauthoredSep 4, 2020
fix(redux-devtools-core): don't mutate source object during stringification (#627)
* Add failing test case for stringifyJSON * Mutate clone of value in stringifyJSON instead of original Co-authored-by: Nathan Bierema <nbierema@gmail.com>
1 parent 9be6641 commit 5259dee

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed
 

‎packages/redux-devtools-core/src/app/utils/stringifyJSON.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { DATA_TYPE_KEY, DATA_REF_KEY } from '../constants/dataTypes';
44
function replacer(key, value) {
55
if (typeof value === 'object' && value !== null && DATA_TYPE_KEY in value) {
66
const __serializedType__ = value[DATA_TYPE_KEY];
7-
delete value[DATA_TYPE_KEY]; // eslint-disable-line no-param-reassign
8-
const r = { data: value, __serializedType__ };
9-
if (DATA_REF_KEY in value) r.__serializedRef__ = value[DATA_REF_KEY];
7+
const clone = { ...value };
8+
delete clone[DATA_TYPE_KEY]; // eslint-disable-line no-param-reassign
9+
const r = { data: clone, __serializedType__ };
10+
if (DATA_REF_KEY in value) r.__serializedRef__ = clone[DATA_REF_KEY];
1011
return r;
1112
}
1213
return value;

‎packages/redux-devtools-core/test/app.spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import App from '../src/app/containers/App';
77
import api from '../src/app/middlewares/api';
88
import exportState from '../src/app/middlewares/exportState';
99
import rootReducer from '../src/app/reducers';
10+
import { DATA_TYPE_KEY } from '../src/app/constants/dataTypes';
11+
import stringifyJSON from '../src/app/utils/stringifyJSON';
1012

1113
let wrapper;
1214

@@ -43,3 +45,25 @@ describe('App container', () => {
4345
).toMatch(/<div class="actionListRows-\d-\d-\d+"><\/div>/);
4446
});
4547
});
48+
49+
describe('stringifyJSON', () => {
50+
it('should not mutate the source object', () => {
51+
const src = {
52+
isTest: true,
53+
[DATA_TYPE_KEY]: 'Test',
54+
};
55+
56+
const result = {
57+
data: {
58+
isTest: true,
59+
},
60+
__serializedType__: 'Test',
61+
};
62+
63+
expect(stringifyJSON(src, true)).toEqual(JSON.stringify(result));
64+
expect(src).toEqual({
65+
isTest: true,
66+
[DATA_TYPE_KEY]: 'Test',
67+
});
68+
});
69+
});

0 commit comments

Comments
 (0)
Please sign in to comment.