Skip to content

Conversation

nicolas-hili
Copy link
Contributor

Fixed a bug when a containment feature is defined with a upper bound set to 1. In this case, it is possible to set the feature by using the set function of EObject, but the model cannot be serialized. The problem is located here (https://github.com/ghillairet/ecore.js/blob/master/src/xmi.js#L214):

if (root.eContainer.isKindOf('Resource')) {

Indeed, eContainer of the root object (in this case, the EObject set in the containment feature) is undefined (the set function of EObject does not check whether the reference feature is a containment feature or not).

You can test with the following model defined using Eclipse:

<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="sample" nsURI="http://www.example.org/sample" nsPrefix="sample">
  <eClassifiers xsi:type="ecore:EClass" name="A">
    <eStructuralFeatures xsi:type="ecore:EReference" name="b" eType="#//B" containment="true"/>
  </eClassifiers>
  <eClassifiers xsi:type="ecore:EClass" name="B"/>
</ecore:EPackage>

This (meta)model defines two concepts A and B, with a containment feature A::b (lowerBound = 0, upperBound = 1). If you try to create a model conformed to this metamodel and then serialize it, it will fail.

Commit ac68cd2 fixes this bug.
Commit 6c62f2e adds a test for this bug.

Besides, commit 45fef67 ensures that for a containment feature whose upper bound is equal to 1, if an object is set and then a second object is set, the first one is unset, and its eContainer and eContainingFeature properties are cleared. Commit 1c89e74 adds a test for it.

By the way, https://github.com/ghillairet/ecore.js/blob/master/src/ecore.js#L191 checks if the reference feature is a containment feature, by checking its value (true or false) and type (boolean). In case of the above metamodel defined with Eclipse, the containment property is a string so isContainment is always false (the variable seems to be never used though).

This commit fix a bug when the upper bound of a containment feature
is equal to 1. In this case, the feature is set with the set function of
EObject. It is then impossible to serialise into XMI beacause the
eContainer and eContainingFeature of the feature object are undefined.
This test check if a containment feature with upper bound equal to 1 can
be serialised correctly.
This commit permits to unset a containment feature whose upper bound
equal to 1. In this case, the unset function of EObject is called during
a set. This ensure that an unset object has its eContainer and
eContainingFeature properties set to undefined.
@ghillairet ghillairet merged commit 1c89e74 into emfjson:master Jul 23, 2015
@ghillairet
Copy link
Member

Merged, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants