Skip to content

Commit

Permalink
Fix conversion of quoted strings in YAML sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
speth authored and ischoegl committed Jul 29, 2024
1 parent 6a5f6a2 commit 75bb717
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/base/AnyMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ Type elementTypes(const YAML::Node& node)
types = types | Type::Sequence;
} else if (el.IsScalar()) {
string nodestr = el.as<string>();
if (isInt(nodestr)) {
if (el.Tag() == "!") {
// Prevent implicit conversion of quoted strings to numeric types
types = types | Type::String;
} else if (isInt(nodestr)) {
types = types | Type::Integer;
} else if (isFloat(nodestr)) {
types = types | Type::Double;
Expand Down
11 changes: 11 additions & 0 deletions test/general/test_containers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,17 @@ TEST(AnyMap, conversion_to_anyvalue)
EXPECT_EQ(n.at("strings").asVector<AnyValue>()[0].asString(), "foo");
}

TEST(AnyMap, conversion_from_quoted)
{
AnyMap m = AnyMap::fromYamlString(
"{top: '99', mixed: [5.1, '122599', '3.140'], uniform: ['99', 100.1']}");
EXPECT_TRUE(m["top"].is<string>());
EXPECT_FALSE(m["top"].is<int>());
EXPECT_THROW(m["mixed"].asVector<double>(), InputFileError);
EXPECT_DOUBLE_EQ(m["mixed"].asVector<AnyValue>()[0].asDouble(), 5.1);
EXPECT_THROW(m["uniform"].asVector<double>(), InputFileError);
}

TEST(AnyMap, read_subnormal)
{
AnyMap m = AnyMap::fromYamlString(
Expand Down

0 comments on commit 75bb717

Please sign in to comment.