New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array data source in the path of another can result in golang Panic #4366

Closed
vassudanagunta opened this Issue Feb 1, 2018 · 0 comments

Comments

Projects
None yet
1 participant
@vassudanagunta
Contributor

vassudanagunta commented Feb 1, 2018

When two source data files at the same level in the Site.Data tree contain array data at the top level, such as the following:

themes/mytheme/data/a/b1.json:

["x", "y", "z"]

data/a/b1.json:

["1", "2", "3"]

...the following error occurs:

PANIC: interface conversion: interface {} is []interface {}, not map[string]interface {}

The expected behavior is that Site.Data.a.b1 = ["1", "2", "3"]

The culprit is the Site.handleDataFile(...) in site.go, in which the following section of code checks for pre-existing data at a node in the tree and incorrectly assumes it is a map node:

hugo/hugolib/site.go

Lines 815 to 817 in 19e26cb

// Copy content from current to data when needed
if _, ok := current[r.BaseFileName()]; ok {
data := data.(map[string]interface{})

This is a pre-existing bug that goes back to at least 2015.

Add the following test to datafiles_test.go to uncover (and cover) this case:

func TestDataDirMultipleSourcesArrayFile(t *testing.T) {
	t.Parallel()

	sources := [][2]string{
		{filepath.FromSlash("themes/mytheme/data/a/b1.json"), `["x", "y", "z"]`},
		{filepath.FromSlash("data/a/b1.json"), `["1", "2", "3"]`},
	}

	// Per handleDataFile() comment:
	// 1. A theme uses the same key; the main data folder wins
	// 2. A sub folder uses the same key: the sub folder wins
	expected, err := parser.HandleJSONMetaData([]byte(`
{ "a" : {
        "b1": ["1", "2", "3"]
        }
}`))

	if err != nil {
		t.Errorf("Error %s", err)
		return
	}

	doTestDataDir(t, expected, sources,
		"theme", "mytheme")
}

(bug uncovered while investigating #4361.

bep added a commit that referenced this issue Feb 2, 2018

Increase data directory test coverage
*  Adds retro-coverage for #4361
*  Verifies open issues #4138, #3890, #4366, 4083
*  Removes test reliance on the very code it is testing (hugo/parser package).
   Expected results are now all built manually / are more precise.
   Tests can run against different versions (no linkage errs)

@vassudanagunta vassudanagunta changed the title from Two array data sources at the same level results in golang Panic to Second array data source can result in golang Panic Feb 3, 2018

@vassudanagunta vassudanagunta changed the title from Second array data source can result in golang Panic to Array data source in the path of another can result in golang Panic Feb 3, 2018

@bep bep closed this in bb549a0 Feb 12, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment