From 29097461476ac14e24032707ba2d8fb3a6eaa393 Mon Sep 17 00:00:00 2001 From: Mark David Avery Date: Wed, 27 Apr 2022 11:02:40 -0700 Subject: [PATCH] fix: Updated entity query param transform to handle keys with '.' in them. Signed-off-by: Mark David Avery --- .changeset/shiny-apes-design.md | 5 +++++ .../parseEntityTransformParams.test.ts | 22 ++++++++++++++++++- .../request/parseEntityTransformParams.ts | 22 +++++++++++++++++-- 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 .changeset/shiny-apes-design.md diff --git a/.changeset/shiny-apes-design.md b/.changeset/shiny-apes-design.md new file mode 100644 index 0000000000..fe3d7d84f2 --- /dev/null +++ b/.changeset/shiny-apes-design.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +Updated entity query param transform to handle keys with '.' in them. This will allow for querying based of annotations such as 'backstage.io/orgin-location' for instance diff --git a/plugins/catalog-backend/src/service/request/parseEntityTransformParams.test.ts b/plugins/catalog-backend/src/service/request/parseEntityTransformParams.test.ts index 1d5d41fc03..8ecae1655f 100644 --- a/plugins/catalog-backend/src/service/request/parseEntityTransformParams.test.ts +++ b/plugins/catalog-backend/src/service/request/parseEntityTransformParams.test.ts @@ -24,6 +24,9 @@ describe('parseEntityTransformParams', () => { metadata: { name: 'n', tags: ['t1', 't2'], + annotations: { + 'test.com/url-like': 'ul1', + }, }, spec: { type: 't', @@ -61,7 +64,24 @@ describe('parseEntityTransformParams', () => { parseEntityTransformParams({ fields: 'kind,metadata.name' })!(entity), ).toEqual({ kind: 'k', metadata: { name: 'n' } }); expect(parseEntityTransformParams({ fields: 'metadata' })!(entity)).toEqual( - { metadata: { name: 'n', tags: ['t1', 't2'] } }, + { + metadata: { + name: 'n', + tags: ['t1', 't2'], + annotations: { 'test.com/url-like': 'ul1' }, + }, + }, ); }); + + it('supports dot notated feilds properly', () => { + expect( + parseEntityTransformParams({ + fields: 'kind,metadata.annotations.test.com/url-like', + })!(entity), + ).toEqual({ + kind: 'k', + metadata: { annotations: { 'test.com/url-like': 'ul1' } }, + }); + }); }); diff --git a/plugins/catalog-backend/src/service/request/parseEntityTransformParams.ts b/plugins/catalog-backend/src/service/request/parseEntityTransformParams.ts index 9935da07a8..adb7b8a133 100644 --- a/plugins/catalog-backend/src/service/request/parseEntityTransformParams.ts +++ b/plugins/catalog-backend/src/service/request/parseEntityTransformParams.ts @@ -20,6 +20,22 @@ import lodash from 'lodash'; import { RecursivePartial } from '../../util/RecursivePartial'; import { parseStringsParam } from './common'; +function getPathArray(input: Entity, field: string) { + const pathArray = []; + let currentPathPart = ''; + + for (const pathPart of field.split('.')) { + currentPathPart += pathPart; + + if (lodash.has(input, pathArray.concat(currentPathPart))) { + pathArray.push(currentPathPart); + currentPathPart = ''; + } + } + + return pathArray; +} + export function parseEntityTransformParams( params: Record, ): ((entity: Entity) => Entity) | undefined { @@ -46,9 +62,11 @@ export function parseEntityTransformParams( const output: RecursivePartial = {}; for (const field of fields) { - const value = lodash.get(input, field); + const pathArray = getPathArray(input, field); + + const value = lodash.get(input, pathArray); if (value !== undefined) { - lodash.set(output, field, value); + lodash.set(output, pathArray, value); } }