simplify the generated filters when parsing a query

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2024-12-05 22:06:28 +01:00
parent 3f72c6c4ca
commit d93390dea7
4 changed files with 20 additions and 18 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend': patch
---
When parsing filters, do not make redundant `anyOf` and `allOf` nodes when there's only a single entry within them
@@ -166,7 +166,7 @@ describe('createRouter readonly disabled', () => {
{ key: 'b', values: ['3'] },
],
},
{ allOf: [{ key: 'c', values: ['4'] }] },
{ key: 'c', values: ['4'] },
],
},
credentials: mockCredentials.user(),
@@ -218,7 +218,7 @@ describe('createRouter readonly disabled', () => {
{ key: 'b', values: ['3'] },
],
},
{ allOf: [{ key: 'c', values: ['4'] }] },
{ key: 'c', values: ['4'] },
],
},
orderFields: [
@@ -258,7 +258,7 @@ describe('createRouter readonly disabled', () => {
{ key: 'b', values: ['3'] },
],
},
{ allOf: [{ key: 'c', values: ['4'] }] },
{ key: 'c', values: ['4'] },
],
},
orderFields: [
@@ -554,13 +554,7 @@ describe('createRouter readonly disabled', () => {
entityRefs: [entityRef],
fields: expect.any(Function),
credentials: mockCredentials.user(),
filter: {
anyOf: [
{
allOf: [{ key: 'kind', values: ['Component'] }],
},
],
},
filter: { key: 'kind', values: ['Component'] },
});
expect(response.status).toEqual(200);
expect(response.body).toEqual({ items: [entity] });
@@ -27,9 +27,7 @@ describe('parseEntityFilterParams', () => {
it('supports single-string format', () => {
const result = parseEntityFilterParams({ filter: 'a=1' })!;
expect(result).toEqual({
anyOf: [{ allOf: [{ key: 'a', values: ['1'] }] }],
});
expect(result).toEqual({ key: 'a', values: ['1'] });
});
it('supports array-of-strings format', () => {
@@ -38,8 +36,8 @@ describe('parseEntityFilterParams', () => {
});
expect(result).toEqual({
anyOf: [
{ allOf: [{ key: 'a', values: ['1'] }] },
{ allOf: [{ key: 'b', values: ['2'] }] },
{ key: 'a', values: ['1'] },
{ key: 'b', values: ['2'] },
],
});
});
@@ -50,7 +48,7 @@ describe('parseEntityFilterParams', () => {
});
expect(result).toEqual({
anyOf: [
{ allOf: [{ key: 'a', values: ['1'] }] },
{ key: 'a', values: ['1'] },
{
allOf: [
{ key: 'b', values: ['2', '3'] },
@@ -36,12 +36,17 @@ export function parseEntityFilterParams(
// Outer array: "any of the inner ones"
// Inner arrays: "all of these must match"
const filters = filterStrings.map(parseEntityFilterString).filter(Boolean);
const filters = filterStrings
.map(parseEntityFilterString)
.filter((r): r is EntitiesSearchFilter[] => Boolean(r));
if (!filters.length) {
return undefined;
}
return { anyOf: filters.map(f => ({ allOf: f! })) };
const outer = filters.map(inner =>
inner.length === 1 ? inner[0] : { allOf: inner },
);
return outer.length === 1 ? outer[0] : { anyOf: outer };
}
/**