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
This commit is contained in:
Nutomic 2023-07-03 15:14:01 +02:00 committed by GitHub
parent 6405761891
commit cb91eedd24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 5 deletions

View file

@ -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<DbUrl>,
/// A URL for a banner.
pub banner: Option<DbUrl>,
#[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<DbUrl>,

View file

@ -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"),
))
}

View file

@ -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<DbUrl>,
/// 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<DbUrl>,