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(); + }); +});