From 6570b948434392a03271fd83844019b741aa2052 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 27 Oct 2017 15:17:58 -0700 Subject: [PATCH] Fix createOnlyInstance for related methods For scoped or related create method, the createOnlyInstance flag should be calculated on the target model. For example, User.createAccessTokens should set the flag only if AccessToken has updateonly properties. --- lib/model.js | 15 +++++++++------ test/remoting.integration.js | 13 +++++++++++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/model.js b/lib/model.js index 2ced78db..1cc87781 100644 --- a/lib/model.js +++ b/lib/model.js @@ -763,12 +763,7 @@ module.exports = function(registry) { var pathName = (scope.options && scope.options.http && scope.options.http.path) || scopeName; - // if there is atleast one updateOnly property, then we set - // createOnlyInstance flag in __create__ to indicate loopback-swagger - // code to create a separate model instance for create operation only - const updateOnlyProps = this.getUpdateOnlyProperties ? - this.getUpdateOnlyProperties() : false; - const hasUpdateOnlyProps = updateOnlyProps && updateOnlyProps.length > 0; + var modelTo = scope.modelTo; var isStatic = scope.isStatic; var toModelName = scope.modelTo.modelName; @@ -780,8 +775,16 @@ module.exports = function(registry) { // For a relation with through model, the toModelName should be the one // from the target model toModelName = relation.modelTo.modelName; + modelTo = relation.modelTo; } + // if there is atleast one updateOnly property, then we set + // createOnlyInstance flag in __create__ to indicate loopback-swagger + // code to create a separate model instance for create operation only + const updateOnlyProps = modelTo.getUpdateOnlyProperties ? + modelTo.getUpdateOnlyProperties() : false; + const hasUpdateOnlyProps = updateOnlyProps && updateOnlyProps.length > 0; + define('__get__' + scopeName, { isStatic: isStatic, http: {verb: 'get', path: '/' + pathName}, diff --git a/test/remoting.integration.js b/test/remoting.integration.js index ad2dd316..a28da3f3 100644 --- a/test/remoting.integration.js +++ b/test/remoting.integration.js @@ -133,8 +133,8 @@ describe('remoting - integration', function() { it('should have correct signatures for hasMany methods', function() { - var physicianClass = findClass('store'); - var methods = getFormattedPrototypeMethods(physicianClass.methods); + var storeClass = findClass('store'); + var methods = getFormattedPrototypeMethods(storeClass.methods); var expectedMethods = [ 'prototype.__findById__widgets(fk:any):widget ' + @@ -208,6 +208,15 @@ describe('remoting - integration', function() { var createMethod = getCreateMethod(customerClass.methods); assert(createMethod.accepts[0].createOnlyInstance === false); }); + + it('sets createOnlyInstance based on target model for scoped or related methods', + function() { + var userClass = findClass('user'); + var createMethod = userClass.methods.find(function(m) { + return (m.name === 'prototype.__create__accessTokens'); + }); + assert(createMethod.accepts[0].createOnlyInstance === false); + }); }); });