From 7917a276fa199b19d6d8f2e3d2fa1708e3481c2b Mon Sep 17 00:00:00 2001
From: vsavkin <avix1000@gmail.com>
Date: Fri, 9 Mar 2018 11:25:42 -0500
Subject: [PATCH] fix(RouterStore): change the default serializer to work
 around cycles in RouterStateSnapshot

---
 modules/router-store/src/serializer.ts | 44 +++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 5 deletions(-)

diff --git a/modules/router-store/src/serializer.ts b/modules/router-store/src/serializer.ts
index ddeecad0b9..9d7f9bedc4 100644
--- a/modules/router-store/src/serializer.ts
+++ b/modules/router-store/src/serializer.ts
@@ -1,13 +1,47 @@
-import { InjectionToken } from '@angular/core';
-import { RouterStateSnapshot } from '@angular/router';
+import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
 
 export abstract class RouterStateSerializer<T> {
   abstract serialize(routerState: RouterStateSnapshot): T;
 }
 
+export interface SerializedRouterStateSnapshot {
+  root: ActivatedRouteSnapshot;
+  url: string;
+}
+
 export class DefaultRouterStateSerializer
-  implements RouterStateSerializer<RouterStateSnapshot> {
-  serialize(routerState: RouterStateSnapshot) {
-    return routerState;
+  implements RouterStateSerializer<SerializedRouterStateSnapshot> {
+  serialize(routerState: RouterStateSnapshot): SerializedRouterStateSnapshot {
+    return {
+      root: this.serializeRoute(routerState.root),
+      url: routerState.url,
+    };
+  }
+
+  private serializeRoute(
+    route: ActivatedRouteSnapshot
+  ): ActivatedRouteSnapshot {
+    const children = route.children.map(c => this.serializeRoute(c));
+    return {
+      params: route.params,
+      paramMap: route.paramMap,
+      data: route.data,
+      url: route.url,
+      outlet: route.outlet,
+      routeConfig: {
+        component: route.routeConfig ? route.routeConfig.component : undefined,
+      },
+      queryParams: route.queryParams,
+      queryParamMap: route.queryParamMap,
+      fragment: route.fragment,
+      component: (route.routeConfig
+        ? route.routeConfig.component
+        : undefined) as any,
+      root: undefined as any,
+      parent: undefined as any,
+      firstChild: children[0],
+      pathFromRoot: undefined as any,
+      children,
+    };
   }
 }