REST: GET: Use convertDBusToJSON
Use new_method_call() + async_send() to get back an sd_bus_message
from the org.freedesktop.DBus.Properties.GetAll call in the GET
handler, and then use convertDBusToJSON to extract any possible
property type instead of having to use a variant with all possible
property types defined ahead of time.
Tested: Did a get on several different paths, including one in
/org/open_power/ that had a signature of a(tx) that previously
didn't return anything.
Resolves openbmc/bmcweb#34
Change-Id: I40309664fa969741c4af9a60b9059c60bf6f35f4
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/include/openbmc_dbus_rest.hpp b/include/openbmc_dbus_rest.hpp
index 907febb..9503191 100644
--- a/include/openbmc_dbus_rest.hpp
+++ b/include/openbmc_dbus_rest.hpp
@@ -1609,12 +1609,15 @@
for (const std::string &interface : interfaceNames)
{
- crow::connections::systemBus->async_method_call(
- [&res, response, propertyName](
- const boost::system::error_code ec,
- const std::vector<std::pair<
- std::string, dbus::utility::DbusVariantType>>
- &properties) {
+ sdbusplus::message::message m =
+ crow::connections::systemBus->new_method_call(
+ connection.first.c_str(), path->c_str(),
+ "org.freedesktop.DBus.Properties", "GetAll");
+ m.append(interface);
+ crow::connections::systemBus->async_send(
+ m, [&res, response,
+ propertyName](const boost::system::error_code ec,
+ sdbusplus::message::message &msg) {
if (ec)
{
BMCWEB_LOG_ERROR << "Bad dbus request error: "
@@ -1622,30 +1625,31 @@
}
else
{
- for (const std::pair<
- std::string,
- dbus::utility::DbusVariantType>
- &property : properties)
+ nlohmann::json properties;
+ int r =
+ convertDBusToJSON("a{sv}", msg, properties);
+ if (r < 0)
{
- // if property name is empty, or matches our
- // search query, add it to the response json
+ BMCWEB_LOG_ERROR
+ << "convertDBusToJSON failed";
+ }
+ else
+ {
+ for (auto &prop : properties.items())
+ {
+ // if property name is empty, or matches
+ // our search query, add it to the
+ // response json
- if (propertyName->empty())
- {
- sdbusplus::message::variant_ns::visit(
- [&response, &property](auto &&val) {
- (*response)[property.first] =
- val;
- },
- property.second);
- }
- else if (property.first == *propertyName)
- {
- sdbusplus::message::variant_ns::visit(
- [&response](auto &&val) {
- (*response) = val;
- },
- property.second);
+ if (propertyName->empty())
+ {
+ (*response)[prop.key()] =
+ std::move(prop.value());
+ }
+ else if (prop.key() == *propertyName)
+ {
+ *response = std::move(prop.value());
+ }
}
}
}
@@ -1666,9 +1670,7 @@
}
res.end();
}
- },
- connection.first, *path,
- "org.freedesktop.DBus.Properties", "GetAll", interface);
+ });
}
}
},