Skip to content

Commit

Permalink
normalize initialNamespaces (#110936)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet authored Sep 2, 2021
1 parent e6faa58 commit 00305db
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
37 changes: 37 additions & 0 deletions src/core/server/saved_objects/service/lib/repository.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }];
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 6 additions & 2 deletions src/core/server/saved_objects/service/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 00305db

Please sign in to comment.