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

[8.0] [Dashboard][Saved Object Tagging] Fix Duplicating Tags (#119079) #119185

Merged
merged 2 commits into from
Nov 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,12 @@ export const createExtract = (
});

// We're going to prefix the names of the references so that we don't end up with dupes (from visualizations for instance)
const prefixedReferences = panelReferences.map((reference) => ({
...reference,
name: `${prefix}${reference.name}`,
}));
const prefixedReferences = panelReferences
.filter((reference) => reference.type !== 'tag') // panel references should never contain tags. If they do, they must be removed
.map((reference) => ({
...reference,
name: `${prefix}${reference.name}`,
}));

references.push(...prefixedReferences);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,20 @@ describe('extractTagReferences', () => {
expect(resultRefs).toEqual([refA, refB]);
expect(resultAttrs).toEqual({ someString: 'foo', someNumber: 42 });
});

it('removes duplicated tags', () => {
const attributes = {
__tags: ['tag-id-1', 'tag-id-1', 'tag-id-1', 'tag-id-1', 'tag-id-2'],
};

const { references: resultRefs } = extractTagReferences({
attributes,
references: [] as SavedObjectReference[],
});

expect(resultRefs).toEqual([
{ id: 'tag-id-1', name: 'tag-tag-id-1', type: 'tag' },
{ id: 'tag-id-2', name: 'tag-tag-id-2', type: 'tag' },
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const extractTagReferences: Required<SavedObjectConfig>['extractReference
references,
}) => {
const { __tags, ...otherAttributes } = attributes;
const tags = (__tags as string[]) ?? [];
const tags = [...new Set(__tags as string[])] ?? [];
return {
attributes: otherAttributes,
references: [
Expand Down
15 changes: 14 additions & 1 deletion x-pack/plugins/lens/common/embeddable_factory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,20 @@ export const inject: EmbeddableRegistryDefinition['inject'] = (state, references
const typedState = state as LensEmbeddablePersistableState;

if ('attributes' in typedState && typedState.attributes !== undefined) {
typedState.attributes.references = references as unknown as Serializable[];
// match references based on name, so only references associated with this lens panel are injected.
const matchedReferences: SavedObjectReference[] = [];

if (Array.isArray(typedState.attributes.references)) {
typedState.attributes.references.forEach((serializableRef) => {
const internalReference = serializableRef as unknown as SavedObjectReference;
const matchedReference = references.find(
(reference) => reference.name === internalReference.name
);
if (matchedReference) matchedReferences.push(matchedReference);
});
}

typedState.attributes.references = matchedReferences as unknown as Serializable[];
}

return typedState;
Expand Down