Skip to content

fix: handle non-mapping YAML root in YamlConfigurationFileParser#1386

Open
smuchow1962 wants to merge 1 commit into
net-daemon:mainfrom
smuchow1962:fix/yaml-parser-non-mapping-root
Open

fix: handle non-mapping YAML root in YamlConfigurationFileParser#1386
smuchow1962 wants to merge 1 commit into
net-daemon:mainfrom
smuchow1962:fix/yaml-parser-non-mapping-root

Conversation

@smuchow1962
Copy link
Copy Markdown

Summary

The host crashes at config-build time when an app-config folder contains a YAML file whose root is not a mapping.

I hit this running a real config: a Home Assistant !include target like automations.yaml whose content is a sequence root ([]) gets picked up by AddYamlAppConfig, and the parser casts the root unconditionally to a mapping. That throws InvalidCastException from inside HostBuilder.Build(), before the host (and any logging) is up, so the failure is opaque.

Repro

An app-config directory containing a YAML file with a sequence root:

# automations.yaml
[]

AddYamlAppConfig feeds it through YamlConfigurationFileParser, and (YamlMappingNode)RootNode throws during HostBuilder.Build().

Root cause

The parser guards only the zero-document case, then casts any root unconditionally to YamlMappingNode. A sequence root or a scalar root is a valid YAML document, so the cast throws instead of being handled.

Fix

A pattern-match guard. The configuration model is key/value pairs, so only a mapping root carries config data. A non-mapping root carries no key/value config, so it contributes nothing instead of throwing. This mirrors the existing empty-file guard right above it.

if (yaml.Documents[0].RootNode is not YamlMappingNode mapping) return _data;

Tests

Added regression cases to ConfigTests:

  • sequence root ([]) — the automations.yaml case
  • sequence root with items
  • scalar root
  • empty file

Each asserts the built configuration contributes no data instead of throwing.

Notes

Found by running a real config, not synthetically. Frank asked for the fix to be sent upstream.

I couldn't apply the pr: bugfix label from a fork — a maintainer may want to add it.

AddYamlAppConfig feeds every *.y* file under the app-config folder through
YamlConfigurationFileParser. The parser guarded only the empty-document case
and then cast the root node unconditionally to YamlMappingNode:

    var mapping = (YamlMappingNode)yaml.Documents[0].RootNode;

Home Assistant !include targets such as automations.yaml have a sequence root
(automations.yaml is literally "[]"), so the cast throws
InvalidCastException: YamlSequenceNode -> YamlMappingNode inside
HostBuilder.Build() -> InitializeAppConfiguration() at config-build time,
before the host starts.

Guard the root-node type the same way the empty-document case is already
guarded: a non-mapping root carries no key/value config, so it contributes no
data instead of throwing. Adds regression tests for sequence-root,
scalar-root, and empty YAML files.
@FrankBakkerNl FrankBakkerNl enabled auto-merge (squash) June 3, 2026 22:12
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82%. Comparing base (0116c98) to head (b17179a).

Additional details and impacted files
@@         Coverage Diff          @@
##           main   #1386   +/-   ##
====================================
- Coverage    82%     82%   -1%     
====================================
  Files       202     202           
  Lines      3941    3941           
  Branches    444     445    +1     
====================================
- Hits       3240    3239    -1     
- Misses      531     532    +1     
  Partials    170     170           
Flag Coverage Δ
unittests 82% <100%> (-1%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants