diff --git a/lib/transport/binary/protocol28/serializer.js b/lib/transport/binary/protocol28/serializer.js index b2e6933..5d9b993 100644 --- a/lib/transport/binary/protocol28/serializer.js +++ b/lib/transport/binary/protocol28/serializer.js @@ -69,7 +69,7 @@ function serializeDocument (document, isMap) { function serializeValue (value) { var type = typeof value; if (type === 'string') { - return '"' + value.replace(/\\/, "\\\\").replace(/"/g, '\\"') + '"'; + return '"' + value.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + '"'; } else if (type === 'number') { return ~value.toString().indexOf('.') ? value + 'f' : value; diff --git a/test/bugs/328-wrong-backslash-quoting.js b/test/bugs/328-wrong-backslash-quoting.js new file mode 100644 index 0000000..3cca293 --- /dev/null +++ b/test/bugs/328-wrong-backslash-quoting.js @@ -0,0 +1,27 @@ +describe("Bug #328: wrong serialization of fields with multiple backslash characters", function () { + before(function () { + return CREATE_TEST_DB(this, 'testdb_bug_328') + .bind(this) + .then(function () { + return this.db.class.create('TestSerializeBackslash'); + }); + }); + after(function () { + return DELETE_TEST_DB('testdb_bug_328'); + }); + + it('should insert a document with correct quotes for backslashes', function () { + return this.db + .insert() + .into('TestSerializeBackslash') + .set({ + foo: 'kratke, , nadherne, proste bozi, chjo som sa ostrihal :\\\\', + bar: '>' + }) + .one() + .then(function (res) { + res.foo.should.equal('kratke, , nadherne, proste bozi, chjo som sa ostrihal :\\\\'); + }); + }); + +});