From ab8d82d7f9743798eccba92ce406cf6fefa271f8 Mon Sep 17 00:00:00 2001
From: Raymond Feng <raymond@strongloop.com>
Date: Thu, 24 Jul 2014 10:26:49 -0700
Subject: [PATCH] Fix remoting types for related models

---
 lib/models/model.js | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/lib/models/model.js b/lib/models/model.js
index cc860e61..c5fb92c2 100644
--- a/lib/models/model.js
+++ b/lib/models/model.js
@@ -136,7 +136,8 @@ Model.setup = function () {
 
   var idDesc = ModelCtor.modelName + ' id';
   ModelCtor.sharedCtor.accepts = [
-    {arg: 'id', type: 'any', http: {source: 'path'}, description: idDesc}
+    {arg: 'id', type: 'any', required: true, http: {source: 'path'},
+      description: idDesc}
     // {arg: 'instance', type: 'object', http: {source: 'body'}}
   ];
 
@@ -347,6 +348,7 @@ Model.belongsToRemoting = function(relationName, relation, define) {
 }
 
 Model.hasManyRemoting = function (relationName, relation, define) {
+  var toModelName = relation.modelTo.modelName;
   var findByIdFunc = this.prototype['__findById__' + relationName];
   define('__findById__' + relationName, {
     isStatic: false,
@@ -355,7 +357,7 @@ Model.hasManyRemoting = function (relationName, relation, define) {
       description: 'Foreign key for ' + relationName, required: true,
       http: {source: 'path'}},
     description: 'Find a related item by id for ' + relationName,
-    returns: {arg: 'result', type: relation.modelTo.modelName, root: true}
+    returns: {arg: 'result', type: toModelName, root: true}
   }, findByIdFunc);
 
   var destroyByIdFunc = this.prototype['__destroyById__' + relationName];
@@ -373,11 +375,14 @@ Model.hasManyRemoting = function (relationName, relation, define) {
   define('__updateById__' + relationName, {
     isStatic: false,
     http: {verb: 'put', path: '/' + relationName + '/:fk'},
-    accepts: {arg: 'fk', type: 'any',
-      description: 'Foreign key for ' + relationName, required: true,
-      http: {source: 'path'}},
+    accepts: [
+      {arg: 'fk', type: 'any',
+        description: 'Foreign key for ' + relationName, required: true,
+        http: {source: 'path'}},
+      {arg: 'data', type: toModelName, http: {source: 'body'}}
+    ],
     description: 'Update a related item by id for ' + relationName,
-    returns: {arg: 'result', type: relation.modelTo.modelName, root: true}
+    returns: {arg: 'result', type: toModelName, root: true}
   }, updateByIdFunc);
 
   if (relation.modelThrough) {
@@ -432,7 +437,7 @@ Model.scopeRemoting = function(relationName, relation, define) {
   define('__create__' + relationName, {
     isStatic: false,
     http: {verb: 'post', path: '/' + relationName},
-    accepts: {arg: 'data', type: 'object', http: {source: 'body'}},
+    accepts: {arg: 'data', type: toModelName, http: {source: 'body'}},
     description: 'Creates a new instance in ' + relationName + ' of this model.',
     returns: {arg: 'data', type: toModelName, root: true}
   });