Skip to content

Commit

Permalink
add getVisibleTypes API to SO type registry (#70559)
Browse files Browse the repository at this point in the history
* add getVisibleTypes API

* doc nit

* fix mocking in tests
  • Loading branch information
pgayvallet authored Jul 2, 2020
1 parent a8347fa commit 1cfc935
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

## SavedObjectTypeRegistry.getAllTypes() method

Return all [types](./kibana-plugin-core-server.savedobjectstype.md) currently registered.
Return all [types](./kibana-plugin-core-server.savedobjectstype.md) currently registered, including the hidden ones.

To only get the visible types (which is the most common use case), use `getVisibleTypes` instead.

<b>Signature:</b>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [SavedObjectTypeRegistry](./kibana-plugin-core-server.savedobjecttyperegistry.md) &gt; [getVisibleTypes](./kibana-plugin-core-server.savedobjecttyperegistry.getvisibletypes.md)

## SavedObjectTypeRegistry.getVisibleTypes() method

Returns all visible [types](./kibana-plugin-core-server.savedobjectstype.md)<!-- -->.

A visible type is a type that doesn't explicitly define `hidden=true` during registration.

<b>Signature:</b>

```typescript
getVisibleTypes(): SavedObjectsType[];
```
<b>Returns:</b>

`SavedObjectsType[]`

Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ export declare class SavedObjectTypeRegistry

| Method | Modifiers | Description |
| --- | --- | --- |
| [getAllTypes()](./kibana-plugin-core-server.savedobjecttyperegistry.getalltypes.md) | | Return all [types](./kibana-plugin-core-server.savedobjectstype.md) currently registered. |
| [getAllTypes()](./kibana-plugin-core-server.savedobjecttyperegistry.getalltypes.md) | | Return all [types](./kibana-plugin-core-server.savedobjectstype.md) currently registered, including the hidden ones.<!-- -->To only get the visible types (which is the most common use case), use <code>getVisibleTypes</code> instead. |
| [getImportableAndExportableTypes()](./kibana-plugin-core-server.savedobjecttyperegistry.getimportableandexportabletypes.md) | | Return all [types](./kibana-plugin-core-server.savedobjectstype.md) currently registered that are importable/exportable. |
| [getIndex(type)](./kibana-plugin-core-server.savedobjecttyperegistry.getindex.md) | | Returns the <code>indexPattern</code> property for given type, or <code>undefined</code> if the type is not registered. |
| [getType(type)](./kibana-plugin-core-server.savedobjecttyperegistry.gettype.md) | | Return the [type](./kibana-plugin-core-server.savedobjectstype.md) definition for given type name. |
| [getVisibleTypes()](./kibana-plugin-core-server.savedobjecttyperegistry.getvisibletypes.md) | | Returns all visible [types](./kibana-plugin-core-server.savedobjectstype.md)<!-- -->.<!-- -->A visible type is a type that doesn't explicitly define <code>hidden=true</code> during registration. |
| [isHidden(type)](./kibana-plugin-core-server.savedobjecttyperegistry.ishidden.md) | | Returns the <code>hidden</code> property for given type, or <code>false</code> if the type is not registered. |
| [isImportableAndExportable(type)](./kibana-plugin-core-server.savedobjecttyperegistry.isimportableandexportable.md) | | Returns the <code>management.importableAndExportable</code> property for given type, or <code>false</code> if the type is not registered or does not define a management section. |
| [isMultiNamespace(type)](./kibana-plugin-core-server.savedobjecttyperegistry.ismultinamespace.md) | | Returns whether the type is multi-namespace (shareable); resolves to <code>false</code> if the type is not registered |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const createRegistryMock = (): jest.Mocked<
const mock = {
registerType: jest.fn(),
getType: jest.fn(),
getVisibleTypes: jest.fn(),
getAllTypes: jest.fn(),
getImportableAndExportableTypes: jest.fn(),
isNamespaceAgnostic: jest.fn(),
Expand All @@ -35,6 +36,7 @@ const createRegistryMock = (): jest.Mocked<
isImportableAndExportable: jest.fn(),
};

mock.getVisibleTypes.mockReturnValue([]);
mock.getAllTypes.mockReturnValue([]);
mock.getImportableAndExportableTypes.mockReturnValue([]);
mock.getIndex.mockReturnValue('.kibana-test');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,37 @@ describe('SavedObjectTypeRegistry', () => {
});
});

