Skip to content
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 · 1 comment
Closed
Labels

Comments

@vassudanagunta
Copy link
Contributor

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 pushed a commit that referenced this issue Feb 2, 2018
*  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 Two array data sources at the same level results in golang Panic Second array data source can result in golang Panic Feb 3, 2018
@vassudanagunta vassudanagunta changed the title Second array data source can result in golang Panic Array data source in the path of another can result in golang Panic Feb 3, 2018
@bep bep closed this as completed in bb549a0 Feb 12, 2018
@github-actions
Copy link

github-actions bot commented Mar 8, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant