Skip to content

Commit

Permalink
Corrected validation issue introduced in #119, avoid unchecked JSON a…
Browse files Browse the repository at this point in the history
…ccess
  • Loading branch information
BrianMichell committed Sep 24, 2024
1 parent 2c74346 commit 04b25d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
2 changes: 0 additions & 2 deletions mdio/acceptance_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,6 @@ TEST(Variable, optionalAttrs) {
ASSERT_TRUE(f8.status().ok()) << f8.status();
ASSERT_TRUE(voided.status().ok()) << voided.status();

std::cout << i2.value().GetAttributes().dump(4) << std::endl;

EXPECT_EQ(i2.value().GetAttributes()["attributes"]["foo"], "bar")
<< i2.value().GetAttributes();
EXPECT_EQ(i4.value().GetAttributes()["attributes"]["foo"], "bar")
Expand Down
31 changes: 12 additions & 19 deletions mdio/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -638,31 +638,24 @@ Future<Variable<T, R, M>> OpenVariable(const nlohmann::json& json_store,
// specifies a different chunkGrid, it will not be used and should
// actually fail here.
nlohmann::json correctedSuppliedAttrs = suppliedAttributes;
if (correctedSuppliedAttrs["attributes"].contains("metadata")) {
if (correctedSuppliedAttrs["attributes"]["metadata"].contains(
"chunkGrid")) {
correctedSuppliedAttrs["attributes"]["metadata"].erase("chunkGrid");
if (correctedSuppliedAttrs.contains("attributes")) {
if (correctedSuppliedAttrs["attributes"].contains("metadata")) {
if (correctedSuppliedAttrs["attributes"]["metadata"].contains(
"chunkGrid")) {
correctedSuppliedAttrs["attributes"]["metadata"].erase("chunkGrid");
}
}
for (auto& item :
correctedSuppliedAttrs["attributes"]["metadata"].items()) {
correctedSuppliedAttrs["attributes"][item.key()] =
std::move(item.value());
auto savedAttrs = correctedSuppliedAttrs["attributes"];
correctedSuppliedAttrs.erase("attributes");
for (auto& item : savedAttrs.items()) {
correctedSuppliedAttrs[item.key()] = std::move(item.value());
}
correctedSuppliedAttrs["attributes"].erase("metadata");
}
// BFS to make sure supplied attributes match stored attributes
nlohmann::json searchableMetadata = new_metadata;
if (searchableMetadata["attributes"].contains("variable_name")) {
if (searchableMetadata.contains("variable_name")) {
// Since we don't actually want to have to specify the variable name
searchableMetadata["attributes"].erase("variable_name");
}
if (searchableMetadata["attributes"].contains("metadata")) {
for (auto& item :
searchableMetadata["attributes"]["metadata"].items()) {
searchableMetadata["attributes"][item.key()] =
std::move(item.value());
}
searchableMetadata["attributes"].erase("metadata");
searchableMetadata.erase("variable_name");
}
std::queue<std::pair<nlohmann::json, nlohmann::json>> queue;
queue.push({searchableMetadata, correctedSuppliedAttrs});
Expand Down

0 comments on commit 04b25d0

Please sign in to comment.