Fix comment-only yaml in device config validation (#29673)

This commit is contained in:
andig 2026-05-05 18:11:05 +02:00 • committed by GitHub
parent 338e8b4a46
commit 2c149b908e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 2 deletions

View file

@ -466,9 +466,9 @@ func decodeDeviceConfig(r io.Reader) (configReq, error) {
return configReq{}, errors.New("invalid config: cannot mix yaml and other")
}
// validate yaml syntax
// validate yaml syntax; tolerate whitespace/comment-only input
var tmp map[string]any
if err := yaml.Unmarshal([]byte(res.Yaml), &tmp); err != nil && err != io.EOF {
if err := yaml.Unmarshal([]byte(res.Yaml), &tmp); err != nil && !strings.Contains(err.Error(), "no documents in stream") {
return configReq{}, err
}

View file

@ -13,6 +13,21 @@ func TestYamlFloat(t *testing.T) {
require.NoError(t, yaml.Unmarshal([]byte(b), &res))
}
func TestYamlEmpty(t *testing.T) {
var res map[string]any
err := yaml.Unmarshal([]byte(""), &res)
require.ErrorContains(t, err, "no documents in stream")
}
func TestYamlCommentsOnly(t *testing.T) {
b := `# just a comment
# another comment
`
var res map[string]any
err := yaml.Unmarshal([]byte(b), &res)
require.ErrorContains(t, err, "no documents in stream")
}
func TestYamlError(t *testing.T) {
b := `block:
data: foo