From d01160a9f4f8e45774102b25f3dea712ca78a205 Mon Sep 17 00:00:00 2001 From: Cole-Greer Date: Fri, 6 Dec 2024 13:47:16 -0800 Subject: [PATCH] Fix js translator only escaping first occurance of single-quote CTR --- .../gremlin-javascript/lib/process/translator.js | 4 ++-- .../gremlin-javascript/test/unit/translator-test.js | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/translator.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/translator.js index 7eceb258c4..fb58907cf1 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/translator.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/translator.js @@ -91,7 +91,7 @@ class Translator { } script += `('${key}', `; if (anyObject[key] instanceof String || typeof anyObject[key] === 'string') { - script += `'${anyObject[key]}'`; + script += `'${`${anyObject[key]}`.replaceAll("'", "\\'")}'`; // eslint-disable-line quotes } else { script += anyObject[key]; } @@ -111,7 +111,7 @@ class Translator { } else if (typeof anyObject === 'number' || typeof anyObject === 'boolean') { script += anyObject; } else { - script += `'${`${anyObject}`.replace("'", "\\'")}'`; // eslint-disable-line quotes + script += `'${`${anyObject}`.replaceAll("'", "\\'")}'`; // eslint-disable-line quotes } return script; diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/translator-test.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/translator-test.js index 986dbd161a..a91742fc25 100644 --- a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/translator-test.js +++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/translator-test.js @@ -133,5 +133,13 @@ describe('Translator', function () { assert.ok(script); assert.strictEqual(script, 'g.addV(\'test\').property(\'quotes\', \'some "quotes\\\' in the middle.\')'); }); + + it('should properly escape quotes in Object values', function () { + const g = new graph.Graph().traversal(); + const o = {key: "some \"quotes' in the middle."} + const script = new Translator('g').translate(g.inject(o)); + assert.ok(script); + assert.strictEqual(script, 'g.inject((\'key\', \'some "quotes\\\' in the middle.\'))'); + }); }); });