aboutsummaryrefslogtreecommitdiffhomepage
path: root/parser/metadecoders
diff options
context:
space:
mode:
authorAcClassic <[email protected]>2022-12-12 18:12:46 +0100
committerBjørn Erik Pedersen <[email protected]>2022-12-14 18:03:47 +0100
commite30d711c29b3a8e93a2e2062149d8f8d9c841f5e (patch)
tree34d621c92dada5b91a4a14939cc02b71102caece /parser/metadecoders
parentad2059878a8d6ace9669ccc5ff0a8d4e5811ad37 (diff)
downloadhugo-e30d711c29b3a8e93a2e2062149d8f8d9c841f5e.tar.gz
hugo-e30d711c29b3a8e93a2e2062149d8f8d9c841f5e.zip
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 #8601
Diffstat (limited to 'parser/metadecoders')
-rw-r--r--parser/metadecoders/decoder.go2
-rw-r--r--parser/metadecoders/decoder_test.go1
2 files changed, 2 insertions, 1 deletions
diff --git a/parser/metadecoders/decoder.go b/parser/metadecoders/decoder.go
index 3db0ae338..a31bcd5cd 100644
--- a/parser/metadecoders/decoder.go
+++ b/parser/metadecoders/decoder.go
@@ -112,7 +112,7 @@ func (d Decoder) UnmarshalStringTo(data string, typ any) (any, error) {
// Unmarshal will unmarshall data in format f into an interface{}.
// This is what's needed for Hugo's /data handling.
func (d Decoder) Unmarshal(data []byte, f Format) (any, error) {
- if data == nil {
+ if data == nil || len(data) == 0 {
switch f {
case CSV:
return make([][]string, 0), nil
diff --git a/parser/metadecoders/decoder_test.go b/parser/metadecoders/decoder_test.go
index 7b762667c..fbeab41a4 100644
--- a/parser/metadecoders/decoder_test.go
+++ b/parser/metadecoders/decoder_test.go
@@ -122,6 +122,7 @@ func TestUnmarshalToInterface(t *testing.T) {
}{
{`[ "Brecker", "Blake", "Redman" ]`, JSON, []any{"Brecker", "Blake", "Redman"}},
{`{ "a": "b" }`, JSON, expect},
+ {``, JSON, map[string]any{}},
{`#+a: b`, ORG, expect},
{`#+DATE: <2020-06-26 Fri>`, ORG, map[string]any{"date": "2020-06-26"}},
{`a = "b"`, TOML, expect},