-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #702 from DoomHEADSHOT/fix/EmptyStringBug
fix(core): StreamingServer Settings.remote_https uses null while stremio 4 uses empty string, with tests
- Loading branch information
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,6 @@ pub use serde_as_ext::*; | |
|
||
mod r#true; | ||
pub use r#true::*; | ||
|
||
mod serde_ext; | ||
pub use serde_ext::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters