Bläddra i källkod

fix(core): ListQueryBuilder handles empty in operator

Michael Bromley 3 år sedan
förälder
incheckning
229affffb0

+ 28 - 0
packages/core/e2e/list-query-builder.e2e-spec.ts

@@ -364,6 +364,20 @@ describe('ListQueryBuilder', () => {
             expect(getItemLabels(testEntities.items)).toEqual(['A', 'F']);
         });
 
+        it('in with empty set', async () => {
+            const { testEntities } = await adminClient.query(GET_LIST, {
+                options: {
+                    filter: {
+                        ownerId: {
+                            in: [],
+                        },
+                    },
+                },
+            });
+
+            expect(getItemLabels(testEntities.items)).toEqual([]);
+        });
+
         it('notIn', async () => {
             const { testEntities } = await adminClient.query(GET_LIST, {
                 options: {
@@ -378,6 +392,20 @@ describe('ListQueryBuilder', () => {
             expect(getItemLabels(testEntities.items)).toEqual(['B', 'C', 'D', 'E']);
         });
 
+        it('notIn with empty set', async () => {
+            const { testEntities } = await adminClient.query(GET_LIST, {
+                options: {
+                    filter: {
+                        ownerId: {
+                            notIn: [],
+                        },
+                    },
+                },
+            });
+
+            expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
+        });
+
         describe('regex', () => {
             it('simple substring', async () => {
                 const { testEntities } = await adminClient.query(GET_LIST, {

+ 10 - 3
packages/core/src/service/helpers/list-query-builder/parse-filter-params.ts

@@ -20,7 +20,7 @@ import { getCalculatedColumns } from './get-calculated-columns';
 
 export interface WhereCondition {
     clause: string;
-    parameters: { [param: string]: string | number };
+    parameters: { [param: string]: string | number | string[] };
 }
 
 type AllOperators = StringOperators & BooleanOperators & NumberOperators & DateOperators & ListOperators;
@@ -87,6 +87,7 @@ function buildWhereCondition(
     argIndex: number,
     dbType: ConnectionOptions['type'],
 ): WhereCondition {
+    const emptySetPlaceholder = '___empty_set_placeholder___';
     switch (operator) {
         case 'eq':
             return {
@@ -118,12 +119,18 @@ function buildWhereCondition(
         case 'in':
             return {
                 clause: `${fieldName} IN (:...arg${argIndex})`,
-                parameters: { [`arg${argIndex}`]: operand },
+                parameters: {
+                    [`arg${argIndex}`]:
+                        Array.isArray(operand) && operand.length ? operand : [emptySetPlaceholder],
+                },
             };
         case 'notIn':
             return {
                 clause: `${fieldName} NOT IN (:...arg${argIndex})`,
-                parameters: { [`arg${argIndex}`]: operand },
+                parameters: {
+                    [`arg${argIndex}`]:
+                        Array.isArray(operand) && operand.length ? operand : [emptySetPlaceholder],
+                },
             };
         case 'regex':
             return {