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

Fix duplicating tags after importing from 7.12

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Devon Thomson 2021-11-19 12:17:48 -05:00 committed by GitHub
parent b627dca3ba
commit 6209ec4578
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 6 deletions

View file

@ -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);

View file

@ -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' },
]);
});
});

View file

@ -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: [

View file

@ -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;