Skip to content

Commit

Permalink
cql3: Fix invalid JSON parsing for JSON objects with ASCII keys
Browse files Browse the repository at this point in the history
For column type of map<ascii, int>, we don't want to parse the map keys as JSON.
Because ascii map keys are not valid JSON. Fixes: scylladb#7949

Signed-off-by: michaelhly <michaelhly@gmail.com>
  • Loading branch information
michaelhly committed Sep 20, 2023
1 parent a56a4b6 commit a0230c5
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions cql3/type_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,20 @@ static bytes from_json_object_aux(const map_type_impl& t, const rjson::value& va
}
std::map<bytes, bytes, serialized_compare> raw_map(t.get_keys_type()->as_less_comparator());
for (auto it = value.MemberBegin(); it != value.MemberEnd(); ++it) {
const abstract_type& key_type = *t.get_keys_type();
bytes value = from_json_object(*t.get_values_type(), it->value);
if (!t.get_keys_type()->is_compatible_with(*utf8_type)) {
if (!key_type.is_compatible_with(*utf8_type) && !key_type.is_compatible_with(*ascii_type)) {
// Keys in maps can only be strings in JSON, but they can also be a string representation
// of another JSON type, which needs to be reparsed. Example - map<frozen<list<int>>, int>
// will be represented like this: { "[1, 3, 6]": 3, "[]": 0, "[1, 2]": 2 }
rjson::value map_key = rjson::parse(rjson::to_string_view(it->name));
raw_map.emplace(from_json_object(*t.get_keys_type(), map_key), std::move(value));
try {
rjson::value map_key = rjson::parse(rjson::to_string_view(it->name));
raw_map.emplace(from_json_object(key_type, map_key), std::move(value));
} catch(rjson::error& e) {
throw marshal_exception(format("Failed parsing map_key {}: {}", it->name, e.what()));
}
} else {
raw_map.emplace(from_json_object(*t.get_keys_type(), it->name), std::move(value));
raw_map.emplace(from_json_object(key_type, it->name), std::move(value));
}
}
return map_type_impl::serialize_to_bytes(raw_map);
Expand Down

0 comments on commit a0230c5

Please sign in to comment.