normalize initialNamespaces (#110936)

This commit is contained in:
Pierre Gayvallet 2021-09-02 22:01:32 +02:00 committed by GitHub
parent e6faa58873
commit 00305dbfb2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 2 deletions

View file

@ -555,6 +555,25 @@ describe('SavedObjectsRepository', () => {
await test(namespace);
});
it(`normalizes initialNamespaces from 'default' to undefined`, async () => {
const test = async (namespace) => {
const objects = [{ ...obj1, type: 'dashboard', initialNamespaces: ['default'] }];
await bulkCreateSuccess(objects, { namespace, overwrite: true });
const body = [
{ index: expect.objectContaining({ _id: `dashboard:${obj1.id}` }) },
expect.not.objectContaining({ namespace: 'default' }),
];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body }),
expect.anything()
);
client.bulk.mockClear();
client.mget.mockClear();
};
await test(undefined);
await test(namespace);
});
it(`doesn't add namespaces to request body for any types that are not multi-namespace`, async () => {
const test = async (namespace) => {
const objects = [obj1, { ...obj2, type: NAMESPACE_AGNOSTIC_TYPE }];
@ -2072,6 +2091,24 @@ describe('SavedObjectsRepository', () => {
);
});
it(`normalizes initialNamespaces from 'default' to undefined`, async () => {
await savedObjectsRepository.create('dashboard', attributes, {
id,
namespace,
initialNamespaces: ['default'],
});
expect(client.create).toHaveBeenCalledTimes(1);
expect(client.create).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
id: `dashboard:${id}`,
body: expect.not.objectContaining({ namespace: 'default' }),
}),
expect.anything()
);
});
it(`doesn't prepend namespace to the id or add namespace or namespaces fields when using namespace-agnostic type`, async () => {
await createSuccess(NAMESPACE_AGNOSTIC_TYPE, attributes, { id, namespace });
expect(client.create).toHaveBeenCalledWith(

View file

@ -301,7 +301,9 @@ export class SavedObjectsRepository {
let savedObjectNamespaces: string[] | undefined;
if (this._registry.isSingleNamespace(type)) {
savedObjectNamespace = initialNamespaces ? initialNamespaces[0] : namespace;
savedObjectNamespace = initialNamespaces
? normalizeNamespace(initialNamespaces[0])
: namespace;
} else if (this._registry.isMultiNamespace(type)) {
if (id && overwrite) {
// we will overwrite a multi-namespace saved object if it exists; if that happens, ensure we preserve its included namespaces
@ -476,7 +478,9 @@ export class SavedObjectsRepository {
versionProperties = getExpectedVersionProperties(version, actualResult);
} else {
if (this._registry.isSingleNamespace(object.type)) {
savedObjectNamespace = initialNamespaces ? initialNamespaces[0] : namespace;
savedObjectNamespace = initialNamespaces
? normalizeNamespace(initialNamespaces[0])
: namespace;
} else if (this._registry.isMultiNamespace(object.type)) {
savedObjectNamespaces = initialNamespaces || getSavedObjectNamespaces(namespace);
}