From dad018fa7c270ae78a66cac9863391277f5346e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20V=C4=82LCIU?= Date: Thu, 29 Feb 2024 19:33:18 +0200 Subject: [PATCH] fix(catalog-backend): ignore long key paths when building entity search MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * stop failing on stitching when the entity contains deeply nested properties or long keys and `null` values; Signed-off-by: Valentin VĂLCIU --- .changeset/tricky-donkeys-wink.md | 5 +++ .../stitcher/buildEntitySearch.test.ts | 9 ++++- .../operations/stitcher/buildEntitySearch.ts | 33 ++++++++++--------- 3 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 .changeset/tricky-donkeys-wink.md diff --git a/.changeset/tricky-donkeys-wink.md b/.changeset/tricky-donkeys-wink.md new file mode 100644 index 0000000000..37e5a8c5cb --- /dev/null +++ b/.changeset/tricky-donkeys-wink.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +Stop failing on stitching when the entity contains deeply nested properties or long keys. diff --git a/plugins/catalog-backend/src/database/operations/stitcher/buildEntitySearch.test.ts b/plugins/catalog-backend/src/database/operations/stitcher/buildEntitySearch.test.ts index 8bc5ba57a9..4241599351 100644 --- a/plugins/catalog-backend/src/database/operations/stitcher/buildEntitySearch.test.ts +++ b/plugins/catalog-backend/src/database/operations/stitcher/buildEntitySearch.test.ts @@ -111,7 +111,14 @@ describe('buildEntitySearch', () => { }); it('skips very large keys', () => { - const input = [{ key: 'a'.repeat(10000), value: 'foo' }]; + const input = [ + { key: 'a'.repeat(10000), value: true }, + { key: 'b'.repeat(10000), value: false }, + { key: 'c'.repeat(10000), value: 7 }, + { key: 'd'.repeat(10000), value: 'foo' }, + { key: 'e'.repeat(10000), value: null }, + { key: 'f'.repeat(10000), value: undefined }, + ]; const output = mapToRows(input, 'eid'); expect(output).toEqual([]); }); diff --git a/plugins/catalog-backend/src/database/operations/stitcher/buildEntitySearch.ts b/plugins/catalog-backend/src/database/operations/stitcher/buildEntitySearch.ts index 30f2987cb5..75962ce2fb 100644 --- a/plugins/catalog-backend/src/database/operations/stitcher/buildEntitySearch.ts +++ b/plugins/catalog-backend/src/database/operations/stitcher/buildEntitySearch.ts @@ -133,6 +133,9 @@ export function mapToRows(input: Kv[], entityId: string): DbSearchRow[] { for (const { key: rawKey, value: rawValue } of input) { const key = rawKey.toLocaleLowerCase('en-US'); + if (MAX_KEY_LENGTH < key.length) { + continue; + } if (rawValue === undefined || rawValue === null) { result.push({ entity_id: entityId, @@ -142,22 +145,20 @@ export function mapToRows(input: Kv[], entityId: string): DbSearchRow[] { }); } else { const value = String(rawValue).toLocaleLowerCase('en-US'); - if (key.length <= MAX_KEY_LENGTH) { - if (value.length <= MAX_VALUE_LENGTH) { - result.push({ - entity_id: entityId, - key, - original_value: String(rawValue), - value: value, - }); - } else { - result.push({ - entity_id: entityId, - key, - original_value: null, - value: null, - }); - } + if (value.length <= MAX_VALUE_LENGTH) { + result.push({ + entity_id: entityId, + key, + original_value: String(rawValue), + value: value, + }); + } else { + result.push({ + entity_id: entityId, + key, + original_value: null, + value: null, + }); } } }