chore: throw error when accessing DI from business object

Related to https://github.com/bpmn-io/bpmn-js/issues/1472
This commit is contained in:
Martin Stamm 2021-08-25 14:49:00 +02:00
parent e5bf18b0ad
commit 1ec8bf9373
No known key found for this signature in database
GPG Key ID: B3A641060A8CBCF4
7 changed files with 95 additions and 5 deletions

View File

@ -22,6 +22,10 @@ import {
DEFAULT_LABEL_SIZE DEFAULT_LABEL_SIZE
} from '../../util/LabelUtil'; } from '../../util/LabelUtil';
import {
ensureCompatDiRef
} from '../../util/CompatibilityUtil';
/** /**
* A bpmn-aware factory for diagram-js shapes * A bpmn-aware factory for diagram-js shapes
@ -72,6 +76,8 @@ ElementFactory.prototype.createBpmnElement = function(elementType, attrs) {
} }
businessObject = this._bpmnFactory.create(attrs.type); businessObject = this._bpmnFactory.create(attrs.type);
ensureCompatDiRef(businessObject);
} }
if (!di) { if (!di) {

View File

@ -8,6 +8,11 @@ import {
elementToString elementToString
} from './Util'; } from './Util';
import {
ensureCompatDiRef
} from '../util/CompatibilityUtil';
/** /**
* Returns true if an element has the given meta-model type * Returns true if an element has the given meta-model type
* *
@ -113,6 +118,8 @@ export default function BpmnTreeWalker(handler, translate) {
); );
} else { } else {
diMap[bpmnElement.id] = di; diMap[bpmnElement.id] = di;
ensureCompatDiRef(bpmnElement);
} }
} else { } else {
logError( logError(

View File

@ -1,4 +1,7 @@
import { isFunction } from 'min-dash'; import {
has,
isFunction
} from 'min-dash';
// TODO(nikku): remove with future bpmn-js version // TODO(nikku): remove with future bpmn-js version
@ -51,3 +54,20 @@ export function wrapForCompatibility(api) {
} }
}; };
} }
// TODO(nikku): remove with future bpmn-js version
var DI_ERROR_MESSAGE = 'Tried to access di from the businessObject. The di is available through the diagram element only. For more information, see https://github.com/bpmn-io/bpmn-js/issues/1472';
export function ensureCompatDiRef(businessObject) {
// bpmnElement can have multiple independent DIs
if (!has(businessObject, 'di')) {
Object.defineProperty(businessObject, 'di', {
get: function() {
throw new Error(DI_ERROR_MESSAGE);
}
});
}
}

View File

@ -468,6 +468,27 @@ describe('Modeler', function() {
}); });
it('should error when accessing <di> from businessObject', function() {
var xml = require('../fixtures/bpmn/simple.bpmn');
var modeler = new Modeler({ container: container });
return modeler.importXML(xml).then(function() {
// given
var elementRegistry = modeler.get('elementRegistry'),
shape = elementRegistry.get('Task_1');
// then
expect(shape.di).to.exist;
expect(function() {
shape.businessObject.di;
}).to.throw(/The di is available through the diagram element only./);
});
});
it('should create new diagram', function() { it('should create new diagram', function() {
var modeler = new Modeler({ container: container }); var modeler = new Modeler({ container: container });
return modeler.createDiagram(); return modeler.createDiagram();

View File

@ -287,6 +287,27 @@ describe('Viewer', function() {
}); });
}); });
it('should error when accessing <di> from businessObject', function() {
var xml = require('../fixtures/bpmn/simple.bpmn');
return createViewer(container, Viewer, xml).then(function(result) {
// given
var viewer = result.viewer,
elementRegistry = viewer.get('elementRegistry'),
shape = elementRegistry.get('Task_1');
// then
expect(shape.di).to.exist;
expect(function() {
shape.businessObject.di;
}).to.throw(/The di is available through the diagram element only./);
});
});
}); });

View File

@ -141,7 +141,7 @@ describe('features/modeling - append shape', function() {
var connection = find(subProcess.get('flowElements'), function(e) { var connection = find(subProcess.get('flowElements'), function(e) {
return e.sourceRef === startEvent && e.targetRef === target; return e.sourceRef === startEvent && e.targetRef === target;
}), }),
connectionDi = getDi(connection); connectionDi = getDi(elementRegistry.get(connection.id));
// when // when
@ -174,7 +174,7 @@ describe('features/modeling - append shape', function() {
var connection = find(subProcess.get('flowElements'), function(e) { var connection = find(subProcess.get('flowElements'), function(e) {
return e.sourceRef === startEvent && e.targetRef === target; return e.sourceRef === startEvent && e.targetRef === target;
}), }),
connectionDi = getDi(connection); connectionDi = getDi(elementRegistry.get(connection.id));
// when // when
commandStack.undo(); commandStack.undo();
@ -209,7 +209,7 @@ describe('features/modeling - append shape', function() {
var connection = find(subProcess.get('flowElements'), function(e) { var connection = find(subProcess.get('flowElements'), function(e) {
return e.sourceRef === startEvent && e.targetRef === target; return e.sourceRef === startEvent && e.targetRef === target;
}), }),
connectionDi = getDi(connection); connectionDi = getDi(elementRegistry.get(connection.id));
// when // when
commandStack.undo(); commandStack.undo();
@ -270,7 +270,7 @@ describe('features/modeling - append shape', function() {
var connection = find(subProcess.get('flowElements'), function(e) { var connection = find(subProcess.get('flowElements'), function(e) {
return e.sourceRef === startEvent && e.targetRef === target; return e.sourceRef === startEvent && e.targetRef === target;
}), }),
connectionDi = getDi(connection); connectionDi = getDi(elementRegistry.get(connection.id));
// when // when
commandStack.undo(); commandStack.undo();

View File

@ -101,6 +101,21 @@ describe('features - element factory', function() {
})); }));
it('should error when accessing <di> via businessObject', inject(function(elementFactory) {
// given
var shape = elementFactory.createShape({
type: 'bpmn:Task',
});
// then
expect(shape.di).to.exist;
expect(function() {
shape.businessObject.di;
}).to.throw(/The di is available through the diagram element only./);
}));
describe('integration', function() { describe('integration', function() {
it('should create event definition with ID', inject(function(elementFactory) { it('should create event definition with ID', inject(function(elementFactory) {