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

Commit 4f53efa

Browse files
fix: support non-alphanumeric field name (#1165)
Currently REST mode transcode doesn't support non-alphanumeric in the field name, and it cause [firestore test](https://github.com/googleapis/nodejs-firestore/blob/main/dev/system-test/firestore.ts#L754-L771) fails. code sample: ``` test() { const ref = randomCol.doc('doc'); return ref .set({'!.\\`': {'!.\\`': 'value'}}) .then(() => { return ref.get(); }) .then(doc => { // SEE ERROR ON THIS LINE expect(doc.data()).to.deep.equal({'!.\\`': {'!.\\`': 'value'}}); return ref.update(new FieldPath('!.\\`', '!.\\`'), 'new-value'); }) .then(() => { return ref.get(); }) .then(doc => { expect(doc.data()).to.deep.equal({'!.\\`': {'!.\\`': 'new-value'}}); }); } ``` Error ``` AssertionError: expected { '.': { '.': 'value' } } to deeply equal { '!.\\`': { '!.\\`': 'value' } } ```
1 parent 7c3d7f1 commit 4f53efa

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

src/util.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ function capitalize(str: string) {
3838
* camelCase (used by protobuf.js)
3939
*/
4040
export function snakeToCamelCase(str: string) {
41-
// split on spaces, non-alphanumeric, or capital letters
41+
// split on spaces, underscore, or capital letters
4242
const splitted = str
43-
.split(/(?=[A-Z])|(?:(?!\.)[\s\W_])+/)
44-
.filter(w => w.length > 0)
43+
.split(/(?=[A-Z])|(?:(?!(_(\W+)))[\s_])+/)
44+
.filter(w => w && w.length > 0)
4545
// Keep the capitalization for the first split.
4646
.map((word, index) => (index === 0 ? word : word.toLowerCase()));
4747
if (splitted.length === 0) {

test/unit/util.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ describe('util.ts', () => {
2929
assert.strictEqual(camelToSnakeCase('a.1'), 'a.1');
3030
assert.strictEqual(camelToSnakeCase('abc.1Foo'), 'abc.1_foo');
3131
assert.strictEqual(camelToSnakeCase('abc.foo'), 'abc.foo');
32+
assert.strictEqual(camelToSnakeCase('a!.\\'), 'a!.\\');
33+
assert.strictEqual(camelToSnakeCase('!._\\`'), '!._\\`');
34+
assert.strictEqual(camelToSnakeCase('a!B`'), 'a!_b`');
35+
assert.strictEqual(camelToSnakeCase('a.1B`'), 'a.1_b`');
3236
});
3337

3438
it('snakeToCamelCase', () => {
@@ -40,5 +44,9 @@ describe('util.ts', () => {
4044
assert.strictEqual(snakeToCamelCase('a.1'), 'a.1');
4145
assert.strictEqual(snakeToCamelCase('abc.1_foo'), 'abc.1Foo');
4246
assert.strictEqual(snakeToCamelCase('abc.foo'), 'abc.foo');
47+
assert.strictEqual(snakeToCamelCase('a!.\\`'), 'a!.\\`');
48+
assert.strictEqual(snakeToCamelCase('!._\\`'), '!._\\`');
49+
assert.strictEqual(snakeToCamelCase('a!_b`'), 'a!B`');
50+
assert.strictEqual(snakeToCamelCase('a.1_b`'), 'a.1B`');
4351
});
4452
});

0 commit comments

Comments
 (0)