feat(util/model): improve the way we clone properties
Now we make sure we don't copy id or reference properties/elements.
This commit is contained in:
parent
af9a36b9b6
commit
2dcfb1b3c9
|
@ -2,9 +2,10 @@
|
||||||
|
|
||||||
var forEach = require('lodash/collection/forEach'),
|
var forEach = require('lodash/collection/forEach'),
|
||||||
filter = require('lodash/collection/filter'),
|
filter = require('lodash/collection/filter'),
|
||||||
isArray = require('lodash/lang/isArray'),
|
any = require('lodash/collection/any'),
|
||||||
contains = require('lodash/collection/contains');
|
isArray = require('lodash/lang/isArray');
|
||||||
|
|
||||||
|
var IGNORED_PROPERTIES = require('./ModelCloneUtils').IGNORED_PROPERTIES;
|
||||||
|
|
||||||
function isAllowedIn(extProp, type) {
|
function isAllowedIn(extProp, type) {
|
||||||
var allowedIn = extProp.meta.allowedIn;
|
var allowedIn = extProp.meta.allowedIn;
|
||||||
|
@ -17,107 +18,125 @@ function isAllowedIn(extProp, type) {
|
||||||
return allowedIn.indexOf(type) !== -1;
|
return allowedIn.indexOf(type) !== -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isType(element, types) {
|
||||||
function isAllowedIn(extProp, type) {
|
return any(types, function(type) {
|
||||||
var allowedIn = extProp.meta.allowedIn;
|
return typeof element === type;
|
||||||
|
});
|
||||||
// '*' is a wildcard, which means any element is allowed to use this property
|
|
||||||
if (allowedIn.length === 1 && allowedIn[0] === '*') {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return allowedIn.indexOf(type) !== -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A bpmn properties cloning interface
|
* A bpmn properties cloning interface
|
||||||
*
|
*
|
||||||
* @param {Moddle} moddle
|
|
||||||
*/
|
*/
|
||||||
function ModelCloneHelper(moddle) {
|
function ModelCloneHelper() {}
|
||||||
this._moddle = moddle;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = ModelCloneHelper;
|
module.exports = ModelCloneHelper;
|
||||||
|
|
||||||
|
|
||||||
ModelCloneHelper.prototype.clone = function(oldElement, newElement, properties) {
|
ModelCloneHelper.prototype.clone = function(oldElement, newElement, properties) {
|
||||||
var moddle = this._moddle;
|
this._newElement = newElement;
|
||||||
|
|
||||||
forEach(properties, function(propName) {
|
forEach(properties, function(propName) {
|
||||||
var oldElementProp = oldElement.$model.properties.get(oldElement, propName),
|
var oldElementProp = oldElement.get(propName),
|
||||||
newElementProp = newElement.$model.properties.get(newElement, propName);
|
newElementProp = newElement.get(propName),
|
||||||
|
propDescriptor = newElement.$model.getPropertyDescriptor(newElement, propName),
|
||||||
|
name;
|
||||||
|
|
||||||
if (newElementProp === oldElementProp) {
|
// we're not interested in cloning:
|
||||||
|
// - same values from simple types
|
||||||
|
// - cloning id's
|
||||||
|
// - cloning reference elements
|
||||||
|
if (newElementProp === oldElementProp ||
|
||||||
|
(propDescriptor && (propDescriptor.isId || propDescriptor.isReference))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if it's not an 'extensionElement' or 'documentation' just set the property
|
// if the property is of type 'boolean', 'string', 'number' or 'null', just set it
|
||||||
if (!(contains([ 'bpmn:extensionElements', 'bpmn:documentation' ], propName))) {
|
if (isType(oldElementProp, [ 'boolean', 'string', 'number' ]) || oldElementProp === null) {
|
||||||
newElement.$model.properties.set(newElement, propName, oldElementProp);
|
newElement.set(propName, oldElementProp);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (propName === 'bpmn:extensionElements') {
|
if (isArray(oldElementProp)) {
|
||||||
newElement.$model.properties.set(newElement, propName, moddle.create('bpmn:ExtensionElements', { values: [] }));
|
|
||||||
|
|
||||||
forEach(oldElementProp.values, function(extElement) {
|
|
||||||
|
|
||||||
var extProp = moddle.registry.typeMap[extElement.$type];
|
|
||||||
|
|
||||||
if (extProp.meta.allowedIn && isAllowedIn(extProp, newElement.$type)) {
|
|
||||||
|
|
||||||
var newProp = this._deepClone(extElement);
|
|
||||||
|
|
||||||
newProp.$parent = newElement.extensionElements;
|
|
||||||
|
|
||||||
newElement.extensionElements.values.push(newProp);
|
|
||||||
}
|
|
||||||
}, this);
|
|
||||||
} else if (propName === 'bpmn:documentation') {
|
|
||||||
newElement.documentation = [];
|
|
||||||
|
|
||||||
forEach(oldElementProp, function(extElement) {
|
forEach(oldElementProp, function(extElement) {
|
||||||
var newProp = this._deepClone(extElement);
|
var newProp = this._deepClone(extElement);
|
||||||
|
|
||||||
newProp.$parent = newElement;
|
newProp.$parent = newElement;
|
||||||
|
|
||||||
newElement.documentation.push(newProp);
|
newElementProp.push(newProp);
|
||||||
}, this);
|
}, this);
|
||||||
|
} else {
|
||||||
|
name = propName.replace(/bpmn:/, '');
|
||||||
|
|
||||||
|
newElement[name] = this._deepClone(oldElementProp);
|
||||||
}
|
}
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
return newElement;
|
return newElement;
|
||||||
};
|
};
|
||||||
|
|
||||||
ModelCloneHelper.prototype._deepClone = function _deepClone(extElement) {
|
ModelCloneHelper.prototype._deepClone = function _deepClone(element) {
|
||||||
var newProp = extElement.$model.create(extElement.$type),
|
var newElement = this._newElement;
|
||||||
properties = filter(Object.keys(extElement), function(prop) { return prop !== '$type'; });
|
|
||||||
|
var newProp = element.$model.create(element.$type);
|
||||||
|
|
||||||
|
var properties = filter(Object.keys(element), function(prop) {
|
||||||
|
var descriptor = newProp.$model.getPropertyDescriptor(newProp, prop);
|
||||||
|
|
||||||
|
if (descriptor && (descriptor.isId || descriptor.isReference)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// we need to make sure we don't clone certain properties
|
||||||
|
// which we cannot easily know if they hold references or not
|
||||||
|
if (IGNORED_PROPERTIES.indexOf(prop) !== -1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure we don't copy the type
|
||||||
|
return prop !== '$type';
|
||||||
|
});
|
||||||
|
|
||||||
forEach(properties, function(propName) {
|
forEach(properties, function(propName) {
|
||||||
// check if the extElement has this property defined
|
// check if the element has this property defined
|
||||||
if (extElement[propName] !== undefined && (extElement[propName].$type || isArray(extElement[propName]))) {
|
if (element[propName] !== undefined && (element[propName].$type || isArray(element[propName]))) {
|
||||||
|
|
||||||
if (isArray(extElement[propName])) {
|
if (isArray(element[propName])) {
|
||||||
newProp[propName] = [];
|
newProp[propName] = [];
|
||||||
|
|
||||||
forEach(extElement[propName], function(property) {
|
forEach(element[propName], function(property) {
|
||||||
var newDeepProp = this._deepClone(property);
|
var extProp = element.$model.getTypeDescriptor(property.$type),
|
||||||
|
newDeepProp;
|
||||||
|
|
||||||
|
// we're not going to copy undefined types
|
||||||
|
if (!extProp) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure only allowed extensionElements are copied
|
||||||
|
if (element.$type === 'bpmn:ExtensionElements' &&
|
||||||
|
extProp.meta && extProp.meta.allowedIn &&
|
||||||
|
!isAllowedIn(extProp, newElement.$type)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
newDeepProp = this._deepClone(property);
|
||||||
|
|
||||||
newDeepProp.$parent = newProp;
|
newDeepProp.$parent = newProp;
|
||||||
|
|
||||||
newProp[propName].push(newDeepProp);
|
newProp[propName].push(newDeepProp);
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
} else if (extElement[propName].$type) {
|
} else if (element[propName].$type) {
|
||||||
newProp[propName] = this._deepClone(extElement[propName]);
|
newProp[propName] = this._deepClone(element[propName]);
|
||||||
|
|
||||||
newProp[propName].$parent = newProp;
|
newProp[propName].$parent = newProp;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// just assign directly if it's a value
|
// just assign directly if it's a value
|
||||||
newProp[propName] = extElement[propName];
|
newProp[propName] = element[propName];
|
||||||
}
|
}
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
|
|
|
@ -117,6 +117,9 @@ describe('util/ModelCloneHelper', function() {
|
||||||
var executionListener = serviceTask.extensionElements.values[0];
|
var executionListener = serviceTask.extensionElements.values[0];
|
||||||
|
|
||||||
// then
|
// then
|
||||||
|
expect(executionListener).to.not.equal(userTask.extensionElements.values[0]);
|
||||||
|
expect(executionListener.$type).to.equal('camunda:ExecutionListener');
|
||||||
|
|
||||||
expect(executionListener.$type).to.equal('camunda:ExecutionListener');
|
expect(executionListener.$type).to.equal('camunda:ExecutionListener');
|
||||||
expect(executionListener.$parent).to.equal(serviceTask.extensionElements);
|
expect(executionListener.$parent).to.equal(serviceTask.extensionElements);
|
||||||
|
|
||||||
|
@ -182,6 +185,30 @@ describe('util/ModelCloneHelper', function() {
|
||||||
expect(newOutParam.definition.items[0].value).to.equal('${1+1}');
|
expect(newOutParam.definition.items[0].value).to.equal('${1+1}');
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
it('should not pass disallowed deeply nested property - connector', inject(function(moddle) {
|
||||||
|
|
||||||
|
// given
|
||||||
|
var connector = moddle.create('camunda:Connector', {
|
||||||
|
connectorId: 'hello_connector'
|
||||||
|
});
|
||||||
|
|
||||||
|
var extensionElements = moddle.create('bpmn:ExtensionElements', { values: [ connector ] });
|
||||||
|
|
||||||
|
var serviceTask = moddle.create('bpmn:UserTask', {
|
||||||
|
extensionElements: extensionElements
|
||||||
|
});
|
||||||
|
|
||||||
|
var userTask = helper.clone(serviceTask, moddle.create('bpmn:UserTask'), [
|
||||||
|
'bpmn:extensionElements'
|
||||||
|
]);
|
||||||
|
|
||||||
|
var extElem = userTask.extensionElements;
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(extElem.values).to.be.empty;
|
||||||
|
}));
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue