Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SOR] normalize initialNamespaces for create and bulkCreate #110936

Merged
merged 1 commit into from
Sep 2, 2021
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
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