shithub: hugo

Download patch

ref: be7c3bbb097eb104c7029d742c6501c5d18d8684
parent: be778c31606b08e7320cafb6082700e4240be2fd
author: bep <[email protected]>
date: Sat May 23 08:28:01 EDT 2015

Prevent cyclic ref crash in JSON encode

Note that this commit makes no promise about great JSON output from the encoder, but the cyclic refs should be broken.

Fixes #1123

--- a/hugolib/node.go
+++ b/hugolib/node.go
@@ -22,7 +22,7 @@
 
 type Node struct {
 	RSSLink template.HTML
-	Site    *SiteInfo
+	Site    *SiteInfo `json:"-"`
 	//	layout      string
 	Data        map[string]interface{}
 	Title       string
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -71,7 +71,7 @@
 	renderingConfigInit sync.Once
 	PageMeta
 	Source
-	Position
+	Position `json:"-"`
 	Node
 	pageMenus     PageMenus
 	pageMenusInit sync.Once
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -79,7 +79,7 @@
 	timer          *nitro.B
 	Targets        targetList
 	targetListInit sync.Once
-	Completed      chan bool
+	Completed      chan bool `json:"-"`
 	RunMode        runmode
 	params         map[string]interface{}
 	draftCount     int
--- /dev/null
+++ b/hugolib/siteJSONEncode_test.go
@@ -1,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)
+	}
+}