From: Nutomic Date: Mon, 3 Jul 2023 13:14:01 +0000 (+0200) Subject: Use serde(skip) instead of skip_serializing, add placeholder values (#3362) X-Git-Url: http://these/git/%22https:/nerdica.net/photo/contact/80/%7BdocsUrl%7D?a=commitdiff_plain;h=cb91eedd24a0a5b9c8275309461c886ee9a7b3ef;p=lemmy.git Use serde(skip) instead of skip_serializing, add placeholder values (#3362) * Use serde(skip) instead of skip_serializing The latter breaks lemmy_crawler as the field is not included in the Lemmy API, but is required when attempting to parse API responses. Should only use serde(skip) to avoid this problem * use option * add placeholders * no unwrap --- diff --git a/crates/db_schema/src/source/community.rs b/crates/db_schema/src/source/community.rs index c24a6459..64b30b2a 100644 --- a/crates/db_schema/src/source/community.rs +++ b/crates/db_schema/src/source/community.rs @@ -1,6 +1,9 @@ -use crate::newtypes::{CommunityId, DbUrl, InstanceId, PersonId}; #[cfg(feature = "full")] use crate::schema::{community, community_follower, community_moderator, community_person_ban}; +use crate::{ + newtypes::{CommunityId, DbUrl, InstanceId, PersonId}, + source::placeholder_apub_url, +}; use serde::{Deserialize, Serialize}; use serde_with::skip_serializing_none; #[cfg(feature = "full")] @@ -42,9 +45,9 @@ pub struct Community { pub icon: Option, /// A URL for a banner. pub banner: Option, - #[serde(skip_serializing)] + #[serde(skip, default = "placeholder_apub_url")] pub followers_url: DbUrl, - #[serde(skip_serializing)] + #[serde(skip, default = "placeholder_apub_url")] pub inbox_url: DbUrl, #[serde(skip)] pub shared_inbox_url: Option, diff --git a/crates/db_schema/src/source/mod.rs b/crates/db_schema/src/source/mod.rs index 926e23e7..a46f4fb4 100644 --- a/crates/db_schema/src/source/mod.rs +++ b/crates/db_schema/src/source/mod.rs @@ -1,3 +1,6 @@ +use crate::newtypes::DbUrl; +use url::Url; + #[cfg(feature = "full")] pub mod activity; pub mod actor_language; @@ -30,3 +33,13 @@ pub mod registration_application; pub mod secret; pub mod site; pub mod tagline; + +/// Default value for columns like [community::Community.inbox_url] which are marked as serde(skip). +/// +/// This is necessary so they can be successfully deserialized from API responses, even though the +/// value is not sent by Lemmy. Necessary for crates which rely on Rust API such as lemmy-stats-crawler. +fn placeholder_apub_url() -> DbUrl { + DbUrl(Box::new( + Url::parse("http://example.com").expect("parse placeholer url"), + )) +} diff --git a/crates/db_schema/src/source/person.rs b/crates/db_schema/src/source/person.rs index 1500e898..82d6f61a 100644 --- a/crates/db_schema/src/source/person.rs +++ b/crates/db_schema/src/source/person.rs @@ -1,6 +1,9 @@ -use crate::newtypes::{DbUrl, InstanceId, PersonId}; #[cfg(feature = "full")] use crate::schema::{person, person_follower}; +use crate::{ + newtypes::{DbUrl, InstanceId, PersonId}, + source::placeholder_apub_url, +}; use serde::{Deserialize, Serialize}; use serde_with::skip_serializing_none; #[cfg(feature = "full")] @@ -40,7 +43,7 @@ pub struct Person { pub banner: Option, /// Whether the person is deleted. pub deleted: bool, - #[serde(skip_serializing)] + #[serde(skip, default = "placeholder_apub_url")] pub inbox_url: DbUrl, #[serde(skip)] pub shared_inbox_url: Option,