From 7bacfdc9e8bd415015c5c5715befce32fd366966 Mon Sep 17 00:00:00 2001 From: Eugene Ghanizadeh Date: Sat, 28 Oct 2023 19:09:54 +0200 Subject: [PATCH] fix an issue with transforming corrupted models from v2 (#316) --- src/main/compat/v2/transform.ts | 15 ++++++++++----- src/tests/unit/compat/v2/transform-test.ts | 10 ++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 src/tests/unit/compat/v2/transform-test.ts diff --git a/src/main/compat/v2/transform.ts b/src/main/compat/v2/transform.ts index 1a1084a0..a4720868 100644 --- a/src/main/compat/v2/transform.ts +++ b/src/main/compat/v2/transform.ts @@ -10,11 +10,16 @@ import { UMLModelV2, isCommunicationLink } from './typings'; * */ export function v2ModeltoV3Model(model: UMLModelV2): UMLModel { + const elements = Array.isArray(model.elements) ? model.elements : []; + const relationships = Array.isArray(model.relationships) ? model.relationships : []; + const assessments = Array.isArray(model.assessments) ? model.assessments : []; + const interactive = model.interactive || { elements: [], relationships: [] }; + return { ...model, version: '3.0.0', - elements: model.elements.reduce((acc, val) => ({ ...acc, [val.id]: val }), {}), - relationships: model.relationships + elements: elements.reduce((acc, val) => ({ ...acc, [val.id]: val }), {}), + relationships: relationships .map((relationship) => { if (isCommunicationLink(relationship)) { return { @@ -26,10 +31,10 @@ export function v2ModeltoV3Model(model: UMLModelV2): UMLModel { } }) .reduce((acc, val) => ({ ...acc, [val.id]: val }), {}), - assessments: model.assessments.reduce((acc, val) => ({ ...acc, [val.modelElementId]: val }), {}), + assessments: assessments.reduce((acc, val) => ({ ...acc, [val.modelElementId]: val }), {}), interactive: { - elements: model.interactive.elements.reduce((acc, val) => ({ ...acc, [val]: true }), {}), - relationships: model.interactive.relationships.reduce((acc, val) => ({ ...acc, [val]: true }), {}), + elements: interactive.elements.reduce((acc, val) => ({ ...acc, [val]: true }), {}), + relationships: interactive.relationships.reduce((acc, val) => ({ ...acc, [val]: true }), {}), }, }; } diff --git a/src/tests/unit/compat/v2/transform-test.ts b/src/tests/unit/compat/v2/transform-test.ts new file mode 100644 index 00000000..bfe279a1 --- /dev/null +++ b/src/tests/unit/compat/v2/transform-test.ts @@ -0,0 +1,10 @@ +import { v2ModeltoV3Model } from '../../../../main/compat/v2/transform'; + +describe('test v2 to v3 model conversion', () => { + it('handles corrupt models.', () => { + expect(() => v2ModeltoV3Model({} as any)).not.toThrow(); + expect(() => v2ModeltoV3Model({ elements: {} } as any)).not.toThrow(); + expect(() => v2ModeltoV3Model({ elements: [], relationships: {} } as any)).not.toThrow(); + expect(() => v2ModeltoV3Model({ assessments: {} } as any)).not.toThrow(); + }); +});