Skip to content

Commit 5e5b236

Browse files
committed
Merge pull request #77 from JonathanPrince/master
bugfix for PK name for related models
2 parents 25ced2d + e48e6fe commit 5e5b236

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

lib/serialize.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ module.exports = function (app, defaults) {
2020
var data,
2121
type,
2222
options,
23-
relatedModelName,
2423
modelNamePlural,
2524
relatedModelPlural,
2625
relations,
@@ -63,24 +62,21 @@ module.exports = function (app, defaults) {
6362
return next();
6463
}
6564

66-
//match on __GET__, etc.
67-
if (ctx.methodString.match(/.*\.__.*__.*/)) {
68-
//get the model name of the related model in plural form.
69-
//we cant just get the relationship name because the name of
70-
//the relationship may not match the related model plural.
71-
//eg. /posts/1/author could actually be a user model so we
72-
//would want type = 'users'
73-
74-
//WARNING: feels fragile but functional.
75-
relatedModelName = ctx.method.returns[0].type;
76-
relatedModelPlural = utils.pluralForModel(app.models[relatedModelName]);
65+
var primaryKeyField = utils.primaryKeyForModel(model);
66+
var regexRelationFromUrl = /\/.*\/(.*$)/g;
67+
var regexMatches = regexRelationFromUrl.exec(ctx.req.path);
68+
var relationName = (regexMatches && regexMatches[1]) ? regexMatches[1] : null;
69+
70+
var relation = model.relations[relationName];
71+
if (relationName && relation) {
72+
relatedModelPlural = utils.pluralForModel(relation.modelTo);
73+
primaryKeyField = utils.primaryKeyForModel(relation.modelTo);
74+
7775
if (relatedModelPlural) {
7876
type = relatedModelPlural;
7977
}
8078
}
8179

82-
var primaryKeyField = utils.primaryKeyForModel(model);
83-
8480
options = {
8581
primaryKeyField: primaryKeyField,
8682
host: utils.hostFromContext(ctx),

test/belongsTo.test.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,36 @@ describe('loopback json api belongsTo relationships', function () {
8686
describe('Comment with a post and relationship back to comment', function (done) {
8787
beforeEach(function (done) {
8888
Post.hasMany(Comment, {as: 'comments', foreignKey: 'postId'});
89-
Comment.create({
90-
title: 'my comment',
91-
comment: 'my post comment'
92-
}, function (err, comment) {
89+
Comment.create([{
90+
title: 'my comment 1',
91+
comment: 'my post comment 1'
92+
}, {
93+
title: 'my comment 2',
94+
comment: 'my post comment 2'
95+
}], function (err, comments) {
9396
expect(err).to.equal(null);
94-
comment.post.create({
97+
comments[1].post.create({
9598
title: 'My post',
9699
content: 'My post content'
97100
}, done);
98101
});
99102
});
100103

101-
describe('GET /comments/1/post', function () {
102-
it('should display correct relationships', function (done) {
103-
request(app).get('/comments/1/post')
104+
describe('should display correct relationships', function () {
105+
it('GET /comments/2/post', function (done) {
106+
request(app).get('/comments/2/post')
107+
.end(function (err, res) {
108+
expect(err).to.equal(null);
109+
expect(res.body.data.relationships.comments).to.be.an('object');
110+
expect(res.body.data.relationships.comments.links).to.be.an('object');
111+
expect(res.body.data.relationships.comments.links.related).to.match(/^http.*\/posts\/1\/comments$/);
112+
expect(res.body.data.relationships.post).to.be.undefined;
113+
done();
114+
});
115+
});
116+
117+
it('GET /posts/1', function (done) {
118+
request(app).get('/posts/1')
104119
.end(function (err, res) {
105120
expect(err).to.equal(null);
106121
expect(res.body.data.relationships.comments).to.be.an('object');

0 commit comments

Comments
 (0)