From 2656a92cd7a14e9c249f51779af205737dd22246 Mon Sep 17 00:00:00 2001 From: Fedor Tolstonozhenko Date: Sun, 29 Sep 2024 21:32:13 +0200 Subject: [PATCH] Fix serialization of empty Options with Some("") --- crates/opensi-core/src/lib.rs | 34 +++++++++++++++------------ crates/opensi-core/src/serde_utils.rs | 3 +-- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/crates/opensi-core/src/lib.rs b/crates/opensi-core/src/lib.rs index ad78b61..849545e 100644 --- a/crates/opensi-core/src/lib.rs +++ b/crates/opensi-core/src/lib.rs @@ -30,17 +30,18 @@ pub struct Package { pub publisher: String, #[serde(rename = "@difficulty")] pub difficulty: u8, - #[serde(rename = "@language")] + #[serde(rename = "@language", skip_serializing_if = "Option::is_none")] pub language: Option, - #[serde(rename = "@logo")] + #[serde(rename = "@logo", skip_serializing_if = "Option::is_none")] pub logo: Option, - #[serde(rename = "@restriction")] + #[serde(rename = "@restriction", skip_serializing_if = "Option::is_none")] pub restriction: Option, // elements pub info: Info, #[serde(deserialize_with = "unwrap_list", serialize_with = "wrap_round_list")] pub rounds: Vec, + #[serde(skip_serializing_if = "Option::is_none")] pub tags: Option>, // resources @@ -307,9 +308,12 @@ impl PackageNode { #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)] pub struct Info { + #[serde(skip_serializing_if = "Option::is_none")] pub comments: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub extension: Option, pub authors: Authors, + #[serde(skip_serializing_if = "Option::is_none")] pub sources: Option>, } @@ -329,9 +333,9 @@ pub struct Rounds { pub struct Round { #[serde(rename = "@name")] pub name: String, - #[serde(rename = "@type")] + #[serde(rename = "@type", skip_serializing_if = "Option::is_none")] pub variant: Option, - #[serde(rename = "@info")] + #[serde(rename = "@info", skip_serializing_if = "Option::is_none")] pub info: Option, #[serde(deserialize_with = "unwrap_list", serialize_with = "wrap_theme_list")] pub themes: Vec, @@ -343,7 +347,7 @@ pub struct Theme { pub name: String, #[serde(deserialize_with = "unwrap_list", serialize_with = "wrap_question_list")] pub questions: Vec, - #[serde(rename = "@info")] + #[serde(rename = "@info", skip_serializing_if = "Option::is_none")] pub info: Option, } @@ -357,15 +361,15 @@ pub struct Questions { pub struct Question { #[serde(rename = "@price")] pub price: usize, + #[serde(rename = "type", skip_serializing_if = "Option::is_none")] + pub question_type: Option, #[serde(deserialize_with = "unwrap_list", serialize_with = "wrap_atom_list")] pub scenario: Vec, #[serde(deserialize_with = "unwrap_list", serialize_with = "wrap_answer_list")] pub right: Vec, - #[serde(deserialize_with = "unwrap_option_list", serialize_with = "wrap_option_answer_list", default)] + #[serde(deserialize_with = "unwrap_option_list", default, serialize_with = "wrap_option_answer_list", skip_serializing_if = "Option::is_none",)] pub wrong: Option>, - #[serde(rename = "type")] - pub question_type: Option, - #[serde(rename = "@info")] + #[serde(rename = "@info", skip_serializing_if = "Option::is_none")] pub info: Option, } @@ -373,13 +377,13 @@ pub struct Question { pub struct QuestionType { #[serde(rename = "@name")] pub name: String, - #[serde(rename = "param")] + #[serde(rename = "param", skip_serializing_if = "Option::is_none")] pub params: Option>, } #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)] pub struct Answer { - #[serde(rename = "$value")] + #[serde(rename = "$value", skip_serializing_if = "Option::is_none")] pub body: Option, } @@ -393,11 +397,11 @@ pub struct Param { #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)] pub struct Atom { - #[serde(rename = "@time")] + #[serde(rename = "@time", skip_serializing_if = "Option::is_none")] pub time: Option, - #[serde(rename = "@type")] + #[serde(rename = "@type", skip_serializing_if = "Option::is_none")] pub variant: Option, - #[serde(rename = "$value")] + #[serde(rename = "$value", skip_serializing_if = "Option::is_none")] pub body: Option, } diff --git a/crates/opensi-core/src/serde_utils.rs b/crates/opensi-core/src/serde_utils.rs index 82c1ea5..7257959 100644 --- a/crates/opensi-core/src/serde_utils.rs +++ b/crates/opensi-core/src/serde_utils.rs @@ -20,9 +20,8 @@ where D: Deserializer<'de>, T: Deserialize<'de> + Default, { - #[derive(Serialize, Deserialize)] + #[derive(Deserialize)] struct List { - // #[serde(default)] #[serde(rename = "$value")] element: Option>, }