fix: Updated entity query param transform to handle keys with '.' in them.

Signed-off-by: Mark David Avery <mark@webark.cc>
This commit is contained in:
Mark David Avery
2022-04-27 11:02:40 -07:00
parent d06fbf7bb0
commit 2909746147
3 changed files with 46 additions and 3 deletions
+5
View File
@@ -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
@@ -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' } },
});
});
});
@@ -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<string, unknown>,
): ((entity: Entity) => Entity) | undefined {
@@ -46,9 +62,11 @@ export function parseEntityTransformParams(
const output: RecursivePartial<Entity> = {};
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);
}
}