Skip to content

Commit e30d711

Browse files
acclassicbep
authored andcommitted
parser/metadecoders: Add empty /data JSON file as empty map
When fetching JSON files from the /data folder that are empty they will be added as empty map[string]any. This makes sure that no empty JSON file causes the site to crash because of a failed unmarshal. This happens because empty is not a valid JSON string. It is therefore important to check the lenght of the data before passing it to the JSON unmarshal function. Fixes gohugoio#8601
1 parent ad20598 commit e30d711

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

parser/metadecoders/decoder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (d Decoder) UnmarshalStringTo(data string, typ any) (any, error) {
112112
// Unmarshal will unmarshall data in format f into an interface{}.
113113
// This is what's needed for Hugo's /data handling.
114114
func (d Decoder) Unmarshal(data []byte, f Format) (any, error) {
115-
if data == nil {
115+
if data == nil || len(data) == 0 {
116116
switch f {
117117
case CSV:
118118
return make([][]string, 0), nil

parser/metadecoders/decoder_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func TestUnmarshalToInterface(t *testing.T) {
122122
}{
123123
{`[ "Brecker", "Blake", "Redman" ]`, JSON, []any{"Brecker", "Blake", "Redman"}},
124124
{`{ "a": "b" }`, JSON, expect},
125+
{``, JSON, map[string]any{}},
125126
{`#+a: b`, ORG, expect},
126127
{`#+DATE: <2020-06-26 Fri>`, ORG, map[string]any{"date": "2020-06-26"}},
127128
{`a = "b"`, TOML, expect},

0 commit comments

Comments
 (0)