diff --git a/src/plugins/dashboard/common/embeddable/dashboard_container_persistable_state.ts b/src/plugins/dashboard/common/embeddable/dashboard_container_persistable_state.ts index c04f2623d6d555..bc8f56fc8c4dcd 100644 --- a/src/plugins/dashboard/common/embeddable/dashboard_container_persistable_state.ts +++ b/src/plugins/dashboard/common/embeddable/dashboard_container_persistable_state.ts @@ -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); diff --git a/src/plugins/saved_objects_tagging_oss/public/decorator/extract_tag_references.test.ts b/src/plugins/saved_objects_tagging_oss/public/decorator/extract_tag_references.test.ts index fee1b6e0b0707f..80edbaaf875f37 100644 --- a/src/plugins/saved_objects_tagging_oss/public/decorator/extract_tag_references.test.ts +++ b/src/plugins/saved_objects_tagging_oss/public/decorator/extract_tag_references.test.ts @@ -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' }, + ]); + }); }); diff --git a/src/plugins/saved_objects_tagging_oss/public/decorator/extract_tag_references.ts b/src/plugins/saved_objects_tagging_oss/public/decorator/extract_tag_references.ts index 33d425dc82453b..354f6825d8220f 100644 --- a/src/plugins/saved_objects_tagging_oss/public/decorator/extract_tag_references.ts +++ b/src/plugins/saved_objects_tagging_oss/public/decorator/extract_tag_references.ts @@ -18,7 +18,7 @@ export const extractTagReferences: Required['extractReference references, }) => { const { __tags, ...otherAttributes } = attributes; - const tags = (__tags as string[]) ?? []; + const tags = [...new Set(__tags as string[])] ?? []; return { attributes: otherAttributes, references: [ diff --git a/x-pack/plugins/lens/common/embeddable_factory/index.ts b/x-pack/plugins/lens/common/embeddable_factory/index.ts index 2ea5c5192a1da9..e3ed6dc36da258 100644 --- a/x-pack/plugins/lens/common/embeddable_factory/index.ts +++ b/x-pack/plugins/lens/common/embeddable_factory/index.ts @@ -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;