From 6570b948434392a03271fd83844019b741aa2052 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 27 Oct 2017 15:17:58 -0700 Subject: [PATCH 1/2] 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); + }); }); }); From 010c7bcd5ffe1bc156ed4dce5a60e04c1391159e Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 27 Oct 2017 21:43:40 -0700 Subject: [PATCH 2/2] 3.16.1 * Fix createOnlyInstance for related methods (Raymond Feng) --- CHANGES.md | 6 ++++++ package.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 6f18cf45..21fd0cd3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,9 @@ +2017-10-28, Version 3.16.1 +========================== + + * Fix createOnlyInstance for related methods (Raymond Feng) + + 2017-10-24, Version 3.16.0 ========================== diff --git a/package.json b/package.json index 920c20d4..ea5f41f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback", - "version": "3.16.0", + "version": "3.16.1", "description": "LoopBack: Open Source Framework for Node.js", "homepage": "http://loopback.io", "keywords": [