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 committed by Nico Rehwaldt
parent 0c40cbe9f3
commit 597c417dce
7 changed files with 95 additions and 5 deletions

View File

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

View File

@ -8,6 +8,11 @@ import {
elementToString
} from './Util';
import {
ensureCompatDiRef
} from '../util/CompatibilityUtil';
/**
* Returns true if an element has the given meta-model type
*
@ -113,6 +118,8 @@ export default function BpmnTreeWalker(handler, translate) {
);
} else {
diMap[bpmnElement.id] = di;
ensureCompatDiRef(bpmnElement);
}
} else {
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
@ -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() {
var modeler = new Modeler({ container: container });
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) {
return e.sourceRef === startEvent && e.targetRef === target;
}),
connectionDi = getDi(connection);
connectionDi = getDi(elementRegistry.get(connection.id));
// when
@ -174,7 +174,7 @@ describe('features/modeling - append shape', function() {
var connection = find(subProcess.get('flowElements'), function(e) {
return e.sourceRef === startEvent && e.targetRef === target;
}),
connectionDi = getDi(connection);
connectionDi = getDi(elementRegistry.get(connection.id));
// when
commandStack.undo();
@ -209,7 +209,7 @@ describe('features/modeling - append shape', function() {
var connection = find(subProcess.get('flowElements'), function(e) {
return e.sourceRef === startEvent && e.targetRef === target;
}),
connectionDi = getDi(connection);
connectionDi = getDi(elementRegistry.get(connection.id));
// when
commandStack.undo();
@ -270,7 +270,7 @@ describe('features/modeling - append shape', function() {
var connection = find(subProcess.get('flowElements'), function(e) {
return e.sourceRef === startEvent && e.targetRef === target;
}),
connectionDi = getDi(connection);
connectionDi = getDi(elementRegistry.get(connection.id));
// when
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() {
it('should create event definition with ID', inject(function(elementFactory) {