describe('#getVisibleTypes', () => {
it('returns only visible registered types', () => {
const typeA = createType({ name: 'typeA', hidden: false });
const typeB = createType({ name: 'typeB', hidden: true });
const typeC = createType({ name: 'typeC', hidden: false });
registry.registerType(typeA);
registry.registerType(typeB);
registry.registerType(typeC);

const registered = registry.getVisibleTypes();
expect(registered.length).toEqual(2);
expect(registered).toContainEqual(typeA);
expect(registered).toContainEqual(typeC);
});

it('does not mutate the registered types when altering the list', () => {
registry.registerType(createType({ name: 'typeA', hidden: false }));
registry.registerType(createType({ name: 'typeB', hidden: true }));
registry.registerType(createType({ name: 'typeC', hidden: false }));

const types = registry.getVisibleTypes();
types.splice(0, 2);

expect(registry.getVisibleTypes().length).toEqual(2);
});
});

describe('#getAllTypes', () => {
it('returns all registered types', () => {
const typeA = createType({ name: 'typeA' });
const typeB = createType({ name: 'typeB' });
const typeB = createType({ name: 'typeB', hidden: true });
const typeC = createType({ name: 'typeC' });
registry.registerType(typeA);
registry.registerType(typeB);
Expand Down
13 changes: 12 additions & 1 deletion src/core/server/saved_objects/saved_objects_type_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,18 @@ export class SavedObjectTypeRegistry {
}

/**
* Return all {@link SavedObjectsType | types} currently registered.
* Returns all visible {@link SavedObjectsType | types}.
*
* A visible type is a type that doesn't explicitly define `hidden=true` during registration.
*/
public getVisibleTypes() {
return [...this.types.values()].filter((type) => !this.isHidden(type.name));
}

/**
* Return all {@link SavedObjectsType | types} currently registered, including the hidden ones.
*
* To only get the visible types (which is the most common use case), use `getVisibleTypes` instead.
*/
public getAllTypes() {
return [...this.types.values()];
Expand Down
1 change: 1 addition & 0 deletions src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2468,6 +2468,7 @@ export class SavedObjectTypeRegistry {
getImportableAndExportableTypes(): SavedObjectsType[];
getIndex(type: string): string | undefined;
getType(type: string): SavedObjectsType | undefined;
getVisibleTypes(): SavedObjectsType[];
isHidden(type: string): boolean;
isImportableAndExportable(type: string): boolean;
isMultiNamespace(type: string): boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/server/saved_objects/saved_objects_mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export function savedObjectsMixin(kbnServer, server) {
const typeRegistry = kbnServer.newPlatform.start.core.savedObjects.getTypeRegistry();
const mappings = migrator.getActiveMappings();
const allTypes = typeRegistry.getAllTypes().map((t) => t.name);
const visibleTypes = typeRegistry.getVisibleTypes().map((t) => t.name);
const schema = new SavedObjectsSchema(convertTypesToLegacySchema(typeRegistry.getAllTypes()));
const visibleTypes = allTypes.filter((type) => !schema.isHiddenType(type));

server.decorate('server', 'kibanaMigrator', migrator);

Expand Down
8 changes: 1 addition & 7 deletions x-pack/plugins/features/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@ const initContext = coreMock.createPluginInitializerContext();
const coreSetup = coreMock.createSetup();
const coreStart = coreMock.createStart();
const typeRegistry = savedObjectsServiceMock.createTypeRegistryMock();
typeRegistry.getAllTypes.mockReturnValue([
typeRegistry.getVisibleTypes.mockReturnValue([
{
name: 'foo',
hidden: false,
mappings: { properties: {} },
namespaceType: 'single' as 'single',
},
{
name: 'bar',
hidden: true,
mappings: { properties: {} },
namespaceType: 'agnostic' as 'agnostic',
},
]);
coreStart.savedObjects.getTypeRegistry.mockReturnValue(typeRegistry);

Expand Down
5 changes: 1 addition & 4 deletions x-pack/plugins/features/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ export class Plugin {

private registerOssFeatures(savedObjects: SavedObjectsServiceStart) {
const registry = savedObjects.getTypeRegistry();
const savedObjectTypes = registry
.getAllTypes()
.filter((t) => !t.hidden)
.map((t) => t.name);
const savedObjectTypes = registry.getVisibleTypes().map((t) => t.name);

this.logger.debug(
`Registering OSS features with SO types: ${savedObjectTypes.join(', ')}. "includeTimelion": ${
Expand Down

0 comments on commit 1cfc935

Please sign in to comment.