Skip to content

Commit 33407aa

Browse files
FedShatapolukhin
authored andcommitted
fix: fix yaml_config mode when accessing array by index
When accessing YamlConfig array by index returned value always contained `kSecure` mode, despite the original config mode --- Pull Request resolved: #1118 Co-authored-by: antoshkka <antoshkka@userver.tech> Co-authored-by: antoshkka <antoshkka@userver.tech> commit_hash:8bc706f671822dd201ce1079029398d1c5a1cb56
1 parent 1b60cff commit 33407aa

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

universal/src/yaml_config/impl/validate_static_config_test.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,37 @@ additionalProperties: false
425425
UEXPECT_NO_THROW(Validate(static_config, schema));
426426
}
427427

428+
TEST(StaticConfigValidator, ArrayWithEnvPassed) {
429+
const std::string static_config = R"(
430+
component:
431+
values:
432+
- value#env: ENV_VALUE
433+
)";
434+
435+
const std::string schema = R"(
436+
type: object
437+
description: test component with nested array
438+
additionalProperties: false
439+
properties:
440+
values:
441+
type: array
442+
description: values
443+
items:
444+
type: object
445+
additionalProperties: false
446+
properties:
447+
value:
448+
type: string
449+
description: value
450+
)";
451+
452+
UEXPECT_THROW(Validate(static_config, schema), formats::yaml::MemberMissingException);
453+
454+
// NOLINTNEXTLINE(concurrency-mt-unsafe)
455+
::setenv("ENV_VALUE", "100", 1);
456+
UEXPECT_THROW(Validate(static_config, schema), formats::yaml::MemberMissingException);
457+
}
458+
428459
TEST(StaticConfigValidator, Enum) {
429460
const std::string correct_static_config = R"(
430461
mode: on

universal/src/yaml_config/yaml_config.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ YamlConfig YamlConfig::operator[](size_t index) const {
227227
return MakeMissingConfig(*this, index)[value.As<std::string>()];
228228
}
229229

230-
return {std::move(value), config_vars_};
230+
return {std::move(value), config_vars_, mode_};
231231
}
232232

233233
std::size_t YamlConfig::GetSize() const { return yaml_.GetSize(); }

universal/src/yaml_config/yaml_config_test.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,30 @@ TEST(YamlConfig, SampleEnv) {
9393
/// [sample env]
9494
}
9595

96+
TEST(YamlConfig, SampleEnvInArray) {
97+
auto node = formats::yaml::FromString(R"(
98+
some_elements:
99+
- some#env: ENV_VARIABLE_NAME_1
100+
- some#env: ENV_VARIABLE_NAME_2
101+
)");
102+
103+
// NOLINTNEXTLINE(concurrency-mt-unsafe)
104+
::setenv("ENV_VARIABLE_NAME_1", "100", 1);
105+
// NOLINTNEXTLINE(concurrency-mt-unsafe)
106+
::setenv("ENV_VARIABLE_NAME_2", "100", 1);
107+
108+
const yaml_config::YamlConfig yaml(std::move(node), {}, yaml_config::YamlConfig::Mode::kEnvAllowed);
109+
const auto& array = yaml["some_elements"];
110+
for (const auto& element : array) {
111+
EXPECT_EQ(element["some"].As<int>(), 100);
112+
}
113+
114+
// NOLINTNEXTLINE(concurrency-mt-unsafe)
115+
::unsetenv("ENV_VARIABLE_NAME_1");
116+
// NOLINTNEXTLINE(concurrency-mt-unsafe)
117+
::unsetenv("ENV_VARIABLE_NAME_2");
118+
}
119+
96120
TEST(YamlConfig, SampleMultiple) {
97121
const auto node = formats::yaml::FromString(R"(
98122
# /// [sample multiple]

0 commit comments

Comments
 (0)