Skip to content

Commit 85baab2

Browse files
only transform legend if present (elastic#213814)
The inventory_view saved object allows the legend attribute to be optional and limits and/or sets the number of steps during an upgrade between model version 1 and 2. The transform function needs to handle cases where legend is not set to prevent migration failures. Related to elastic#207007 - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
1 parent 88e41ed commit 85baab2

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

x-pack/solutions/observability/plugins/infra/server/saved_objects/inventory_view/inventory_view_saved_object.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,24 @@ describe('invetoryViewSavedObject model version transformation', () => {
5757
});
5858
expect(migrated.attributes).toEqual(inventoryViewV2.attributes);
5959
});
60+
61+
it('should return unaltered document if legend is not defined when converting from v1 to v2', () => {
62+
const { legend, ...inventoryViewV2bAttributes } = inventoryViewV2.attributes;
63+
const inventoryViewV1b = JSON.parse(
64+
JSON.stringify({ ...inventoryViewV2, attributes: inventoryViewV2bAttributes })
65+
);
66+
delete inventoryViewV1b.attributes.legend;
67+
const migrated = migrator.migrate({
68+
document: {
69+
...inventoryViewV1b,
70+
attributes: {
71+
...inventoryViewV1b.attributes,
72+
},
73+
},
74+
fromVersion: 1,
75+
toVersion: 2,
76+
});
77+
expect(migrated.attributes).toEqual(inventoryViewV2bAttributes);
78+
});
6079
});
6180
});

x-pack/solutions/observability/plugins/infra/server/saved_objects/inventory_view/inventory_view_saved_object.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ export const inventoryViewSavedObjectType: SavedObjectsType = {
5959
{
6060
type: 'unsafe_transform',
6161
transformFn: (document) => {
62-
if (document.attributes.legend.steps > 18) {
62+
if (document.attributes.legend?.steps > 18) {
6363
document.attributes.legend.steps = 18;
64-
} else if (document.attributes.legend.steps < 2) {
64+
} else if (document.attributes.legend?.steps < 2) {
6565
document.attributes.legend.steps = 2;
6666
}
6767
return { document };

0 commit comments

Comments
 (0)