diff --git a/parser/metadecoders/decoder.go b/parser/metadecoders/decoder.go index eb33f1ee9..1655ea513 100644 --- a/parser/metadecoders/decoder.go +++ b/parser/metadecoders/decoder.go @@ -152,7 +152,19 @@ func (d Decoder) UnmarshalTo(data []byte, f Format, v any) error { if err != nil { return toFileError(f, data, fmt.Errorf("failed to unmarshal XML: %w", err)) } - xmlValue = xmlRoot[xmlRootName].(map[string]any) + + // Get the root value and verify it's a map + rootValue := xmlRoot[xmlRootName] + if rootValue == nil { + return toFileError(f, data, fmt.Errorf("XML root element '%s' has no value", xmlRootName)) + } + + // Type check before conversion + mapValue, ok := rootValue.(map[string]any) + if !ok { + return toFileError(f, data, fmt.Errorf("XML root element '%s' must be a map/object, got %T", xmlRootName, rootValue)) + } + xmlValue = mapValue } switch v := v.(type) { diff --git a/parser/metadecoders/decoder_test.go b/parser/metadecoders/decoder_test.go index f0ebe57e5..d78293402 100644 --- a/parser/metadecoders/decoder_test.go +++ b/parser/metadecoders/decoder_test.go @@ -99,6 +99,7 @@ func TestUnmarshalToMap(t *testing.T) { // errors {`a = b`, TOML, false}, {`a,b,c`, CSV, false}, // Use Unmarshal for CSV + {`just a string`, XML, false}, } { msg := qt.Commentf("%d: %s", i, test.format) m, err := d.UnmarshalToMap([]byte(test.data), test.format)