Handle empty values array in EntityFilter

While an empty values array is not really a sensible input, if an empty array is provided, technically nothing should be returned as a matching value, thus the existing behavior was technically a bug.

Signed-off-by: Joon Park <joonp@spotify.com>
This commit is contained in:
Joon Park
2021-11-22 21:01:51 +00:00
parent e74daa5893
commit 740f958290
3 changed files with 38 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend': patch
---
Providing an empty values array in an EntityFilter will now return no matches.
@@ -435,5 +435,37 @@ describe('NextEntitiesCatalog', () => {
expect(entities).toContainEqual(entity1);
},
);
it.each(databases.eachSupportedId())(
'should return no matches for an empty values array',
// NOTE: An empty values array is not a sensible input in a realistic scenario.
async databaseId => {
const { knex } = await createDatabase(databaseId);
const entity1: Entity = {
apiVersion: 'a',
kind: 'k',
metadata: { name: 'one' },
spec: {},
};
const entity2: Entity = {
apiVersion: 'a',
kind: 'k',
metadata: { name: 'two' },
spec: {},
};
await addEntityToSearch(knex, entity1);
await addEntityToSearch(knex, entity2);
const catalog = new NextEntitiesCatalog(knex);
const testFilter = {
key: 'kind',
values: [],
};
const request = { filter: testFilter };
const { entities } = await catalog.entities(request);
expect(entities.length).toBe(0);
},
);
});
});
@@ -91,7 +91,7 @@ function addCondition(
if (filter.values) {
if (filter.values.length === 1) {
this.where({ value: filter.values[0].toLowerCase() });
} else if (filter.values.length > 1) {
} else {
this.andWhere(
'value',
'in',