From 7253a2dd8c3e04fbf00e57727b4e06163f80be71 Mon Sep 17 00:00:00 2001 From: Simon Kornblith Date: Wed, 3 Jun 2015 23:42:08 -0400 Subject: [PATCH] Map base fields to item-specific fields in Item#fromJSON() --- chrome/content/zotero/xpcom/data/item.js | 7 +++++-- test/tests/itemTest.js | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 5e528777ec..7acfdeb935 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -3816,7 +3816,8 @@ Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) { yield this.loadAllData(); } - this.setType(Zotero.ItemTypes.getID(json.itemType)); + let itemTypeID = Zotero.ItemTypes.getID(json.itemType); + this.setType(itemTypeID); var isValidForType = {}; var setFields = {}; @@ -3895,8 +3896,10 @@ Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) { // Item fields default: + let fieldID = Zotero.ItemFields.getID(field); isValidForType[field] = Zotero.ItemFields.isValidForType( - Zotero.ItemFields.getID(field), this.itemTypeID + Zotero.ItemFields.getFieldIDFromTypeAndBase(itemTypeID, fieldID) || fieldID, + this.itemTypeID ); if (!isValidForType[field]) { Zotero.logError("Discarding unknown JSON field " + field); diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index 0595c72052..ca0ea9c8a9 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -739,4 +739,15 @@ describe("Zotero.Item", function () { assert.isUndefined(json.numPages); }) }) + + describe("#fromJSON()", function () { + it("should map a base field to an item-specific field", function* () { + var item = new Zotero.Item("bookSection"); + yield item.fromJSON({ + "itemType":"bookSection", + "publicationTitle":"Publication Title" + }); + assert.equal(item.getField("bookTitle"), "Publication Title"); + }); + }); });