Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: escape single quotation mark in json merge values #430

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,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).replace(/'/g, "\\'")}')`);
}
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
Loading