Skip to content

Commit f6cd0c8

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Fix YAML syntax error reporting on non-map YAML (e.g. "hello")
PiperOrigin-RevId: 889328156
1 parent f4d72d9 commit f6cd0c8

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

env/env_yaml.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,15 @@ void EmitFunctionConfigs(const Config& env_config, YAML::Emitter& out) {
998998
absl::StatusOr<Config> EnvConfigFromYaml(const std::string& yaml) {
999999
Config config;
10001000
CEL_ASSIGN_OR_RETURN(YAML::Node root, LoadYaml(yaml));
1001+
if (!root.IsDefined() || root.IsNull()) {
1002+
return config;
1003+
}
1004+
1005+
if (!root.IsMap()) {
1006+
return absl::InvalidArgumentError(FormatYamlErrorMessage(
1007+
yaml, "Invalid CEL environment config YAML", root.Mark()));
1008+
}
1009+
10011010
CEL_RETURN_IF_ERROR(ParseName(config, yaml, root));
10021011
CEL_RETURN_IF_ERROR(ParseContainerConfig(config, yaml, root));
10031012
CEL_RETURN_IF_ERROR(ParseExtensionConfigs(config, yaml, root));

env/env_yaml_test.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,12 @@ TEST_P(EnvYamlParseTest, EnvYamlSyntaxError) {
528528
INSTANTIATE_TEST_SUITE_P(
529529
EnvYamlParseTest, EnvYamlParseTest,
530530
::testing::Values(
531+
ParseTestCase{
532+
.yaml = R"yaml( invalid yaml )yaml",
533+
.expected_error = "1:2: Invalid CEL environment config YAML\n"
534+
"| invalid yaml \n"
535+
"| ^",
536+
},
531537
ParseTestCase{
532538
.yaml = R"yaml(
533539
name:

0 commit comments

Comments
 (0)