From 3f8bde5aab77c1019f4bc01a1515e520689a3eb1 Mon Sep 17 00:00:00 2001 From: Dmitry Pinaev Date: Mon, 23 Nov 2026 10:45:15 +0100 Subject: [PATCH 1/3] Read the remaining node and group ids from JSON as 64-bit numbers Scene load, group restore and DataFlowGraphModel::loadNode still used QJsonValue::toInt(). It returns the default value for a number above the int range, so an id of 2^31 or more was lost. Use jsonValueToUInt32(), as the undo commands do. --- src/BasicGraphicsScene.cpp | 8 ++++---- src/DataFlowGraphModel.cpp | 2 +- src/DataFlowGraphicsScene.cpp | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/BasicGraphicsScene.cpp b/src/BasicGraphicsScene.cpp index e5460016b..8f7e1929c 100644 --- a/src/BasicGraphicsScene.cpp +++ b/src/BasicGraphicsScene.cpp @@ -49,7 +49,7 @@ using QtNodes::NodeId; NodeId jsonValueToNodeId(QJsonValue const &value) { if (value.isDouble()) { - return static_cast(value.toInt()); + return QtNodes::jsonValueToUInt32(value); } if (value.isString()) { @@ -614,7 +614,7 @@ std::unordered_map BasicGraphicsScene::loadItems(const QByteArray nodeById.reserve(nodesJsonArray.size()); for (const QJsonValue &v : nodesJsonArray) { QJsonObject o = v.toObject(); - NodeId id = static_cast(o.value("id").toInt(InvalidNodeId)); + NodeId id = QtNodes::jsonValueToUInt32(o.value("id")); if (id != InvalidNodeId) nodeById.insert(id, o); } @@ -653,7 +653,7 @@ std::unordered_map BasicGraphicsScene::loadItems(const QByteArray for (QJsonValueRef node : nodesJsonArray) { QJsonObject nodeObj = node.toObject(); - NodeId oldNodeId = static_cast(nodeObj.value("id").toInt(InvalidNodeId)); + NodeId oldNodeId = QtNodes::jsonValueToUInt32(nodeObj.value("id")); if (createdOldNodeIds.contains(oldNodeId)) { continue; @@ -811,7 +811,7 @@ BasicGraphicsScene::restoreGroup(QJsonObject const &groupJson, QJsonObject nodeObject; if (nodeVal.isDouble()) { - NodeId const oldNodeId = static_cast(nodeVal.toInt(InvalidNodeId)); + NodeId const oldNodeId = QtNodes::jsonValueToUInt32(nodeVal); if (oldNodeId == InvalidNodeId) { qWarning() << "restoreGroup(): invalid node id in group:" << nodeVal; continue; diff --git a/src/DataFlowGraphModel.cpp b/src/DataFlowGraphModel.cpp index 95d028872..2a8128302 100644 --- a/src/DataFlowGraphModel.cpp +++ b/src/DataFlowGraphModel.cpp @@ -602,7 +602,7 @@ void DataFlowGraphModel::loadNode(QJsonObject const &nodeJson) // loading. // 2. When undoing the deletion command. Conflict is not possible // because all the new ids were created past the removed nodes. - NodeId restoredNodeId = nodeJson["id"].toInt(); + NodeId restoredNodeId = jsonValueToUInt32(nodeJson["id"]); _nextNodeId = std::max(_nextNodeId, restoredNodeId + 1); diff --git a/src/DataFlowGraphicsScene.cpp b/src/DataFlowGraphicsScene.cpp index 5a0e390ca..4edfd8f46 100644 --- a/src/DataFlowGraphicsScene.cpp +++ b/src/DataFlowGraphicsScene.cpp @@ -38,7 +38,7 @@ using QtNodes::InvalidGroupId; GroupId jsonValueToGroupId(QJsonValue const &value) { if (value.isDouble()) { - return static_cast(value.toInt()); + return QtNodes::jsonValueToUInt32(value); } if (value.isString()) { @@ -269,7 +269,7 @@ bool DataFlowGraphicsScene::load() groupNodes.reserve(nodeIdsJson.size()); for (QJsonValue idValue : nodeIdsJson) { - NodeId const nodeId = static_cast(idValue.toInt()); + NodeId const nodeId = jsonValueToUInt32(idValue); if (auto *nodeObject = nodeGraphicsObject(nodeId)) { groupNodes.push_back(nodeObject); } From 869ca624969e067b90f3d0677194969b2e20618c Mon Sep 17 00:00:00 2001 From: Dmitry Pinaev Date: Mon, 23 Nov 2026 10:48:44 +0100 Subject: [PATCH 2/3] Test that scene load and group restore keep node ids above the int range Without the previous change, the group is not restored and the DataFlowGraphModel does not contain the loaded nodes. --- test/src/TestSerialization.cpp | 36 +++++++++++++++++++ test/src/TestSerializedItemCommands.cpp | 46 +++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/test/src/TestSerialization.cpp b/test/src/TestSerialization.cpp index 854076f4e..9e4b1a699 100644 --- a/test/src/TestSerialization.cpp +++ b/test/src/TestSerialization.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -153,3 +154,38 @@ TEST_CASE("Individual node serialization", "[serialization]") CHECK(nodeJson.contains("position")); } } + +TEST_CASE("DataFlowGraphModel loads node ids above the signed 32-bit range", "[serialization]") +{ + auto app = applicationSetup(); + auto registry = std::make_shared(); + registry->registerModel(createSerializableModel, "SerializableTestModel"); + + DataFlowGraphModel model(registry); + NodeId const a = model.addNode("SerializableTestModel"); + NodeId const b = model.addNode("SerializableTestModel"); + model.addConnection(ConnectionId{a, 0, b, 0}); + + quint32 const bigA = 3000000000u; + quint32 const bigB = 3000000001u; + + QJsonObject json = model.save(); + + QJsonArray nodes = json["nodes"].toArray(); + for (int i = 0; i < nodes.size(); ++i) { + QJsonObject node = nodes[i].toObject(); + node["id"] = static_cast(node["id"].toInt() == static_cast(a) ? bigA : bigB); + nodes[i] = node; + } + json["nodes"] = nodes; + + QJsonObject conn = QtNodes::toJson(ConnectionId{bigA, 0, bigB, 0}); + json["connections"] = QJsonArray{conn}; + + DataFlowGraphModel loaded(registry); + loaded.load(json); + + CHECK(loaded.nodeExists(bigA)); + CHECK(loaded.nodeExists(bigB)); + CHECK(loaded.connectionExists(ConnectionId{bigA, 0, bigB, 0})); +} diff --git a/test/src/TestSerializedItemCommands.cpp b/test/src/TestSerializedItemCommands.cpp index 95b6af3cd..25f0f8c18 100644 --- a/test/src/TestSerializedItemCommands.cpp +++ b/test/src/TestSerializedItemCommands.cpp @@ -4,11 +4,16 @@ #include #include #include +#include +#include #include #include +#include +#include #include +#include #include #include @@ -515,3 +520,44 @@ TEST_CASE("Paste keeps connections for node ids above the signed 32-bit range", for (NodeId id : model.deletedIds) CHECK(id >= BigIdGraphModel::firstId); } + +TEST_CASE("Loading serialized items restores a group and a connection for ids above the signed " + "32-bit range", + "[undo][serialized][group]") +{ + auto app = applicationSetup(); + + BigIdGraphModel model; + BasicGraphicsScene scene(model); + scene.setGroupingEnabled(true); + + NodeId const a = addNodeAt(model, QPointF(0, 0)); + NodeId const b = addNodeAt(model, QPointF(100, 0)); + model.addConnection(ConnectionId{a, 0, b, 0}); + + selectOnly(scene, {a, b}); + auto const group = scene.createGroupFromSelection("Big").lock(); + REQUIRE(group != nullptr); + + // Nodes in a group are locked. Select the group to copy it with its nodes. + scene.clearSelection(); + group->groupGraphicsObject().setSelected(true); + scene.undoStack().push(new CopyCommand(&scene)); + + QMimeData const *mimeData = QApplication::clipboard()->mimeData(); + REQUIRE(mimeData != nullptr); + QByteArray const data = mimeData->data("application/qt-nodes-graph"); + REQUIRE_FALSE(data.isEmpty()); + + BigIdGraphModel otherModel; + BasicGraphicsScene otherScene(otherModel); + otherScene.setGroupingEnabled(true); + otherScene.loadFromMemory(data); + + CHECK(otherModel.allNodeIds().size() == 2); + REQUIRE(otherScene.groups().size() == 1); + CHECK(otherScene.groups().begin()->second->childNodes().size() == 2); + + NodeId const first = *otherModel.allNodeIds().begin(); + CHECK(otherModel.allConnectionIds(first).size() == 1); +} From 8e2d7426b58987e04e3b5c85ba296798edeb158e Mon Sep 17 00:00:00 2001 From: Dmitry Pinaev Date: Mon, 23 Nov 2026 10:48:44 +0100 Subject: [PATCH 3/3] Document how to read ids from JSON --- AGENTS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/AGENTS.md b/AGENTS.md index 8099efce4..87d467a0d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -54,6 +54,8 @@ signals. - Styling: JSON styles via `NodeStyle`, `ConnectionStyle`, `GraphicsViewStyle`, managed by the `StyleCollection` singleton. - Serialization: the `Serializable` interface, `save()`/`load()` with `QJsonObject`. + Read ids from JSON with `jsonValueToUInt32()`, never `toInt()`. `toInt()` returns 0 for + ids of 2^31 or more. Core types: `NodeId` and `PortIndex` are `unsigned int` (`InvalidNodeId` is the maximum value). `PortType` is `In`, `Out` or `None`. `ConnectionId` is