Fix deserialization expansion with serde fork

This commit is contained in:
emilis 2022-11-07 23:25:04 +00:00
parent 94af6929ab
commit 271eba8d7b
4 changed files with 51 additions and 35 deletions

23
Cargo.lock generated
View File

@ -53,9 +53,9 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]] [[package]]
name = "axum" name = "axum"
version = "0.5.16" version = "0.5.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9e3356844c4d6a6d6467b8da2cffb4a2820be256f50a3a386c9d152bab31043" checksum = "acee9fd5073ab6b045a275b3e709c163dd36c90685219cb21804a147b58dba43"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"axum-core", "axum-core",
@ -84,9 +84,9 @@ dependencies = [
[[package]] [[package]]
name = "axum-core" name = "axum-core"
version = "0.2.8" version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9f0c0a60006f2a293d82d571f635042a72edf927539b7685bd62d361963839b" checksum = "37e5939e02c56fecd5c017c37df4238c0a839fa76b7f97acdd7efb804fd181cc"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"bytes", "bytes",
@ -1277,18 +1277,16 @@ dependencies = [
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.144" version = "1.0.147"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://sectorinf.com/emilis/serde#adabf4f770d7d0e2f5554720b27c2d73a168f9cc"
checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.144" version = "1.0.147"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://sectorinf.com/emilis/serde#adabf4f770d7d0e2f5554720b27c2d73a168f9cc"
checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1297,9 +1295,8 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.85" version = "1.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://sectorinf.com/emilis/serde_json#18a1411b84b99082f0390775dd6607474aa7dd10"
checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44"
dependencies = [ dependencies = [
"itoa", "itoa",
"ryu", "ryu",

View File

@ -9,7 +9,7 @@ edition = "2021"
anyhow = "1.0.64" anyhow = "1.0.64"
argon2 = "0.4.1" argon2 = "0.4.1"
async-trait = "0.1.58" async-trait = "0.1.58"
axum = "0.5.16" axum = "0.5.17"
base-62 = "0.1.1" base-62 = "0.1.1"
handlebars = "4.3.3" handlebars = "4.3.3"
jsonwebtoken = "8.1.1" jsonwebtoken = "8.1.1"
@ -31,3 +31,7 @@ serde_json = "1.0.85"
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
tokio-postgres = { version = "0.7.7", features = ["with-serde_json-1"] } tokio-postgres = { version = "0.7.7", features = ["with-serde_json-1"] }
tower-cookies = "0.7.0" tower-cookies = "0.7.0"
[patch.crates-io]
serde = { git = "https://sectorinf.com/emilis/serde" }
serde_json = { git = "https://sectorinf.com/emilis/serde_json" }

View File

@ -1,7 +1,12 @@
use async_trait::async_trait; use async_trait::async_trait;
use serde::{Deserialize, Serialize}; 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 context;
pub mod note; pub mod note;
@ -46,6 +51,23 @@ pub struct ActivityLD {
pub object: Option<ObjectLD>, pub object: Option<ObjectLD>,
} }
#[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<String>,
// pub context: Option<String>,
#[serde(deserialize_with = "expand")]
pub to: Context,
pub url: Option<String>,
}
#[derive(Clone, Serialize, Deserialize, Debug)] #[derive(Clone, Serialize, Deserialize, Debug)]
pub struct ObjectLD { pub struct ObjectLD {
#[serde(rename = "@context")] #[serde(rename = "@context")]
@ -67,7 +89,7 @@ pub struct ObjectLD {
pub enum ActorType {} pub enum ActorType {}
#[derive(Debug, Clone)] #[derive(Debug, Clone, Deserialize)]
pub struct Actor { pub struct Actor {
id: String, id: String,
} }
@ -75,11 +97,11 @@ pub struct Actor {
pub async fn test() { pub async fn test() {
let obj = r#"{ let obj = r#"{
"@context": "https://www.w3.org/ns/activitystreams", "@context": "https://www.w3.org/ns/activitystreams",
"attributedTo": "https://localhost/u/test", "attributedTo": "http://localhost:3001/u/test",
"content": "honk donk", "content": "honk donk",
"context": "data:,electrichonkytonk-2jqQ42HyJXctnBKTy1", "context": "data:,electrichonkytonk-2jqQ42HyJXctnBKTy1",
"conversation": "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", "published": "2022-09-30T19:04:45Z",
"summary": "", "summary": "",
"to": "https://www.w3.org/ns/activitystreams#Public", "to": "https://www.w3.org/ns/activitystreams#Public",
@ -87,9 +109,8 @@ pub async fn test() {
"url": "https://localhost/u/test/h/6Q8BFF8W6PZT2ddngZ" "url": "https://localhost/u/test/h/6Q8BFF8W6PZT2ddngZ"
}"#; }"#;
let obj = serde_json::from_str::<ObjectLD>(obj).unwrap(); let obj = serde_json::from_str::<Activity>(obj).unwrap();
let unwrapped = Note::expand(&obj).await.unwrap(); println!("to: {:#?}", &obj.to);
println!("{:#?}", &unwrapped); println!();
println!("to: {:#?}", obj.to_uris); println!("{:#?}", obj);
println!("{}", serde_json::to_string_pretty(&obj).unwrap());
} }

View File

@ -2,8 +2,6 @@ use super::resolve::Resolver;
use std::{fmt, marker::PhantomData}; use std::{fmt, marker::PhantomData};
use serde::{de::Visitor, Deserialize, Deserializer}; use serde::{de::Visitor, Deserialize, Deserializer};
static TYPE_STR: &str = std::any::type_name::<&str>();
static TYPE_STRING: &str = std::any::type_name::<String>();
// Allows a value that's a string to be expanded into an object AND the serialization of that object itself // 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 // TODO: deserializing the actual object isnt supported atm LOL
@ -54,15 +52,11 @@ where
} }
} }
let type_name = std::any::type_name::<Out>(); let resolve_visitor = ResolveVisitor {
if type_name == TYPE_STR || type_name == TYPE_STRING {
deserializer.deserialize_any(ResolveVisitor {
resolver: resolver, resolver: resolver,
_type: PhantomData, _type: PhantomData,
}) };
} else { deserializer.deserialize_map_string(resolve_visitor, |des| Out::deserialize(des))
Out::deserialize(deserializer)
}
} }
// Allows deserialization of a single item into a vector of that item // Allows deserialization of a single item into a vector of that item
@ -143,6 +137,6 @@ mod tests {
serde_json::from_str::<Expandable>(JSONLD_INPUT).expect("deserializing with expand"); serde_json::from_str::<Expandable>(JSONLD_INPUT).expect("deserializing with expand");
assert!(result.expansive.id == "1"); assert!(result.expansive.id == "1");
assert!(result.expansive.truth) assert!(result.expansive.truth);
} }
} }