Aleph-Alpha/ts-rs

prevent warning for deserialize_with- Require Option<T> value, where null == None

Zerowalker opened this issue · 1 comments

I got cases where i want the values to be specified and null indicates that it's not used.
To do this i got something like:

#[derive(TS, Deserialize, Serialize, Debug, Clone)]
#[ts(export)]
pub struct Foo{
    pub id: i32,
    #[serde(deserialize_with = "require_value_or_null")]
    pub value: Option<String>,
}
pub fn require_value_or_null<'de, D, T>(deserializer: D) -> Result<Option<T>, D::Error>
where
    D: Deserializer<'de>,
    for<'a> T: Deserialize<'a>,
{
    #[derive(Deserialize, Debug)]
    #[serde(untagged)]
    enum Aux<T> {
        T(T),
        Null,
    }
    match Deserialize::deserialize(deserializer)? {
        Aux::T(t) => Ok(Some(t)),
        Aux::Null => Ok(None),
    }
}

This works but as deserialize_with isn't supported by TS it will throw a warning.
Is there a way to achieve this without that?

raine commented

I have a similar problem:

#[derive(Debug, Serialize, Deserialize, TS)]
#[ts(export)]
#[serde(rename_all = "camelCase")]
pub struct ProductVariant {
    pub product_id: i32,
    pub product_page_id: i32,
    #[ts(type = "number")]
    #[serde(serialize_with = "serialize_decimal_as_f64")]
    pub price: Decimal,

This prints a warning when building.

warning: failed to parse serde attribute
  |
  | #[serde(serialize_with = "serialize_decimal_as_f64")]
  |
  = note: ts-rs failed to parse this attribute. It will be ignored.