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.
This commit is contained in:
Raymond Feng 2017-10-27 15:17:58 -07:00 committed by Raymond Feng
parent 4d4070e542
commit 6570b94843
2 changed files with 20 additions and 8 deletions

View File

@ -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},

View File

@ -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);
});
});
});