Skip to content

AssignOwnership duplicates the member when an owning membership is re-assigned; no public way to reorder or remove owned relationships #427

Closed

Description

@samatstariongroup

Problem

ElementExtensions.AssignOwnership(source, bridgeRelationship, target) adds target to the relationship's ownedRelatedElement a second time when the relationship already owns it. This happens on any re-assignment of an existing owning membership, whether to its current owner or to another one.

SysML2.NET/Extensions/ElementExtensions.cs:181-186:

if (target.OwningRelationship != null && target.OwningRelationship != bridgeRelationship)
{
    ((IContainedRelationship)target.OwningRelationship).OwnedRelatedElement.Remove(target);
}

((IContainedRelationship)bridgeRelationship).OwnedRelatedElement.Add(target);

When target.OwningRelationship == bridgeRelationship, the removal is skipped but the Add still runs. The membership then owns its member twice, so every derivation through it throws. For example, OwningMembershipExtensions.ComputeOwnedMemberElement throws MultiplicityViolationException: "owningMembershipSubject contains more than one element of type IElement". That in turn breaks Namespace::member and everything derived from it.

Repro

var package = new Package();
var other = new Package();
var member = new Package();
var membership = new OwningMembership();
package.AssignOwnership(membership, member);

other.AssignOwnership(membership, member);     // move the membership to another owner
// membership.OwnedRelatedElement.Count == 2, and other.member throws

Related: no public way to reorder or shrink an ownership list

ContainerList, IContainedElement and IContainedRelationship are internal, and OwnedRelationship / OwnedRelatedElement are exposed as IReadOnlyList. So a caller cannot:

  • remove an owned relationship;
  • reorder the owned relationships of an element (re-assigning to the same owner keeps the position);
  • let a relationship other than an OwningMembership or an Annotation own a related element. AssignOwnership(source, relationship, target) rejects it, although KerML allows it: for example the feature chain owned by a CrossSubsetting or a Specialization (KEBNF GeneralType).

How it was found

Step 5 of the Flexo integration scenario of #407 updates the KerML model. Plan §6.1 U4 needs "an ordered collection reordered, extended and shrunk (ownedRelationship of a Namespace)". Only "extended" can be done through the public API today, so the scenario covers that and defers reordering and shrinking to this issue.

Activity

  1. added
    bug
    on Nov 7, 2026
  2. added 2 commits that reference this issue on Nov 10, 2026
    e1c6afd
    2cc4de3
  3. added a commit that references this issue on Nov 12, 2026
    a38b080
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions