Skip to content

Commit 92592b3

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

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-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
}

test/belongsToPolymorphic.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ describe('loopback json api belongsTo polymorphic relationships', function () {
4040
})
4141

4242
describe('File belonging to a Post', function () {
43+
var postId
44+
4345
beforeEach(function (done) {
4446
Post.create(
4547
{
@@ -48,6 +50,7 @@ describe('loopback json api belongsTo polymorphic relationships', function () {
4850
},
4951
function (err, post) {
5052
expect(err).to.equal(null)
53+
postId = post.id
5154
FileModel.create(
5255
{
5356
fileName: 'blah.jpg',
@@ -105,5 +108,38 @@ describe('loopback json api belongsTo polymorphic relationships', function () {
105108
})
106109
}
107110
)
111+
112+
it(
113+
'should define a relationship to Post when file is created via API',
114+
function (done) {
115+
request(app)
116+
.post('/fileModels')
117+
.send({
118+
data: {
119+
type: 'fileModels',
120+
attributes: {
121+
fileName: 'blah.jpg'
122+
},
123+
relationships: {
124+
parent: {
125+
data: {
126+
id: postId,
127+
type: 'posts'
128+
}
129+
}
130+
}
131+
}
132+
})
133+
.set('accept', 'application/vnd.api+json')
134+
.set('content-type', 'application/json')
135+
.expect(201)
136+
.end(function (err, res) {
137+
expect(err).to.equal(null)
138+
expect(res.body).to.not.have.key('errors')
139+
expect(res.body.data.relationships.parent).to.be.an('object')
140+
done()
141+
})
142+
}
143+
)
108144
})
109145
})

test/hasManyPolymorphic.test.js

+45
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,50 @@ describe('loopback json api hasMany polymorphic relationships', function () {
103103
})
104104
}
105105
)
106+
107+
it(
108+
'should define a relationship to Resources when Post is created via API',
109+
function (done) {
110+
Resource.create(
111+
{
112+
fileName: 'blah.jpg'
113+
},
114+
function (err, resource) {
115+
expect(err).to.equal(null)
116+
request(app)
117+
.post('/posts')
118+
.send({
119+
data: {
120+
type: 'posts',
121+
attributes: {
122+
fileName: 'blah.jpg'
123+
},
124+
relationships: {
125+
resources: {
126+
data: [
127+
{
128+
id: resource.id,
129+
type: 'resources'
130+
}
131+
]
132+
}
133+
}
134+
}
135+
})
136+
.set('accept', 'application/vnd.api+json')
137+
.set('content-type', 'application/json')
138+
.expect(201)
139+
.end(function (err, res) {
140+
expect(err).to.equal(null)
141+
expect(res.body).to.not.have.key('errors')
142+
expect(res.body.data.relationships.resources).to.be.an(
143+
'object'
144+
)
145+
done()
146+
})
147+
}
148+
)
149+
}
150+
)
106151
})
107152
})

0 commit comments

Comments
 (0)