Fix comment-only yaml in device config validation (#29673)
This commit is contained in:
parent
338e8b4a46
commit
2c149b908e
2 changed files with 17 additions and 2 deletions
|
|
@ -466,9 +466,9 @@ func decodeDeviceConfig(r io.Reader) (configReq, error) {
|
||||||
return configReq{}, errors.New("invalid config: cannot mix yaml and other")
|
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
|
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
|
return configReq{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,21 @@ func TestYamlFloat(t *testing.T) {
|
||||||
require.NoError(t, yaml.Unmarshal([]byte(b), &res))
|
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) {
|
func TestYamlError(t *testing.T) {
|
||||||
b := `block:
|
b := `block:
|
||||||
data: foo
|
data: foo
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue