]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
parser/metadecoders: Add empty /data JSON file as empty map
authorAcClassic <cantillo.trd@gmail.com>
Mon, 12 Dec 2022 17:12:46 +0000 (18:12 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 14 Dec 2022 17:03:47 +0000 (18:03 +0100)
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

parser/metadecoders/decoder.go
parser/metadecoders/decoder_test.go

index 3db0ae338f6b0e15dbdbb15248b00bc3074ea332..a31bcd5cdd5ea137cd9b74181e43cda41ff6734b 100644 (file)
@@ -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
index 7b762667c107a14e9d9c151d90a0e0db6d2e0caa..fbeab41a47a4c3409aac231979cf1d26f2253917 100644 (file)
@@ -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},