From d28e324a7f5f6d6a21f45cda61ec30018a29bf31 Mon Sep 17 00:00:00 2001 From: Abhilash Murthy Date: Wed, 13 Jan 2021 16:26:21 +0800 Subject: [PATCH 1/2] [BUGFIX] include may not have a .relation and just be a { [modelName]: true } sometimes --- lib/utilities/relationship-utils.js | 2 +- lib/utils.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/utilities/relationship-utils.js b/lib/utilities/relationship-utils.js index b30a365..b1d9b71 100644 --- a/lib/utilities/relationship-utils.js +++ b/lib/utilities/relationship-utils.js @@ -45,7 +45,7 @@ function getInvalidIncludesError (message) { } function isLoopbackInclude (ctx) { - return ctx.args && ctx.args.filter + return ctx.args && ctx.args.filter && ctx.args.filter.include } function isJSONAPIInclude (req) { diff --git a/lib/utils.js b/lib/utils.js index 3efac19..6e04ebd 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -292,7 +292,7 @@ function setRequestedIncludes (include) { } if (inc instanceof Object) { - return inc.relation + return inc.relation || _.findKey(inc, value => value === true) } }) } From 1d61d900370eb649f2c833038635f4ef92ff3409 Mon Sep 17 00:00:00 2001 From: Abhilash Murthy Date: Tue, 7 Jan 2025 21:41:33 +0800 Subject: [PATCH 2/2] fix(deserializer): unable to create polymorphic relationship --- lib/deserializer.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/deserializer.js b/lib/deserializer.js index feb10a8..f4ecceb 100644 --- a/lib/deserializer.js +++ b/lib/deserializer.js @@ -1,6 +1,7 @@ 'use strict' var _ = require('lodash') +var utils = require('./utils') function defaultBeforeDeserialize (options, cb) { cb(null, options) @@ -78,6 +79,17 @@ function belongsToRelationships (options) { if (!relationship.data) { options.result[fkName] = null } else { + // https://github.com/digitalsadhu/loopback-component-jsonapi/compare/master...grahambates:loopback-component-jsonapi:master + if (serverRelation.polymorphic) { + var relatedType = relationship.data.type + var modelClass = _.find(model.app.models, function (model) { + var plural = utils.pluralForModel(model) + return plural === relatedType + }) + + var discriminator = serverRelation.polymorphic.discriminator + options.result[discriminator] = modelClass.modelName + } options.result[fkName] = relationship.data.id } })