[core] Handle missing link nodes when deserializing edges

Avoid uncaught errors when deserializing edges in case linked
nodes are missing.
Handle missing nodes the same way missing attributes are dealt with.
This commit is contained in:
Yann Lanthony 2025-02-06 16:46:05 +01:00
parent 87fbcee06d
commit 25094ac877
2 changed files with 17 additions and 2 deletions

View file

@ -339,9 +339,12 @@ class Attribute(BaseObject):
elif self.isInput and Attribute.isLinkExpression(v):
# value is a link to another attribute
link = v[1:-1]
linkNode, linkAttr = link.split('.')
linkNodeName, linkAttrName = link.split('.')
try:
g.addEdge(g.node(linkNode).attribute(linkAttr), self)
node = g.node(linkNodeName)
if not node:
raise KeyError(f"Node '{linkNodeName}' not found")
g.addEdge(node.attribute(linkAttrName), self)
except KeyError as err:
logging.warning('Connect Attribute from Expression failed.')
logging.warning('Expression: "{exp}"\nError: "{err}".'.format(exp=v, err=err))

View file

@ -336,3 +336,15 @@ class TestImportGraphContentFromMinimalGraphData:
assert len(graph.nodes) == 1
assert len(graph.compatibilityNodes) == 0
def test_connectionsToMissingNodesAreDiscarded(self):
graph = Graph("")
with registeredNodeTypes([SimpleNode]):
sampleGraphContent = dedent("""
{
"SimpleNode_1": {
"nodeType": "SimpleNode", "inputs": { "input": "{NotSerializedNode.output}" }
}
}
""")
graph._deserialize(json.loads(sampleGraphContent))