From 33ef7b1641cae7cc03e9540dc2beee5654bba23f Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 11 Apr 2023 23:48:07 -0400 Subject: [PATCH] Prevent setting search `.name` to empty value Prevents bug in zotero-citation plugin (at least on macOS) from creating a search that breaks syncing We were already checking for a missing name in `saveTx()`, but the plugin is saving the same search twice in rapid succession, the second time without a name, and the second attempt clears the search object's name value after the first save's `_initSave()` check and before its SQL write. The second save fails, but the first save goes through without a name, resulting in a sync error. https://forums.zotero.org/discussion/104274/id-1702002152-cannot-sync https://github.com/MuiseDestiny/zotero-citation/issues/31 --- chrome/content/zotero/xpcom/data/search.js | 7 ++++++- test/tests/searchTest.js | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/chrome/content/zotero/xpcom/data/search.js b/chrome/content/zotero/xpcom/data/search.js index 84ab907226..7a26959709 100644 --- a/chrome/content/zotero/xpcom/data/search.js +++ b/chrome/content/zotero/xpcom/data/search.js @@ -75,7 +75,12 @@ Zotero.defineProperty(Zotero.Search.prototype, 'key', { }); Zotero.defineProperty(Zotero.Search.prototype, 'name', { get: function() { return this._get('name'); }, - set: function(val) { return this._set('name', val); } + set: function (val) { + if (!val) { + throw new Error("Saved search name cannot be empty"); + } + return this._set('name', val); + } }); Zotero.defineProperty(Zotero.Search.prototype, 'version', { get: function() { return this._get('version'); }, diff --git a/test/tests/searchTest.js b/test/tests/searchTest.js index da791f1e2b..e6f3891239 100644 --- a/test/tests/searchTest.js +++ b/test/tests/searchTest.js @@ -1,4 +1,11 @@ describe("Zotero.Search", function() { + describe("#name", function () { + it("should fail if empty", async function () { + var s = new Zotero.Search(); + assert.throws(() => s.name = ''); + }); + }); + describe("#addCondition()", function () { it("should convert old-style 'collection' condition value", function* () { var col = yield createDataObject('collection');