From 8b8ec92ccf7022ae8b9eca6ba1b68f06f8ca3672 Mon Sep 17 00:00:00 2001 From: Ersin Erdal <92688503+ersin-erdal@users.noreply.github.com> Date: Fri, 2 May 2025 10:28:56 +0200 Subject: [PATCH 1/2] Fix ignored dynamic templates (#219875) This PR fixes the bug introduced with: https://github.com/elastic/kibana/pull/216719 We didn't pass the dynamic_templates param while creating the component template. The fields were still being added because the fields were marked as `dynamic: true` As the dynamic_template was ignored, the fields were added with an extra mapping like `filedname.keyword` This PR fixes that too. --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> (cherry picked from commit 8c6c928e7cde2233da4145c6e712785acf54c5f9) --- .../alerts_service/alerts_service.test.ts | 30 +++++++++++++++++++ .../server/alerts_service/alerts_service.ts | 1 + .../alerts_as_data_dynamic_templates.ts | 4 +-- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/x-pack/platform/plugins/shared/alerting/server/alerts_service/alerts_service.test.ts b/x-pack/platform/plugins/shared/alerting/server/alerts_service/alerts_service.test.ts index d34a10397469e..430e2d951322d 100644 --- a/x-pack/platform/plugins/shared/alerting/server/alerts_service/alerts_service.test.ts +++ b/x-pack/platform/plugins/shared/alerting/server/alerts_service/alerts_service.test.ts @@ -706,6 +706,36 @@ describe('Alerts Service', () => { } }); + test('should save the dynamic_templates', async () => { + const dynamicTemplates = [ + { + strings_as_keywords: { + path_match: 'test-path', + match_mapping_type: 'string', + mapping: { + type: 'keyword', + ignore_above: 1024, + }, + }, + }, + ] satisfies IRuleTypeAlerts['mappings']['dynamicTemplates']; + + alertsService.register({ + ...TestRegistrationContext, + mappings: { + ...TestRegistrationContext.mappings, + dynamicTemplates, + }, + }); + await retryUntil( + 'context initialized', + async () => (await getContextInitialized(alertsService)) === true + ); + + const componentTemplate = clusterClient.cluster.putComponentTemplate.mock.calls[3][0]; + expect(componentTemplate.template.mappings?.dynamic_templates).toEqual(dynamicTemplates); + }); + test('should correctly install resources for custom namespace on demand when isSpaceAware is true', async () => { alertsService.register({ ...TestRegistrationContext, isSpaceAware: true }); await retryUntil( diff --git a/x-pack/platform/plugins/shared/alerting/server/alerts_service/alerts_service.ts b/x-pack/platform/plugins/shared/alerting/server/alerts_service/alerts_service.ts index 6a6dbbdb58dfc..82baee5fcacb2 100644 --- a/x-pack/platform/plugins/shared/alerting/server/alerts_service/alerts_service.ts +++ b/x-pack/platform/plugins/shared/alerting/server/alerts_service/alerts_service.ts @@ -428,6 +428,7 @@ export class AlertsService implements IAlertsService { const componentTemplate = getComponentTemplate({ fieldMap: mappings.fieldMap, dynamic: mappings.dynamic, + dynamicTemplates: mappings.dynamicTemplates, context, }); initFns.push( diff --git a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/group4/alerts_as_data/alerts_as_data_dynamic_templates.ts b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/group4/alerts_as_data/alerts_as_data_dynamic_templates.ts index 0a4556a1b2b7e..7983a3d09c6ba 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/group4/alerts_as_data/alerts_as_data_dynamic_templates.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/tests/alerting/group4/alerts_as_data/alerts_as_data_dynamic_templates.ts @@ -73,7 +73,7 @@ export default function createAlertsAsDataDynamicTemplatesTest({ getService }: F // therefore we add 9 to get the real number. const nestedObjectsAndMultiFields = 9; // Number of free slots that we want to have, so we can add dynamic fields as many - const numberofFreeSlots = 3; + const numberofFreeSlots = 2; const totalFields = numberOfExistingFields + nestedObjectsAndMultiFields + numberofFreeSlots; @@ -128,7 +128,7 @@ export default function createAlertsAsDataDynamicTemplatesTest({ getService }: F ); // new dynamic field has been added - expect(dynamicField).to.eql('text'); + expect(dynamicField).to.eql('keyword'); }); }); From af4a265361866a5773bfb338ffbd9fbe5111a62f Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Fri, 2 May 2025 14:25:36 +0200 Subject: [PATCH 2/2] ignore mistyped param --- .../shared/alerting/server/alerts_service/alerts_service.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/platform/plugins/shared/alerting/server/alerts_service/alerts_service.test.ts b/x-pack/platform/plugins/shared/alerting/server/alerts_service/alerts_service.test.ts index 430e2d951322d..d0fdc9fc54172 100644 --- a/x-pack/platform/plugins/shared/alerting/server/alerts_service/alerts_service.test.ts +++ b/x-pack/platform/plugins/shared/alerting/server/alerts_service/alerts_service.test.ts @@ -733,6 +733,7 @@ describe('Alerts Service', () => { ); const componentTemplate = clusterClient.cluster.putComponentTemplate.mock.calls[3][0]; + // @ts-ignore expect(componentTemplate.template.mappings?.dynamic_templates).toEqual(dynamicTemplates); });