fix: fall back to the default when a servarr returns an unknown enum value
A Servarr build can emit an enum value this app has no name for, either as a string it does not recognise or as a bare integer for a value the build itself cannot name. Until now any such value in a response failed the whole payload, so one unexpected artist status, download status, task name, series type, minimum availability, credit type, or security setting emptied the entire view with "Failed to parse response!". Every enum-typed field on a struct the app only ever deserializes and displays now uses from_json_or_default, so an unrecognised value degrades to that enum's default and the rest of the record is kept. Known values still map exactly as before and serialization is untouched. Fields on request bodies (Add*Body, Add*Options, Edit*Params, the root folder bodies) are deliberately left alone: those structs are only ever serialized, so the attribute would be meaningless there. No candidate struct is round-tripped: every edit and toggle in the app re-fetches the raw JSON value and patches it by key, so the fallback can never be written back to a server as a real setting. The one Option field, the security config's authentication_required, also carries serde(default), because deserialize_with on an Option turns a missing key into a hard error. An unknown value there yields None. Verified by mutation. Removing the attribute from one field per tree fails exactly that field's test and nothing else. Dropping default from the Option field fails only the missing-key test with "missing field authenticationRequired"; dropping deserialize_with from it fails only the unknown-value test. All 21 new fallback tests fail against the unguarded code with "invalid type: integer 2, expected string or map".
This commit is contained in:
10 files changed
+529
-6
No files matched your search
@@ -1,6 +1,7 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use pretty_assertions::{assert_eq, assert_str_eq};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::models::{
|
||||
@@ -17,6 +18,9 @@ mod tests {
|
||||
SonarrTaskName,
|
||||
},
|
||||
};
|
||||
use crate::network::sonarr_network::sonarr_network_test_utils::test_utils::{
|
||||
SERIES_JSON, download_record, task,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_episode_display() {
|
||||
@@ -698,6 +702,83 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_series_type_deserialization_falls_back_to_default() {
|
||||
let expected = Series {
|
||||
series_type: SeriesType::default(),
|
||||
..serde_json::from_str(SERIES_JSON).unwrap()
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
deserialize_with_field::<Series>(SERIES_JSON, "seriesType", json!(2)),
|
||||
expected
|
||||
);
|
||||
assert_eq!(
|
||||
deserialize_with_field::<Series>(SERIES_JSON, "seriesType", json!("notAThing")),
|
||||
expected
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_series_status_deserialization_falls_back_to_default() {
|
||||
let expected = Series {
|
||||
status: SeriesStatus::default(),
|
||||
..serde_json::from_str(SERIES_JSON).unwrap()
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
deserialize_with_field::<Series>(SERIES_JSON, "status", json!(2)),
|
||||
expected
|
||||
);
|
||||
assert_eq!(
|
||||
deserialize_with_field::<Series>(SERIES_JSON, "status", json!("notAThing")),
|
||||
expected
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_download_record_status_deserialization_falls_back_to_default() {
|
||||
let download_record_json = serde_json::to_string(&download_record()).unwrap();
|
||||
let expected = DownloadRecord {
|
||||
status: DownloadStatus::default(),
|
||||
..download_record()
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
deserialize_with_field::<DownloadRecord>(&download_record_json, "status", json!(2)),
|
||||
expected
|
||||
);
|
||||
assert_eq!(
|
||||
deserialize_with_field::<DownloadRecord>(&download_record_json, "status", json!("notAThing")),
|
||||
expected
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sonarr_task_task_name_deserialization_falls_back_to_default() {
|
||||
let task_json = serde_json::to_string(&task()).unwrap();
|
||||
let expected = SonarrTask {
|
||||
task_name: SonarrTaskName::default(),
|
||||
..task()
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
deserialize_with_field::<SonarrTask>(&task_json, "taskName", json!(2)),
|
||||
expected
|
||||
);
|
||||
assert_eq!(
|
||||
deserialize_with_field::<SonarrTask>(&task_json, "taskName", json!("notAThing")),
|
||||
expected
|
||||
);
|
||||
}
|
||||
|
||||
fn deserialize_with_field<T: DeserializeOwned>(json: &str, field: &str, value: Value) -> T {
|
||||
let mut fixture_json: Value = serde_json::from_str(json).unwrap();
|
||||
fixture_json[field] = value;
|
||||
|
||||
serde_json::from_value(fixture_json).unwrap()
|
||||
}
|
||||
|
||||
fn deserialize_history_event_type(event_type: Value) -> SonarrHistoryEventType {
|
||||
let history_item_json = json!({
|
||||
"id": 1,
|
||||
|
||||
Reference in new issue
Block a user