block SO setup API calls after startup (#58718) (#58844)

This commit is contained in:
Pierre Gayvallet 2020-02-28 16:22:09 +01:00 committed by GitHub
parent 53d7f08c95
commit 85fd92aa5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 1 deletions

View file

@ -16,6 +16,8 @@ export interface SavedObjectsServiceSetup
When plugins access the Saved Objects client, a new client is created using the factory provided to `setClientFactory` and wrapped by all wrappers registered through `addClientWrapper`<!-- -->.
All the setup APIs will throw if called after the service has started, and therefor cannot be used from legacy plugin code. Legacy plugins should use the legacy savedObject service until migrated.
## Example 1

View file

@ -917,4 +917,10 @@ Would be converted to:
```typescript
const migration: SavedObjectMigrationFn = (doc, { log }) => {...}
```
```
### Remarks
The `registerType` API will throw if called after the service has started, and therefor cannot be used from
legacy plugin code. Legacy plugins should use the legacy savedObjects service and the legacy way to register
saved object types until migrated.

View file

@ -232,6 +232,36 @@ describe('SavedObjectsService', () => {
expect(migratorInstanceMock.runMigrations).toHaveBeenCalledTimes(1);
});
it('throws when calling setup APIs once started', async () => {
const coreContext = createCoreContext({ skipMigration: false });
const soService = new SavedObjectsService(coreContext);
const setup = await soService.setup(createSetupDeps());
await soService.start({});
expect(() => {
setup.setClientFactoryProvider(jest.fn());
}).toThrowErrorMatchingInlineSnapshot(
`"cannot call \`setClientFactoryProvider\` after service startup."`
);
expect(() => {
setup.addClientWrapper(0, 'dummy', jest.fn());
}).toThrowErrorMatchingInlineSnapshot(
`"cannot call \`addClientWrapper\` after service startup."`
);
expect(() => {
setup.registerType({
name: 'someType',
hidden: false,
namespaceAgnostic: false,
mappings: { properties: {} },
});
}).toThrowErrorMatchingInlineSnapshot(
`"cannot call \`registerType\` after service startup."`
);
});
describe('#getTypeRegistry', () => {
it('returns the internal type registry of the service', async () => {
const coreContext = createCoreContext({ skipMigration: false });

View file

@ -61,6 +61,9 @@ import { registerRoutes } from './routes';
* the factory provided to `setClientFactory` and wrapped by all wrappers
* registered through `addClientWrapper`.
*
* All the setup APIs will throw if called after the service has started, and therefor cannot be used
* from legacy plugin code. Legacy plugins should use the legacy savedObject service until migrated.
*
* @example
* ```ts
* import { SavedObjectsClient, CoreSetup } from 'src/core/server';
@ -275,6 +278,7 @@ export class SavedObjectsService
private migrator$ = new Subject<KibanaMigrator>();
private typeRegistry = new SavedObjectTypeRegistry();
private validations: PropertyValidators = {};
private started = false;
constructor(private readonly coreContext: CoreContext) {
this.logger = coreContext.logger.get('savedobjects-service');
@ -316,12 +320,18 @@ export class SavedObjectsService
return {
setClientFactoryProvider: provider => {
if (this.started) {
throw new Error('cannot call `setClientFactoryProvider` after service startup.');
}
if (this.clientFactoryProvider) {
throw new Error('custom client factory is already set, and can only be set once');
}
this.clientFactoryProvider = provider;
},
addClientWrapper: (priority, id, factory) => {
if (this.started) {
throw new Error('cannot call `addClientWrapper` after service startup.');
}
this.clientFactoryWrappers.push({
priority,
id,
@ -329,6 +339,9 @@ export class SavedObjectsService
});
},
registerType: type => {
if (this.started) {
throw new Error('cannot call `registerType` after service startup.');
}
this.typeRegistry.registerType(type);
},
};
@ -415,6 +428,8 @@ export class SavedObjectsService
clientProvider.addClientWrapperFactory(priority, id, factory);
});
this.started = true;
return {
migrator,
clientProvider,