From 271eba8d7b6e65785909bbef965354ae78825105 Mon Sep 17 00:00:00 2001 From: emilis Date: Mon, 7 Nov 2022 23:25:04 +0000 Subject: [PATCH] Fix deserialization expansion with serde fork --- Cargo.lock | 23 ++++++++++------------- Cargo.toml | 6 +++++- src/astreams/mod.rs | 39 ++++++++++++++++++++++++++++++--------- src/astreams/serde_ext.rs | 18 ++++++------------ 4 files changed, 51 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 65c7944..e8ec067 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -53,9 +53,9 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "axum" -version = "0.5.16" +version = "0.5.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9e3356844c4d6a6d6467b8da2cffb4a2820be256f50a3a386c9d152bab31043" +checksum = "acee9fd5073ab6b045a275b3e709c163dd36c90685219cb21804a147b58dba43" dependencies = [ "async-trait", "axum-core", @@ -84,9 +84,9 @@ dependencies = [ [[package]] name = "axum-core" -version = "0.2.8" +version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9f0c0a60006f2a293d82d571f635042a72edf927539b7685bd62d361963839b" +checksum = "37e5939e02c56fecd5c017c37df4238c0a839fa76b7f97acdd7efb804fd181cc" dependencies = [ "async-trait", "bytes", @@ -1277,18 +1277,16 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.144" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860" +version = "1.0.147" +source = "git+https://sectorinf.com/emilis/serde#adabf4f770d7d0e2f5554720b27c2d73a168f9cc" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.144" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00" +version = "1.0.147" +source = "git+https://sectorinf.com/emilis/serde#adabf4f770d7d0e2f5554720b27c2d73a168f9cc" dependencies = [ "proc-macro2", "quote", @@ -1297,9 +1295,8 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.85" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44" +version = "1.0.87" +source = "git+https://sectorinf.com/emilis/serde_json#18a1411b84b99082f0390775dd6607474aa7dd10" dependencies = [ "itoa", "ryu", diff --git a/Cargo.toml b/Cargo.toml index 4818e56..ac939f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" anyhow = "1.0.64" argon2 = "0.4.1" async-trait = "0.1.58" -axum = "0.5.16" +axum = "0.5.17" base-62 = "0.1.1" handlebars = "4.3.3" jsonwebtoken = "8.1.1" @@ -31,3 +31,7 @@ serde_json = "1.0.85" tokio = { version = "1", features = ["full"] } tokio-postgres = { version = "0.7.7", features = ["with-serde_json-1"] } tower-cookies = "0.7.0" + +[patch.crates-io] +serde = { git = "https://sectorinf.com/emilis/serde" } +serde_json = { git = "https://sectorinf.com/emilis/serde_json" } diff --git a/src/astreams/mod.rs b/src/astreams/mod.rs index 3e2ef10..96e408e 100644 --- a/src/astreams/mod.rs +++ b/src/astreams/mod.rs @@ -1,7 +1,12 @@ use async_trait::async_trait; use serde::{Deserialize, Serialize}; -use self::{context::Context, note::Note, resolve::ResolveError, serde_ext::pull_single_into_vec}; +use self::{ + context::Context, + note::Note, + resolve::ResolveError, + serde_ext::{expand, pull_single_into_vec}, +}; pub mod context; pub mod note; @@ -46,6 +51,23 @@ pub struct ActivityLD { pub object: Option, } +#[derive(Debug, Clone, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Activity { + #[serde(rename = "@context")] + #[serde(deserialize_with = "expand")] + pub ap_context: Context, + pub id: String, + #[serde(rename = "type")] + pub kind: ActivityKind, + pub attributed_to: Actor, + pub content: Option, + // pub context: Option, + #[serde(deserialize_with = "expand")] + pub to: Context, + pub url: Option, +} + #[derive(Clone, Serialize, Deserialize, Debug)] pub struct ObjectLD { #[serde(rename = "@context")] @@ -67,7 +89,7 @@ pub struct ObjectLD { pub enum ActorType {} -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Deserialize)] pub struct Actor { id: String, } @@ -75,11 +97,11 @@ pub struct Actor { pub async fn test() { let obj = r#"{ "@context": "https://www.w3.org/ns/activitystreams", - "attributedTo": "https://localhost/u/test", + "attributedTo": "http://localhost:3001/u/test", "content": "honk donk", "context": "data:,electrichonkytonk-2jqQ42HyJXctnBKTy1", "conversation": "data:,electrichonkytonk-2jqQ42HyJXctnBKTy1", - "id": "https://localhost/u/test/h/6Q8BFF8W6PZT2ddngZ", + "id": "htts://localhost:3001/u/test/h/6Q8BFF8W6PZT2ddngZ", "published": "2022-09-30T19:04:45Z", "summary": "", "to": "https://www.w3.org/ns/activitystreams#Public", @@ -87,9 +109,8 @@ pub async fn test() { "url": "https://localhost/u/test/h/6Q8BFF8W6PZT2ddngZ" }"#; - let obj = serde_json::from_str::(obj).unwrap(); - let unwrapped = Note::expand(&obj).await.unwrap(); - println!("{:#?}", &unwrapped); - println!("to: {:#?}", obj.to_uris); - println!("{}", serde_json::to_string_pretty(&obj).unwrap()); + let obj = serde_json::from_str::(obj).unwrap(); + println!("to: {:#?}", &obj.to); + println!(); + println!("{:#?}", obj); } diff --git a/src/astreams/serde_ext.rs b/src/astreams/serde_ext.rs index a77c715..9fc263f 100644 --- a/src/astreams/serde_ext.rs +++ b/src/astreams/serde_ext.rs @@ -2,8 +2,6 @@ use super::resolve::Resolver; use std::{fmt, marker::PhantomData}; use serde::{de::Visitor, Deserialize, Deserializer}; -static TYPE_STR: &str = std::any::type_name::<&str>(); -static TYPE_STRING: &str = std::any::type_name::(); // Allows a value that's a string to be expanded into an object AND the serialization of that object itself // TODO: deserializing the actual object isnt supported atm LOL @@ -54,15 +52,11 @@ where } } - let type_name = std::any::type_name::(); - if type_name == TYPE_STR || type_name == TYPE_STRING { - deserializer.deserialize_any(ResolveVisitor { - resolver: resolver, - _type: PhantomData, - }) - } else { - Out::deserialize(deserializer) - } + let resolve_visitor = ResolveVisitor { + resolver: resolver, + _type: PhantomData, + }; + deserializer.deserialize_map_string(resolve_visitor, |des| Out::deserialize(des)) } // Allows deserialization of a single item into a vector of that item @@ -143,6 +137,6 @@ mod tests { serde_json::from_str::(JSONLD_INPUT).expect("deserializing with expand"); assert!(result.expansive.id == "1"); - assert!(result.expansive.truth) + assert!(result.expansive.truth); } }