Browse Source

fix(server): IdCodecService handles null / undefined values

Michael Bromley 7 years ago
parent
commit
db085a8c2b

+ 5 - 0
server/src/service/id-codec.service.spec.ts

@@ -31,6 +31,11 @@ describe('IdCodecService', () => {
             expect(result).toEqual(ENCODED);
         });
 
+        it('passes through null or undefined without throwing', () => {
+            expect(idCodecService.encode(null as any)).toBeNull();
+            expect(idCodecService.encode(undefined as any)).toBeUndefined();
+        });
+
         it('works with simple entity', () => {
             const input = { id: 'id', name: 'foo' };
 

+ 3 - 0
server/src/service/id-codec.service.ts

@@ -29,6 +29,9 @@ export class IdCodecService {
     }
 
     private transformRecursive<T>(target: T, transformFn: (input: any) => ID): T {
+        if (target == null) {
+            return target;
+        }
         if (typeof target === 'string' || typeof target === 'number') {
             return transformFn(target as any) as any;
         }