Skip to content

Commit 2b4e42d

Browse files
committed
fix: correctly escape field paths with multiple backslashes or backticks
Previously, only the first backslash and backtick was being escaped. This broke `update` and `set (merge)` calls when a field path contained multiple backslashes. `replaceAll` is an alternative here, but I think a global regex has better compatibility with older versions of Node.
1 parent bef5668 commit 2b4e42d

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

dev/src/path.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ export class FieldPath extends Path<FieldPath> implements firestore.FieldPath {
621621
.map(str => {
622622
return UNESCAPED_FIELD_NAME_RE.test(str)
623623
? str
624-
: '`' + str.replace('\\', '\\\\').replace('`', '\\`') + '`';
624+
: '`' + str.replace(/\\/g, '\\\\').replace(/`/g, '\\`') + '`';
625625
})
626626
.join('.');
627627
}

dev/test/path.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,23 @@ describe('ResourcePath', () => {
6868

6969
describe('FieldPath', () => {
7070
it('encodes field names', () => {
71-
const components = [['foo'], ['foo', 'bar'], ['.', '`'], ['\\']];
72-
73-
const results = ['foo', 'foo.bar', '`.`.`\\``', '`\\\\`'];
71+
const components = [
72+
['foo'],
73+
['foo', 'bar'],
74+
['.', '`'],
75+
['\\'],
76+
['\\\\'],
77+
['``'],
78+
];
79+
80+
const results = [
81+
'foo',
82+
'foo.bar',
83+
'`.`.`\\``',
84+
'`\\\\`',
85+
'`\\\\\\\\`',
86+
'`\\`\\``',
87+
];
7488

7589
for (let i = 0; i < components.length; ++i) {
7690
expect(new FieldPath(...components[i]).toString()).to.equal(results[i]);

0 commit comments

Comments
 (0)