Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): fix InvokeBody::Raw deserialization #10138

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading