Skip to content

Commit

Permalink
fix an issue with transforming corrupted models from v2 (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
loreanvictor authored Oct 28, 2023
1 parent 1ad5da8 commit 7bacfdc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/main/compat/v2/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 }), {}),
},
};
}
10 changes: 10 additions & 0 deletions src/tests/unit/compat/v2/transform-test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 7bacfdc

Please sign in to comment.