Skip to content

Commit

Permalink
fix(core): fix InvokeBody::Raw deserialization (#10138)
Browse files Browse the repository at this point in the history
closes #9948
  • Loading branch information
amrbashir authored Jun 27, 2024
1 parent 2783836 commit e93ca1d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/invoke-body-raw-deserialization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": "patch:bug"
---

Fix `InvokeBody::deserialize` method deserialization for `InvokeBody::Raw` variant
32 changes: 30 additions & 2 deletions core/tauri/src/ipc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use std::sync::{Arc, Mutex};

use futures_util::Future;
use http::HeaderMap;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde::{
de::{DeserializeOwned, IntoDeserializer},
Deserialize, Serialize,
};
use serde_json::Value as JsonValue;
pub use serialize_to_javascript::Options as SerializeOptions;
use tauri_macros::default_runtime;
Expand Down Expand Up @@ -89,7 +92,7 @@ impl InvokeBody {
pub fn deserialize<T: DeserializeOwned>(self) -> serde_json::Result<T> {
match self {
InvokeBody::Json(v) => serde_json::from_value(v),
InvokeBody::Raw(v) => serde_json::from_slice(&v),
InvokeBody::Raw(v) => T::deserialize(v.into_deserializer()),
}
}
}
Expand Down Expand Up @@ -518,3 +521,28 @@ impl<R: Runtime> InvokeMessage<R> {
/// The `Callback` type is the return value of the `transformCallback` JavaScript function.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct CallbackFn(pub u32);

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn deserialize_invoke_body() {
let json = InvokeBody::Json(serde_json::Value::Array(vec![
serde_json::Value::Number(1.into()),
serde_json::Value::Number(123.into()),
serde_json::Value::Number(1231.into()),
]));
assert_eq!(json.deserialize::<Vec<u16>>().unwrap(), vec![1, 123, 1231]);

let json = InvokeBody::Json(serde_json::Value::String("string value".into()));
assert_eq!(json.deserialize::<String>().unwrap(), "string value");

let json = InvokeBody::Json(serde_json::Value::String("string value".into()));
assert!(json.deserialize::<Vec<u16>>().is_err());

let values = vec![1, 2, 3, 4, 5, 6, 1];
let raw = InvokeBody::Raw(values.clone());
assert_eq!(raw.deserialize::<Vec<u8>>().unwrap(), values);
}
}

0 comments on commit e93ca1d

Please sign in to comment.