Skip to content

WriteTreeToXML does not write the name attribute for SubTree nodes, breaking round-trip serialization #1262

Closed

Description

@jiezenghuang

Describe the bug

In addTreeToXML() (src/xml_parsing.cpp), the name attribute is only written for
non-subtree nodes. For SubTreeNode, only ID (and _fullpath when metadata is enabled)
is emitted:

if(const auto* subtree = dynamic_cast<const SubTreeNode*>(&node))
{
  elem = doc.NewElement(node.registrationName().c_str());
  elem->SetAttribute("ID", subtree->subtreeID().c_str());
  if(add_metadata)
  {
    elem->SetAttribute("_fullpath", subtree->config().path.c_str());
  }
}
else
{
  elem = doc.NewElement(node.registrationName().c_str());
  elem->SetAttribute("name", node.name().c_str());   // <-- only for non-subtree nodes
}

As a result, the instance name of a SubTree node is silently lost when a tree is exported
with WriteTreeToXML() and re-imported later.

To Reproduce

  1. Create a tree containing a SubTree node instantiated with a custom name, e.g.:
2. Export the tree with WriteTreeToXML(tree, false, false). 3. Observe the output: The name="my_instance" attribute is missing. 4. Re-parse the exported XML: the node's name() now falls back to the default (registration name), so the original instance name cannot be recovered.

Expected behavior
WriteTreeToXML() should preserve the instance name of every node, including SubTree
nodes, so that export → import round-trips are lossless:

This matters for tooling that serializes a running tree and reloads it later (logging,
editing, tree-diffing), where node names are used as stable identifiers.

Proposed fix
Always set the name attribute, regardless of node type:
elem = doc.NewElement(node.registrationName().c_str());
elem->SetAttribute("name", node.name().c_str());
if(const auto* subtree = dynamic_cast<const SubTreeNode*>(&node))
{
elem->SetAttribute("ID", subtree->subtreeID().c_str());
if(add_metadata)
{
elem->SetAttribute("_fullpath", subtree->config().path.c_str());
}
}

Environment

  • Branch/version: master (observed on 4.10.0+)
  • OS / Compiler: all (pure serialization logic)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions