Jelajahi Sumber

fix(core): Fix error when patching custom fields

Michael Bromley 6 tahun lalu
induk
melakukan
a3afc1b918

+ 19 - 0
packages/core/src/service/helpers/utils/patch-entity.spec.ts

@@ -60,4 +60,23 @@ describe('patchEntity()', () => {
             },
         });
     });
+
+    it('handles missing input customFields', () => {
+        const entity: any = {
+            f1: 'f1',
+            customFields: {
+                cf1: 'cf1',
+                cf2: 'cf2',
+            },
+        };
+
+        const result = patchEntity(entity, { f1: 'foo' });
+        expect(result).toEqual({
+            f1: 'foo',
+            customFields: {
+                cf1: 'cf1',
+                cf2: 'cf2',
+            },
+        });
+    });
 });

+ 1 - 1
packages/core/src/service/helpers/utils/patch-entity.ts

@@ -10,7 +10,7 @@ export type InputPatch<T> = { [K in keyof T]?: T[K] | null };
 export function patchEntity<T extends VendureEntity, I extends InputPatch<T>>(entity: T, input: I): T {
     for (const key of Object.keys(entity)) {
         const value = input[key as keyof T];
-        if (key === 'customFields') {
+        if (key === 'customFields' && value) {
             patchEntity((entity as any)[key], value as any);
         } else if (value !== undefined && key !== 'id') {
             entity[key as keyof T] = value as any;