Skip to content

Commit 94c439d

Browse files
committed
fix(deserializer): unable to create polymorphic relationship #298
1 parent 40f9282 commit 94c439d

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lib/deserializer.js

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
var _ = require('lodash')
4+
var utils = require('./utils')
45

56
function defaultBeforeDeserialize (options, cb) {
67
cb(null, options)
@@ -79,6 +80,20 @@ function belongsToRelationships (options) {
7980
options.result[fkName] = null
8081
} else {
8182
options.result[fkName] = relationship.data.id
83+
if (serverRelation.polymorphic) {
84+
// Find the model which has a plural matching 'data.type'
85+
// Allow case insensitive match
86+
var relatedType = relationship.data.type.toLowerCase()
87+
var modelName = _.findKey(model.app.models, function (model) {
88+
var plural = utils.pluralForModel(model)
89+
return plural.toLowerCase() === relatedType
90+
})
91+
if (!modelName) {
92+
return false
93+
}
94+
var discriminator = serverRelation.polymorphic.discriminator
95+
options.result[discriminator] = modelName
96+
}
8297
}
8398
})
8499
}

0 commit comments

Comments
 (0)