Skip to content

Commit e6636e2

Browse files
committed
[FIX] Model: reject data that postdate the library version
Currently, if we try to load data that postdate tjhe library version, the code will consider the data as "versionless" and will apply every migration step to it, which makes no sense as the data already went through those changes. With this revision, we reject the data that postdate the library version. closes #8069 Task: 5895572 X-original-commit: 0bb9142 Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com> Signed-off-by: Rémi Rahir (rar) <rar@odoo.com>
1 parent c4bfb34 commit e6636e2

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/migrations/data.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,14 @@ function compareVersions(v1: string, v2: string): number {
134134
function migrate(data: any): WorkbookData {
135135
const start = performance.now();
136136
const versions = getSortedVersions();
137-
const index = versions.findIndex((v) => v === data.version);
137+
const index = versions.findIndex((v) => compareVersions(v, data.version) >= 0);
138+
if (index === -1) {
139+
throw new Error(
140+
`Data version ${
141+
data.version
142+
} postdates the current version of o-spreadsheet (version ${getCurrentVersion()}). It cannot be loaded.`
143+
);
144+
}
138145
for (let i = index + 1; i < versions.length; i++) {
139146
const nextVersion = versions[i];
140147
data = migrationStepRegistry.get(nextVersion).migrate(data);

tests/model/model_import_export.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,3 +1218,15 @@ test("Update chart revisions contain the full definition pre 18.5.1", () => {
12181218
const definition2 = model.getters.getChartDefinition("fig2") as LineChartDefinition;
12191219
expect(definition2.dataSets).toEqual([{ dataRange: "B1:B3" }]);
12201220
});
1221+
1222+
test("Reject data import from data with a subsequent version", () => {
1223+
const futureVersion = (parseFloat(getCurrentVersion()) + 1).toString();
1224+
expect(() => new Model({ version: futureVersion })).toThrow(
1225+
`Data version ${futureVersion} postdates the current version of o-spreadsheet (version ${getCurrentVersion()}). It cannot be loaded.`
1226+
);
1227+
});
1228+
1229+
test("Accept data that predates the latest version while not being present in the migration steps", () => {
1230+
const previousVersion = "16.3.1";
1231+
expect(() => new Model({ version: previousVersion })).not.toThrow();
1232+
});

0 commit comments

Comments
 (0)