Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 5a1b733

Browse files
committed
Merge pull request #406 from firebase/kato-400
Fixes #400 - strip deeply nested $ prefixes when saving json
2 parents adfdd00 + e4c62ea commit 5a1b733

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/utils.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@
290290
else {
291291
dat = {};
292292
utils.each(rec, function (v, k) {
293-
dat[k] = v;
293+
dat[k] = stripDollarPrefixedKeys(v);
294294
});
295295
}
296296
if( angular.isDefined(rec.$value) && Object.keys(dat).length === 0 && rec.$value !== null ) {
@@ -316,4 +316,15 @@
316316
return utils;
317317
}
318318
]);
319+
320+
function stripDollarPrefixedKeys(data) {
321+
if( !angular.isObject(data) ) { return data; }
322+
var out = angular.isArray(data)? [] : {};
323+
angular.forEach(data, function(v,k) {
324+
if(typeof k !== 'string' || k.charAt(0) !== '$') {
325+
out[k] = stripDollarPrefixedKeys(v);
326+
}
327+
});
328+
return out;
329+
}
319330
})();

tests/unit/utils.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,30 @@ describe('$firebaseUtils', function () {
121121
expect($utils.toJSON(json)).toEqual({foo: json.foo});
122122
});
123123

124+
it('should remove any deeply nested variables prefixed with $', function() {
125+
var json = {
126+
arr: [[
127+
{$$hashKey: false, $key: 1, alpha: 'alpha', bravo: {$$private: '$$private', $key: '$key', bravo: 'bravo'}},
128+
{$$hashKey: false, $key: 1, alpha: 'alpha', bravo: {$$private: '$$private', $key: '$key', bravo: 'bravo'}}
129+
130+
], ["a", "b", {$$key: '$$key'}]],
131+
obj: {
132+
nest: {$$hashKey: false, $key: 1, alpha: 'alpha', bravo: {$$private: '$$private', $key: '$key', bravo: 'bravo'} }
133+
}
134+
};
135+
136+
expect($utils.toJSON(json)).toEqual({
137+
arr: [[
138+
{alpha: 'alpha', bravo: {bravo: 'bravo'}},
139+
{alpha: 'alpha', bravo: {bravo: 'bravo'}}
140+
141+
], ["a", "b", {}]],
142+
obj: {
143+
nest: {alpha: 'alpha', bravo: {bravo: 'bravo'} }
144+
}
145+
});
146+
});
147+
124148
it('should throw error if an invalid character in key', function() {
125149
expect(function() {
126150
$utils.toJSON({'foo.bar': 'foo.bar'});

0 commit comments

Comments
 (0)