dbus2 / zbus-old

Rust D-Bus crate.
https://gitlab.freedesktop.org/dbus/zbus
Other
49 stars 13 forks source link

DeserializeDict fails with Variant(IncorrectType) #251

Closed zeenix closed 1 year ago

zeenix commented 2 years ago

In GitLab by @lichtfeind on Jan 13, 2022, 14:06

When deserializing a Array of (Dict of {String, Variant}) i get Err(Variant(IncorrectType)).

For a valid IP4Config path see in:

path = "/org/freedesktop/NetworkManager/Devices/*",
interface = "org.freedesktop.NetworkManager.Device",
default_service = "org.freedesktop.NetworkManager"
property: ip4Config

This is my minimum example:

use async_std;
use std::{collections::HashMap, error::Error};
use zbus::{dbus_proxy, Connection};
use zvariant::OwnedValue;
use zvariant_derive::{DeserializeDict, SerializeDict, TypeDict};

#[derive(DeserializeDict, SerializeDict, TypeDict, Debug, OwnedValue)]
pub struct IP4Adress {
    prefix: u32,
    address: String,
}

#[dbus_proxy(
    interface = "org.freedesktop.NetworkManager.IP4Config",
    default_service = "org.freedesktop.NetworkManager"
)]
pub trait IP4Config {
    /// AddressData property
    #[dbus_proxy(property)]
    fn address_data(
        &self,
    ) -> zbus::Result<IP4Adress>;

    /// AddressData property
    #[dbus_proxy(name = "AddressData", property)]
    fn address_data2(
        &self,
    ) -> zbus::Result<Vec<HashMap<String, OwnedValue>>>;
}

#[async_std::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let connection = Connection::system().await?;

    let ip_4_proxy = IP4ConfigProxy::builder(&connection)
        .path("/org/freedesktop/NetworkManager/IP4Config/183")? // Needs to be changed to valid path
        .build()
        .await?;

    let ip_address = ip_4_proxy.address_data2().await;
    println!("  {:?}", ip_address);

    let ip_address = ip_4_proxy.address_data().await;
    println!("  {:?}", ip_address);
    Ok(())
}

main.rs

  Ok([{"address": OwnedValue(Str(Str(Owned("192.168.178.44")))), "prefix": OwnedValue(U32(24))}])
  Err(Variant(IncorrectType))
zeenix commented 2 years ago

mentioned in commit 07d96b3d53ca9846e0fbbcc712027ee3cb768efa

zeenix commented 2 years ago

@lichtfeind I tested my fixes with your code and everything works now. You'll have to make a few changes though:

use zbus::{
    dbus_proxy,
    Connection,
    // Not required but best to use zvariant and macros through zbus directly.
    // With the latest zvariant release this wasn't possible but I fixed the issue now.
    zvariant::{DeserializeDict, SerializeDict, Type, Value, OwnedValue},
};

// You'll need to also use `Value` derive here, not just `OwnedValue` (an issue I need to look into).
// Also you can now just use `Type` instead of `DictType`.
#[derive(DeserializeDict, SerializeDict, Type, Debug, Value, OwnedValue)]
// This attribute needs to be specified now that `DictType` is not needed and is deprecated.
#[zvariant(signature = "dict")]
pub struct IP4Adress {
    prefix: u32,
    address: String,
}
zeenix commented 2 years ago

In GitLab by @lichtfeind on Jan 21, 2022, 11:22

Yes it is now working. Thanks for the fast fix.