Skip to content

Commit

Permalink
Merge pull request #702 from DoomHEADSHOT/fix/EmptyStringBug
Browse files Browse the repository at this point in the history
fix(core): StreamingServer Settings.remote_https uses null while stremio 4 uses empty string, with tests
  • Loading branch information
elpiel authored Jul 31, 2024
2 parents 5e89768 + 306cff3 commit 4cb5708
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/models/streaming_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::runtime::msg::{
use crate::runtime::{Effect, EffectFuture, Effects, Env, EnvError, EnvFutureExt, UpdateWithCtx};
use crate::types::addon::ResourcePath;
use crate::types::api::SuccessResponse;
use crate::types::empty_string_as_null;
use crate::types::profile::{AuthKey, Profile};
use crate::types::streaming_server::{
DeviceInfo, GetHTTPSResponse, NetworkInfo, Settings, SettingsResponse, Statistics,
Expand Down Expand Up @@ -484,6 +485,7 @@ fn set_settings<E: Env + 'static>(url: &Url, settings: &Settings) -> Effect {
bt_download_speed_soft_limit: f64,
bt_download_speed_hard_limit: f64,
bt_min_peers_for_stable: u64,
#[serde(with = "empty_string_as_null")]
remote_https: Option<String>,
proxy_streams_enabled: bool,
transcode_profile: Option<String>,
Expand Down
3 changes: 3 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ pub use serde_as_ext::*;

mod r#true;
pub use r#true::*;

mod serde_ext;
pub use serde_ext::*;
79 changes: 79 additions & 0 deletions src/types/serde_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
pub mod empty_string_as_null {
use serde::{self, Deserialize, Deserializer, Serializer};

pub fn serialize<S>(value: &Option<String>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match *value {
None => serializer.serialize_str(""),
Some(ref s) => serializer.serialize_str(s),
}
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
D: Deserializer<'de>,
{
let s = Option::<String>::deserialize(deserializer)?;
match s {
Some(s) if !s.is_empty() => Ok(Some(s)),
_ => Ok(None),
}
}

#[cfg(test)]
mod test {

use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
struct TestStruct {
#[serde(with = "super")]
field: Option<String>,
}
#[test]
fn test_empty_field() {
let empty_string = serde_json::json!({
"field": ""
});

let test_struct = TestStruct { field: None };

let null_json = serde_json::json!({
"field":null,
});

assert_eq!(
test_struct,
serde_json::from_value::<TestStruct>(empty_string.clone()).expect("Valid Json")
);
assert_eq!(
empty_string,
serde_json::to_value(&test_struct).expect("Should serialize")
);
assert_eq!(
test_struct,
serde_json::from_value::<TestStruct>(null_json.clone()).expect("Should serialize")
);
}
#[test]
fn test_field() {
let string = serde_json::json!({
"field": "https://example.com"
});

let test_struct = TestStruct {
field: Some("https://example.com".to_string()),
};

assert_eq!(
test_struct,
serde_json::from_value::<TestStruct>(string.clone()).expect("Valid Json")
);
assert_eq!(
string,
serde_json::to_value(test_struct).expect("Should serialize")
);
}
}
}
3 changes: 3 additions & 0 deletions src/types/streaming_server/settings.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DefaultOnError};

use crate::types::serde_ext::empty_string_as_null;

#[serde_as]
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Settings {
pub app_path: String,
pub cache_root: String,
pub server_version: String,
#[serde(with = "empty_string_as_null")]
pub remote_https: Option<String>,
// Set to default in case the value is anything other than a string or null
#[serde(default)]
Expand Down

0 comments on commit 4cb5708

Please sign in to comment.