diff --git a/.changeset/shiny-apes-design.md b/.changeset/shiny-apes-design.md index fe3d7d84f2..69dfbf57ab 100644 --- a/.changeset/shiny-apes-design.md +++ b/.changeset/shiny-apes-design.md @@ -2,4 +2,4 @@ '@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 +Updated parseEntityTransformParams to handle keys with '.' in them. This will allow for querying of entities based off annotations such as 'backstage.io/orgin-location' or other entity field keys that have '.' in them. diff --git a/plugins/catalog-backend/src/service/request/parseEntityTransformParams.test.ts b/plugins/catalog-backend/src/service/request/parseEntityTransformParams.test.ts index 8ecae1655f..65f85639e1 100644 --- a/plugins/catalog-backend/src/service/request/parseEntityTransformParams.test.ts +++ b/plugins/catalog-backend/src/service/request/parseEntityTransformParams.test.ts @@ -25,7 +25,9 @@ describe('parseEntityTransformParams', () => { name: 'n', tags: ['t1', 't2'], annotations: { - 'test.com/url-like': 'ul1', + 'example.test/url-like-key': 'ul1', + 'example.com/other-url-like-key': 'ul2', + 'other-example.test/next-url-like-key': 'ul3', }, }, spec: { @@ -68,20 +70,63 @@ describe('parseEntityTransformParams', () => { metadata: { name: 'n', tags: ['t1', 't2'], - annotations: { 'test.com/url-like': 'ul1' }, + annotations: { + 'example.test/url-like-key': 'ul1', + 'example.com/other-url-like-key': 'ul2', + 'other-example.test/next-url-like-key': 'ul3', + }, }, }, ); }); - it('supports dot notated feilds properly', () => { + it('supports dot notated fields properly', () => { expect( parseEntityTransformParams({ - fields: 'kind,metadata.annotations.test.com/url-like', + fields: 'kind,metadata.annotations.example.com/other-url-like-key', })!(entity), ).toEqual({ kind: 'k', - metadata: { annotations: { 'test.com/url-like': 'ul1' } }, + metadata: { annotations: { 'example.com/other-url-like-key': 'ul2' } }, + }); + }); + + it('supports nested dot notated fields properly', () => { + entity.spec = { + ...entity.spec, + 'field-with.dot': 'fd1', + 'other-field-with.dot-also': { + subItem: 'fd2.sub', + 'subite.with/dot': 'fd2.sub.dot', + }, + 'third-field-with.dot-again': 'fd3', + type: 't', + }; + + expect( + parseEntityTransformParams({ + fields: 'kind,spec.other-field-with.dot-also', + })!(entity), + ).toEqual({ + kind: 'k', + spec: { + 'other-field-with.dot-also': { + subItem: 'fd2.sub', + 'subite.with/dot': 'fd2.sub.dot', + }, + }, + }); + expect( + parseEntityTransformParams({ + fields: 'kind,spec.other-field-with.dot-also.subite.with/dot', + })!(entity), + ).toEqual({ + kind: 'k', + spec: { + 'other-field-with.dot-also': { + 'subite.with/dot': 'fd2.sub.dot', + }, + }, }); }); }); diff --git a/plugins/catalog-backend/src/service/request/parseEntityTransformParams.ts b/plugins/catalog-backend/src/service/request/parseEntityTransformParams.ts index adb7b8a133..e09289d5c0 100644 --- a/plugins/catalog-backend/src/service/request/parseEntityTransformParams.ts +++ b/plugins/catalog-backend/src/service/request/parseEntityTransformParams.ts @@ -30,6 +30,8 @@ function getPathArray(input: Entity, field: string) { if (lodash.has(input, pathArray.concat(currentPathPart))) { pathArray.push(currentPathPart); currentPathPart = ''; + } else { + currentPathPart += '.'; } }