From 6fe2312ea9e6bda8c0e17f2e528ca7a1c330ba70 Mon Sep 17 00:00:00 2001 From: Stephen Davies Date: Fri, 15 Mar 2024 01:38:16 +1100 Subject: [PATCH] Fix exception from objectArrayTrait using MergeStrategy topStratum --- CHANGES.md | 1 + lib/Traits/Decorators/objectArrayTrait.ts | 18 ++++++++++-------- test/Traits/objectArrayTraitSpec.ts | 6 ++++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ba852ff241..fd69dd5882 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,7 @@ #### next release (8.6.2) +- Fixed exception thrown from `objectArrayTrait` when a model has 0 strata and a `MergeStrategy` of `topStratum`. - [The next improvement] #### 8.6.1 - 2024-03-14 diff --git a/lib/Traits/Decorators/objectArrayTrait.ts b/lib/Traits/Decorators/objectArrayTrait.ts index fd581b77b0..947da7c213 100644 --- a/lib/Traits/Decorators/objectArrayTrait.ts +++ b/lib/Traits/Decorators/objectArrayTrait.ts @@ -156,14 +156,16 @@ export class ObjectArrayTrait extends Trait { // If merge strategy is topStratum, then we only want to keep the ids that exist in the top stratum if (this.merge === MergeStrategy.TopStratum) { const topStratum = model.strataTopToBottom.values().next().value; - - const topIds = this.getIdsAcrossStrata(new Map([["top", topStratum]])); - // Remove ids that don't exist in the top stratum - idsInCorrectOrder.forEach((id) => { - if (!topIds.has(id)) { - idsWithCorrectRemovals.delete(id); - } - }); + // topStratum will be undefined if a model has 0 strata + if (topStratum !== undefined) { + const topIds = this.getIdsAcrossStrata(new Map([["top", topStratum]])); + // Remove ids that don't exist in the top stratum + idsInCorrectOrder.forEach((id) => { + if (!topIds.has(id)) { + idsWithCorrectRemovals.delete(id); + } + }); + } } // Correct ids are: diff --git a/test/Traits/objectArrayTraitSpec.ts b/test/Traits/objectArrayTraitSpec.ts index bae94c227a..8efe0b7f05 100644 --- a/test/Traits/objectArrayTraitSpec.ts +++ b/test/Traits/objectArrayTraitSpec.ts @@ -392,6 +392,12 @@ describe("objectArrayTrait", function () { expect(model.inner[0].baz).toBe(true); }); + it("no error when a model has 0 strata and merge = top", function () { + const terria = new Terria(); + const model = new TestModelMergeTop("test", terria); + expect(model.inner.length).toBe(0); + }); + it("updates to reflect new strata added after evaluation (with merge = top)", function () { const terria = new Terria(); const model = new TestModelMergeTop("test", terria);