Prevent cyclic ref crash in JSON encode
authorbep <bjorn.erik.pedersen@gmail.com>
Sat, 23 May 2015 10:28:01 +0000 (12:28 +0200)
committerbep <bjorn.erik.pedersen@gmail.com>
Sat, 23 May 2015 10:28:08 +0000 (12:28 +0200)
Note that this commit makes no promise about great JSON output from the encoder, but the cyclic refs should be broken.

Fixes #1123

hugolib/node.go
hugolib/page.go
hugolib/site.go
hugolib/siteJSONEncode_test.go [new file with mode: 0644]

index 7606e88963b2d0962f3344438714838196eea16f..cbc314049b55ca2f627284617945a6a111e7663d 100644 (file)
@@ -22,7 +22,7 @@ import (
 
 type Node struct {
        RSSLink template.HTML
-       Site    *SiteInfo
+       Site    *SiteInfo `json:"-"`
        //      layout      string
        Data        map[string]interface{}
        Title       string
index fd51e72633f840cf3fd0357801badc01c63526d1..391fedf380cbc5940804de6e1e0c873b87544014 100644 (file)
@@ -71,7 +71,7 @@ type Page struct {
        renderingConfigInit sync.Once
        PageMeta
        Source
-       Position
+       Position `json:"-"`
        Node
        pageMenus     PageMenus
        pageMenusInit sync.Once
index 7f2016aced0de00ba5eaf8a7a57ababe413331e2..67d8c50ec4bdac09b6f52524e9bd3a582ad856f6 100644 (file)
@@ -79,7 +79,7 @@ type Site struct {
        timer          *nitro.B
        Targets        targetList
        targetListInit sync.Once
-       Completed      chan bool
+       Completed      chan bool `json:"-"`
        RunMode        runmode
        params         map[string]interface{}
        draftCount     int
diff --git a/hugolib/siteJSONEncode_test.go b/hugolib/siteJSONEncode_test.go
new file mode 100644 (file)
index 0000000..7c4bb48
--- /dev/null
@@ -0,0 +1,32 @@
+package hugolib
+
+import (
+       "encoding/json"
+       "fmt"
+       "testing"
+)
+
+// Issue #1123
+// Testing prevention of cyclic refs in JSON encoding
+// May be smart to run with: -timeout 4000ms
+func TestEncodePage(t *testing.T) {
+
+       // borrowed from menu_test.go
+       s := createTestSite(MENU_PAGE_SOURCES)
+       testSiteSetup(s, t)
+
+       j, err := json.Marshal(s)
+       check(t, err)
+       fmt.Println("Site as JSON", string(j))
+
+       p, err := json.Marshal(s.Pages[0])
+       check(t, err)
+       fmt.Println("Page as JSON", string(p))
+
+}
+
+func check(t *testing.T, err error) {
+       if err != nil {
+               t.Fatalf("Failed %s", err)
+       }
+}