Skip to content

Commit

Permalink
chore: throw error when accessing DI from business object
Browse files Browse the repository at this point in the history
Related to #1472
  • Loading branch information
marstamm authored and nikku committed Aug 27, 2021
1 parent f020034 commit d0a057c
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lib/features/modeling/ElementFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import {
DEFAULT_LABEL_SIZE
} from '../../util/LabelUtil';

import {
ensureCompatDiRef
} from '../../util/CompatibilityUtil';


/**
* A bpmn-aware factory for diagram-js shapes
Expand Down Expand Up @@ -72,6 +76,8 @@ ElementFactory.prototype.createBpmnElement = function(elementType, attrs) {
}

businessObject = this._bpmnFactory.create(attrs.type);

ensureCompatDiRef(businessObject);
}

if (!di) {
Expand Down
7 changes: 7 additions & 0 deletions lib/import/BpmnTreeWalker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import {
elementToString
} from './Util';

import {
ensureCompatDiRef
} from '../util/CompatibilityUtil';


/**
* Returns true if an element has the given meta-model type
*
Expand Down Expand Up @@ -113,6 +118,8 @@ export default function BpmnTreeWalker(handler, translate) {
);
} else {
diMap[bpmnElement.id] = di;

ensureCompatDiRef(bpmnElement);
}
} else {
logError(
Expand Down
22 changes: 21 additions & 1 deletion lib/util/CompatibilityUtil.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { isFunction } from 'min-dash';
import {
has,
isFunction
} from 'min-dash';

// TODO(nikku): remove with future bpmn-js version

Expand Down Expand Up @@ -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);
}
});
}
}
21 changes: 21 additions & 0 deletions test/spec/ModelerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
21 changes: 21 additions & 0 deletions test/spec/ViewerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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./);
});
});

});


Expand Down
8 changes: 4 additions & 4 deletions test/spec/features/modeling/AppendShapeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
15 changes: 15 additions & 0 deletions test/spec/features/modeling/ElementFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d0a057c

Please sign in to comment.