Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type {
export { parseObjectKey, getObjectKey, getIndexForType } from './src/utils';
export {
modelVersionVirtualMajor,
globalSwitchToModelVersionAt,
assertValidModelVersion,
isVirtualModelVersion,
virtualVersionToModelVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@
* The major version that is used to represent model versions.
*/
export const modelVersionVirtualMajor = 10;

/**
* The stack version where all types will be forced to switch to using the model version system.
*/
export const globalSwitchToModelVersionAt = '8.10.0';
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

export { modelVersionVirtualMajor } from './constants';
export { modelVersionVirtualMajor, globalSwitchToModelVersionAt } from './constants';
export {
assertValidModelVersion,
isVirtualModelVersion,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { SavedObjectsType } from '@kbn/core-saved-objects-server';
import { globalSwitchToModelVersionAt } from '@kbn/core-saved-objects-base-server-internal';
import { applyTypeDefaults } from './apply_type_defaults';

const createType = (parts: Partial<SavedObjectsType> = {}): SavedObjectsType => ({
name: 'test',
namespaceType: 'single',
hidden: false,
mappings: { properties: {} },
...parts,
});

describe('applyTypeDefaults', () => {
describe('switchToModelVersionAt', () => {
it(`keeps the type's switchToModelVersionAt if lesser than the global version`, () => {
const type = createType({
switchToModelVersionAt: '8.4.0',
});

const result = applyTypeDefaults(type);
expect(result.switchToModelVersionAt).toEqual('8.4.0');
});

it(`sets switchToModelVersionAt to the global version if unspecified`, () => {
const type = createType({
switchToModelVersionAt: undefined,
});

const result = applyTypeDefaults(type);
expect(result.switchToModelVersionAt).toEqual(globalSwitchToModelVersionAt);
});

it(`throws if switchToModelVersionAt is invalid`, () => {
const type = createType({
switchToModelVersionAt: 'foobar',
});

expect(() => applyTypeDefaults(type)).toThrowErrorMatchingInlineSnapshot(
`"Type test: invalid switchToModelVersionAt provided: foobar"`
);
});

it(`throws if type version is greater than the global version`, () => {
const type = createType({
switchToModelVersionAt: '9.2.0',
});

expect(() => applyTypeDefaults(type)).toThrowErrorMatchingInlineSnapshot(
`"Type test: provided switchToModelVersionAt (9.2.0) is higher than maximum (8.10.0)"`
);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import Semver from 'semver';
import type { SavedObjectsType } from '@kbn/core-saved-objects-server';
import { globalSwitchToModelVersionAt } from '@kbn/core-saved-objects-base-server-internal';

/**
* Apply global defaults to the provided SO type.
*/
export const applyTypeDefaults = (type: SavedObjectsType): SavedObjectsType => {
let switchToModelVersionAt = type.switchToModelVersionAt;
if (switchToModelVersionAt) {
if (!Semver.valid(switchToModelVersionAt)) {
throw new Error(
`Type ${type.name}: invalid switchToModelVersionAt provided: ${switchToModelVersionAt}`
);
}
if (Semver.gt(switchToModelVersionAt, globalSwitchToModelVersionAt)) {
throw new Error(
`Type ${type.name}: provided switchToModelVersionAt (${switchToModelVersionAt}) is higher than maximum (${globalSwitchToModelVersionAt})`
);
}
} else {
switchToModelVersionAt = globalSwitchToModelVersionAt;
}

return {
...type,
switchToModelVersionAt,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,12 @@ export const registerRoutesMock = jest.fn();
jest.doMock('./routes', () => ({
registerRoutes: registerRoutesMock,
}));

export const applyTypeDefaultsMock = jest.fn();
jest.doMock('./apply_type_defaults', () => {
const actual = jest.requireActual('./apply_type_defaults');
return {
...actual,
applyTypeDefaults: applyTypeDefaultsMock,
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
migratorInstanceMock,
registerRoutesMock,
typeRegistryInstanceMock,
applyTypeDefaultsMock,
} from './saved_objects_service.test.mocks';
import { BehaviorSubject, firstValueFrom } from 'rxjs';
import { skip } from 'rxjs/operators';
Expand Down Expand Up @@ -69,6 +70,7 @@ describe('SavedObjectsService', () => {

beforeEach(() => {
deprecationsSetup = createDeprecationRegistryProviderMock();
applyTypeDefaultsMock.mockReset().mockImplementation((type: unknown) => type);
});

const createCoreContext = ({
Expand Down Expand Up @@ -330,22 +332,50 @@ describe('SavedObjectsService', () => {
});

describe('#registerType', () => {
it('registers the type to the internal typeRegistry', async () => {
it('calls `applyTypeDefaults` with the correct parameters', async () => {
// we mocked registerCoreObjectTypes above, so this test case only reflects direct calls to the registerType method
const coreContext = createCoreContext();
const soService = new SavedObjectsService(coreContext);
const setup = await soService.setup(createSetupDeps());

const type = {
const inputType = {
name: 'someType',
hidden: false,
namespaceType: 'single' as 'single',
mappings: { properties: {} },
};
setup.registerType(type);

applyTypeDefaultsMock.mockReturnValue(inputType);

setup.registerType(inputType);

expect(applyTypeDefaultsMock).toHaveBeenCalledTimes(1);
expect(applyTypeDefaultsMock).toHaveBeenCalledWith(inputType);
});

it('registers the type returned by `applyTypeDefaults` to the internal typeRegistry', async () => {
// we mocked registerCoreObjectTypes above, so this test case only reflects direct calls to the registerType method
const coreContext = createCoreContext();
const soService = new SavedObjectsService(coreContext);
const setup = await soService.setup(createSetupDeps());

const inputType = {
name: 'someType',
hidden: false,
namespaceType: 'single' as 'single',
mappings: { properties: {} },
};
const returnedType = {
...inputType,
switchToModelVersionAt: '9.9.9',
};

applyTypeDefaultsMock.mockReturnValue(returnedType);

setup.registerType(inputType);

expect(typeRegistryInstanceMock.registerType).toHaveBeenCalledTimes(1);
expect(typeRegistryInstanceMock.registerType).toHaveBeenCalledWith(type);
expect(typeRegistryInstanceMock.registerType).toHaveBeenCalledWith(returnedType);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { registerRoutes } from './routes';
import { calculateStatus$ } from './status';
import { registerCoreObjectTypes } from './object_types';
import { getSavedObjectsDeprecationsProvider } from './deprecations';
import { applyTypeDefaults } from './apply_type_defaults';

/**
* @internal
Expand Down Expand Up @@ -194,7 +195,7 @@ export class SavedObjectsService
if (this.started) {
throw new Error('cannot call `registerType` after service startup.');
}
this.typeRegistry.registerType(type);
this.typeRegistry.registerType(applyTypeDefaults(type));
Copy link
Copy Markdown
Contributor Author

@pgayvallet pgayvallet May 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing that directly in the type registry class was too low level, just breaking too many tests. Also I think it makes sense to have this done from the service instead.

},
getTypeRegistry: () => this.typeRegistry,
getDefaultIndex: () => MAIN_SAVED_OBJECT_INDEX,
Expand Down
Loading