From e992cf15077c7515cb375cf1fa97c7b029e2c699 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Saquetim?= <1108771+megothss@users.noreply.github.com> Date: Tue, 7 May 2024 16:59:36 -0300 Subject: [PATCH] DEV: Prevent removed keys from being resolved in the DAG (#26912) --- app/assets/javascripts/discourse/app/lib/dag.js | 9 ++++++++- .../discourse/tests/unit/lib/dag-test.js | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/app/lib/dag.js b/app/assets/javascripts/discourse/app/lib/dag.js index ac1db35e7da..2a1b326d44f 100644 --- a/app/assets/javascripts/discourse/app/lib/dag.js +++ b/app/assets/javascripts/discourse/app/lib/dag.js @@ -91,7 +91,14 @@ export default class DAG { @bind resolve() { const result = []; - this.#dag.each((key, value) => result.push({ key, value })); + this.#dag.each((key, value) => { + // We need to filter keys that do not exist in the rawData because the DAGMap will insert a vertex for + // dependencies, for example if an item has a "before: search" dependency, the "search" vertex will be included + // even if it was explicitly excluded from the raw data. + if (this.has(key)) { + result.push({ key, value }); + } + }); return result; } diff --git a/app/assets/javascripts/discourse/tests/unit/lib/dag-test.js b/app/assets/javascripts/discourse/tests/unit/lib/dag-test.js index d306c29dd9a..906f5516589 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/dag-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/dag-test.js @@ -70,6 +70,20 @@ module("Unit | Lib | DAG", function (hooks) { assert.deepEqual(keys, ["key1", "key2", "key4", "key3"]); }); + test("should resolve only existing keys", function (assert) { + dag = new DAG(); + dag.add("key1", "value1"); + dag.add("key2", "value2", { before: "key1" }); + dag.add("key3", "value3"); + + dag.delete("key1"); + + const resolved = dag.resolve(); + const keys = resolved.map((pair) => pair.key); + + assert.deepEqual(keys, ["key2", "key3"]); + }); + test("throws on bad positioning", function (assert) { dag = new DAG();