Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/deserializer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

var _ = require('lodash')
var utils = require('./utils')

function defaultBeforeDeserialize (options, cb) {
cb(null, options)
Expand Down Expand Up @@ -78,6 +79,20 @@ function belongsToRelationships (options) {
if (!relationship.data) {
options.result[fkName] = null
} else {
if (serverRelation.polymorphic) {
// Find the model which has a plural matching 'data.type'
// Allow case insensitive match
var relatedType = relationship.data.type.toLowerCase()
var modelName = _.findKey(model.app.models, function (model) {
var plural = utils.pluralForModel(model)
return plural.toLowerCase() === relatedType
})
if (!modelName) {
return false
}
var discriminator = serverRelation.polymorphic.discriminator
options.result[discriminator] = modelName
}
options.result[fkName] = relationship.data.id
}
})
Expand Down
8 changes: 6 additions & 2 deletions lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,12 @@ function parseRelations (data, relations, options) {
var toType = ''
if (relation.polymorphic && utils.relationFkOnModelFrom(relation)) {
var discriminator = relation.polymorphic.discriminator
var model = options.app.models[data[discriminator]]
toType = utils.pluralForModel(model)
if (data[discriminator]) {
var model = options.app.models[data[discriminator]]
if (model) {
toType = utils.pluralForModel(model)
}
}
} else {
toType = utils.pluralForModel(relation.modelTo)
}
Expand Down
97 changes: 97 additions & 0 deletions test/belongsToPolymorphic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,101 @@ describe('loopback json api belongsTo polymorphic relationships', function () {
}
)
})

describe('File with no relationship to a Post', function () {
beforeEach(function (done) {
FileModel.create(
{
fileName: 'blah.jpg'
},
done
)
})

it('should return a null value for relationship data', function (done) {
request(app).get('/fileModels/1').end(function (err, res) {
expect(err).to.equal(null)
expect(res.body).to.not.have.key('errors')
expect(res.body.data.relationships.parent).to.be.an('object')
expect(res.body.data.relationships.parent.data).to.equal(null)
done()
})
})
})

describe('Create relationship via API', function () {
beforeEach(function (done) {
Post.create(
{
title: 'Post One',
content: 'Content'
},
done
)
})

it('should define a relationship to Post when file is created', function (
done
) {
request(app)
.post('/fileModels')
.send({
data: {
type: 'fileModels',
attributes: {
fileName: 'blah.jpg'
},
relationships: {
parent: {
data: {
id: 1,
type: 'posts'
}
}
}
}
})
.set('accept', 'application/vnd.api+json')
.set('content-type', 'application/json')
.expect(201)
.end(function (err, res) {
expect(err).to.equal(null)
expect(res.body).to.not.have.key('errors')
expect(res.body.data.relationships.parent).to.be.an('object')
expect(res.body.data.relationships.parent.data).to.be.an('object')
done()
})
})

it('should ignore relationships with an invalid type', function (done) {
request(app)
.post('/fileModels')
.send({
data: {
type: 'fileModels',
attributes: {
fileName: 'blah.jpg'
},
relationships: {
parent: {
data: {
id: 1,
type: 'invalidType'
}
}
}
}
})
.set('accept', 'application/vnd.api+json')
.set('content-type', 'application/json')
.expect(201)
.end(function (err, res) {
expect(err).to.equal(null)
expect(res.body).to.not.have.key('errors')
expect(res.body.data.relationships.parent).to.be.an('object')
expect(res.body.data.relationships.parent.data).to.equal(null)
done()
})
})
})
})
56 changes: 56 additions & 0 deletions test/hasManyPolymorphic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,60 @@ describe('loopback json api hasMany polymorphic relationships', function () {
}
)
})

describe('Create relationship via API', function () {
beforeEach(function (done) {
Resource.create(
{
fileName: 'blah.jpg'
},
done
)
})

it(
'should define a relationship to Resources when Post is created',
function (done) {
Resource.create(
{
fileName: 'blah.jpg'
},
function (err, resource) {
expect(err).to.equal(null)
request(app)
.post('/posts')
.send({
data: {
type: 'posts',
attributes: {
fileName: 'blah.jpg'
},
relationships: {
resources: {
data: [
{
id: 1,
type: 'resources'
}
]
}
}
}
})
.set('accept', 'application/vnd.api+json')
.set('content-type', 'application/json')
.expect(201)
.end(function (err, res) {
expect(err).to.equal(null)
expect(res.body).to.not.have.key('errors')
expect(res.body.data.relationships.resources).to.be.an(
'object'
)
done()
})
}
)
}
)
})
})