Procházet zdrojové kódy

fix bug when relations fields have empty values

Olivier Massot před 2 roky
rodič
revize
e93edccb2e

+ 4 - 0
services/data/normalizer/hydraNormalizer.ts

@@ -153,6 +153,10 @@ class HydraNormalizer {
       const value = instance[field]
       const targetEntity = iriEncodedFields[field].entity
 
+      if (_.isEmpty(value)) {
+        continue
+      }
+
       if (_.isArray(value)) {
         instance[field] = value.map((iri: string) => {
           return HydraNormalizer.getIdFromEntityIri(iri, targetEntity)

+ 28 - 0
tests/units/services/data/normalizer/hydraNormalizer.test.ts

@@ -439,7 +439,35 @@ describe('denormalizeEntity', () => {
             oneToOneRelation: 99,
             oneToManyRelation: [123, 124, 125]
         }))
+    })
+
+    test('should handle relations with empty values', () => {
+        const data = {
+            id: 7351,
+            name: null,
+            oneToOneRelation: null,
+            oneToManyRelation: []
+        }
 
+        //@ts-ignore
+        HydraNormalizer.getIriEncodedFields = vi.fn(
+            (entity) => {
+                return {
+                    oneToOneRelation: DummyApiChild,
+                    oneToManyRelation: DummyApiChild
+                }
+            }
+        )
+
+        //@ts-ignore
+        const result = HydraNormalizer.denormalizeEntity(DummyApiModel, data)
+
+        expect(result).toStrictEqual(new DummyApiModel({
+            id: 7351,
+            name: null,
+            oneToOneRelation: null,
+            oneToManyRelation: []
+        }))
     })
 })