From 7080db72eb72aeec39b9d40363e8e5034e17a8c2 Mon Sep 17 00:00:00 2001
From: ChengLei Shao <chareice@live.com>
Date: Wed, 24 May 2023 11:24:15 +0800
Subject: [PATCH] chore: update guard with array contains null (#1922)

---
 .../src/__tests__/update-guard.test.ts        | 13 +++++++
 packages/core/database/src/update-guard.ts    | 35 +++++++++++--------
 2 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/packages/core/database/src/__tests__/update-guard.test.ts b/packages/core/database/src/__tests__/update-guard.test.ts
index 206a2d6bf..acc15a1f6 100644
--- a/packages/core/database/src/__tests__/update-guard.test.ts
+++ b/packages/core/database/src/__tests__/update-guard.test.ts
@@ -108,6 +108,19 @@ describe('update-guard', () => {
     });
   });
 
+  test('association with null array', () => {
+    const values = {
+      name: 'u1',
+      posts: [null],
+    };
+
+    const guard = new UpdateGuard();
+    guard.setModel(User.model);
+    const sanitized = guard.sanitize(values);
+
+    expect(sanitized).toEqual({ name: 'u1', posts: [null] });
+  });
+
   test('association black list', () => {
     const values = {
       name: 'username123',
diff --git a/packages/core/database/src/update-guard.ts b/packages/core/database/src/update-guard.ts
index bc5192609..de059b767 100644
--- a/packages/core/database/src/update-guard.ts
+++ b/packages/core/database/src/update-guard.ts
@@ -10,6 +10,7 @@ type UpdateValues = {
 };
 
 type UpdateAction = 'create' | 'update';
+
 export class UpdateGuard {
   model: ModelStatic<any>;
   action: UpdateAction;
@@ -18,6 +19,21 @@ export class UpdateGuard {
   private blackList: BlackList;
   private whiteList: WhiteList;
 
+  static fromOptions(model, options) {
+    const guard = new UpdateGuard();
+    guard.setModel(model);
+    guard.setWhiteList(options.whitelist);
+    guard.setBlackList(options.blacklist);
+    guard.setAction(lodash.get(options, 'action', 'update'));
+    guard.setAssociationKeysToBeUpdate(options.updateAssociationValues);
+
+    if (options.underscored) {
+      guard.underscored = options.underscored;
+    }
+
+    return guard;
+  }
+
   setAction(action: UpdateAction) {
     this.action = action;
   }
@@ -78,6 +94,10 @@ export class UpdateGuard {
       let associationValues = associationsValues[association];
 
       const filterAssociationToBeUpdate = (value) => {
+        if (value === null) {
+          return value;
+        }
+
         const associationKeysToBeUpdate = this.associationKeysToBeUpdate || [];
 
         if (associationKeysToBeUpdate.includes(association)) {
@@ -155,19 +175,4 @@ export class UpdateGuard {
 
     return result;
   }
-
-  static fromOptions(model, options) {
-    const guard = new UpdateGuard();
-    guard.setModel(model);
-    guard.setWhiteList(options.whitelist);
-    guard.setBlackList(options.blacklist);
-    guard.setAction(lodash.get(options, 'action', 'update'));
-    guard.setAssociationKeysToBeUpdate(options.updateAssociationValues);
-
-    if (options.underscored) {
-      guard.underscored = options.underscored;
-    }
-
-    return guard;
-  }
 }