Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d0bb18

Browse files
author
David Cheung
committed
inherit accepted from model's remoteMethod
this is needed because we added allowArray flag to persisted model's remoteMethod, but when relations try to rebuild such methods, it does not carry over such flags
1 parent 61c0c85 commit 9d0bb18

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

lib/model.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,11 @@ module.exports = function(registry) {
668668
define('__create__' + scopeName, {
669669
isStatic: isStatic,
670670
http: { verb: 'post', path: '/' + pathName },
671-
accepts: { arg: 'data', type: 'object', model: toModelName, http: { source: 'body' }},
671+
// retrieve original remoteMethod's arguments, so it properly inherits options
672+
accepts: retrieveRemoteMethodInformation.call(this, toModelName, 'create') ?
673+
retrieveRemoteMethodInformation.call(this, toModelName, 'create').accepts[0] :
674+
// fall back to default accepts, when theres no remote for modelTo
675+
{ arg: 'data', type: 'object', model: toModelName, http: { source: 'body' }},
672676
description: format('Creates a new instance in %s of this model.', scopeName),
673677
accessType: 'WRITE',
674678
returns: { arg: 'data', type: toModelName, root: true },
@@ -692,6 +696,16 @@ module.exports = function(registry) {
692696
});
693697
};
694698

699+
// helper method to extract the actual model's remoteMethod options / flags
700+
function retrieveRemoteMethodInformation(toModelName, methodName) {
701+
// has remoteClass
702+
if (!this.app._remotes._classes[toModelName]) return false;
703+
704+
return this.app._remotes._classes[toModelName]._methods.find(function(obj) {
705+
if (obj.name == methodName) return true;
706+
});
707+
}
708+
695709
/**
696710
* Enabled deeply-nested queries of related models via REST API.
697711
*

test/model.test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,72 @@ describe.onServer('Remote Methods', function() {
164164
done();
165165
});
166166
});
167+
168+
it('creates related models', function(done) {
169+
User.create({ first: 'Bob' }, function(err, res) {
170+
expect(res).to.have.property('id');
171+
var aPost = { title: 'A story', content: 'Once upon a time' };
172+
request(app)
173+
.post('/users/' + res.id + '/posts')
174+
.send(aPost)
175+
.expect('Content-Type', /json/)
176+
.expect(200)
177+
.end(function(err, result) {
178+
if (err) return done(err);
179+
expect(result.body).to.have.property('id');
180+
expect(result.body).to.have.property('title', aPost.title);
181+
expect(result.body).to.have.property('content', aPost.content);
182+
done();
183+
});
184+
});
185+
});
186+
187+
it('creates array of hasMany models', function(done) {
188+
User.create({ first: 'Bob' }, function(err, res) {
189+
expect(res).to.have.property('id');
190+
var twoPosts = [
191+
{ title: 'One story', content: 'Content #1' },
192+
{ title: 'Two story', content: 'Content #2' },
193+
];
194+
request(app)
195+
.post('/users/' + res.id + '/posts')
196+
.send(twoPosts)
197+
.expect('Content-Type', /json/)
198+
.expect(200)
199+
.end(function(err, result) {
200+
if (err) return done(err);
201+
expect(result.body.length).to.eql(2);
202+
expect(result.body).to.have.deep.property('[0].title', 'One story');
203+
expect(result.body).to.have.deep.property('[1].title', 'Two story');
204+
done();
205+
});
206+
});
207+
});
208+
209+
it('rejects array of obj input for hasOne relation', function(done) {
210+
var Friend = app.registry.createModel('friend', { name: String });
211+
app.model(Friend, { dataSource: 'db' });
212+
User.hasOne(Friend);
213+
214+
User.create({ first: 'Bob' }, function(err, res) {
215+
expect(res).to.have.property('id');
216+
var twoFriends = [
217+
{ name: 'bob' },
218+
{ name: 'rob' },
219+
];
220+
request(app)
221+
.post('/users/' + res.id + '/friend')
222+
.send(twoFriends)
223+
.expect('Content-Type', /json/)
224+
.expect(400)
225+
.end(function(err, result) {
226+
if (err) return done(err);
227+
var resError = result.body.error;
228+
expect(resError.message).to.match(/value(.*?)not(.*?)object(\.?)/i);
229+
done();
230+
});
231+
});
232+
});
167233
});
168234
// destoryAll is not exposed as a remoteMethod by default
169235
describe('Model.destroyAll(callback)', function() {

0 commit comments

Comments
 (0)