Skip to content

Commit

Permalink
fix: escape single quotation mark in json merge values
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjake committed Jan 8, 2025
1 parent 2e31e11 commit aa90e4d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const deepEqual = require('deep-equal');
const debug = require('debug')('leoric');
const pluralize = require('pluralize');
const { executeValidator, LeoricValidateError } = require('./validator');
// const { escape } = require('sqlstring');
require('reflect-metadata');

const { default: DataTypes } = require('./data_types');
Expand Down Expand Up @@ -1580,7 +1581,7 @@ class Bone {
const method = preserve ? 'JSON_MERGE_PRESERVE' : 'JSON_MERGE_PATCH';
const data = { ...values };
for (const [name, value] of Object.entries(values)) {
data[name] = new Raw(`${method}(${name}, '${JSON.stringify(value)}')`);
data[name] = new Raw(`${method}(${name}, '${JSON.stringify(value).replaceAll("'", "\\'")}')`);
}
return this.update(conditions, data, restOptions);
}
Expand Down
6 changes: 6 additions & 0 deletions test/integration/suite/json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ describe('=> Basic', () => {
assert.equal(gen.extra.a, 3);
});

it('Bone.jsonMerge(conditions, values, options) should escape values', async () => {
const gen = await Gen.create({ name: '章3️⃣疯', extra: {} });
await Gen.jsonMerge({ id: gen.id }, { extra: { a: "foo\'bar" } });
await gen.reload();
assert.equal(gen.extra.a, 'foo\'bar');
});

it('Bone.jsonMergePreserve(conditions, values, options) should work', async () => {
const gen = await Gen.create({ name: '章3️⃣疯', extra: {} });
Expand Down

0 comments on commit aa90e4d

Please sign in to comment.