Skip to content

Allow optional first/second in relationship elements#587

Merged
s-heppner merged 2 commits into
eclipse-basyx:developfrom
rwth-iat:fix/optional-rel-element-first-second-refs
Jul 14, 2026
Merged

Allow optional first/second in relationship elements#587
s-heppner merged 2 commits into
eclipse-basyx:developfrom
rwth-iat:fix/optional-rel-element-first-second-refs

Conversation

@JAB1305

@JAB1305 JAB1305 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Metamodel v3.1.2 changed RelationshipElement.first and second to be optional. This was missed earlier and required by JSON and XML adapters.

  • AnnotatedRelationshipElement.__init__ now accepts first and second as Optional[Reference], matching the parent class.
  • The JSON and XML deserializers treat both fields as optional default to None when absent.
  • The JSON serializer omits the fields when None (XML serializer already handled this).
  • Added tests for this behaviour.

Fixes #525

JAB1305 added 2 commits July 10, 2026 14:03
Metamodel v3.1.2 changed `RelationshipElement.first` and `second` to be optional. This was missed earlier and required by JSON and XML adapters.

- `AnnotatedRelationshipElement.__init__` now accepts `first` and `second` as `Optional[Reference]`, matching the parent class.
- The JSON and XML deserializers treat both fields as optional default to `None` when absent.
- The JSON serializer omits the fields when `None` (XML serializer already handled this).
- Added tests for this behaviour.

Fixes eclipse-basyx#525

@s-heppner s-heppner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, does the xml_serialization not need any change?

@JAB1305

JAB1305 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

LGTM, does the xml_serialization not need any change?

No, I only adjusted JSON serialization to have it behave the same as its XML counterpart. The XML serialization does not emit the fields if None:

def relationship_element_to_xml(obj: model.RelationshipElement,
tag: str = NS_AAS+"relationshipElement") -> etree._Element:
"""
Serialization of objects of class :class:`~basyx.aas.model.submodel.RelationshipElement` to XML
:param obj: Object of class :class:`~basyx.aas.model.submodel.RelationshipElement`
:param tag: Namespace+Tag of the serialized element (optional). Default is ``aas:relationshipElement``
:return: Serialized :class:`~lxml.etree._Element` object
"""
et_relationship_element = abstract_classes_to_xml(tag, obj)
if obj.first is not None:
et_relationship_element.append(reference_to_xml(obj.first, NS_AAS+"first"))
if obj.second is not None:
et_relationship_element.append(reference_to_xml(obj.second, NS_AAS+"second"))
return et_relationship_element

Previously JSON serialization did add the fields.

@s-heppner s-heppner merged commit 2ec6901 into eclipse-basyx:develop Jul 14, 2026
15 checks passed
@s-heppner s-heppner deleted the fix/optional-rel-element-first-second-refs branch July 14, 2026 09:09
@s-heppner s-heppner mentioned this pull request Jul 16, 2026
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