schemars
schemars copied to clipboard
Compiler error when using serde_with::with_prefix!
Using schemars ver. 0.8.12, I noticed a compiler error
error[E0573]: expected type, found module `prefix_dest`
--> src/models.rs:308:29
|
308 | #[serde(flatten, with = "prefix_dest")]
| ^^^^^^^^^^^^^ not a type
when using the with_prefix!
macro from the serde_width
crate ver. 3.0.0 (serde
= 1.0.164 and serde_json
= 1.0.96)
with_prefix!(prefix_dest "Dest");
// ...
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone, Default, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct DialStateData
{
#[serde(flatten)]
pub caller_state: NewConnectedLineData,
#[serde(flatten, with = "prefix_dest")]
pub destination_state: NewConnectedLineData,
pub dial_status: AsteriskDialStatus,
}
the compiler error disappears when omitting JsonSchema
.
I suspect the 2 custom serializers of serde_with
and schemars
are clashing.
Additional info:
// Recursive expansion of with_prefix! macro
// ==========================================
pub mod prefix_dest {
use $crate::serde::{Deserialize, Deserializer, Serialize, Serializer};
use $crate::with_prefix::WithPrefix;
#[allow(dead_code)]
pub fn serialize<T, S>(
object: &T,
serializer: S,
) -> $crate::__private__::Result<S::Ok, S::Error>
where
T: Serialize,
S: Serializer,
{
object.serialize(WithPrefix {
delegate: serializer,
prefix: "Dest",
})
}
#[allow(dead_code)]
pub fn deserialize<'de, T, D>(deserializer: D) -> $crate::__private__::Result<T, D::Error>
where
T: Deserialize<'de>,
D: Deserializer<'de>,
{
T::deserialize(WithPrefix {
delegate: deserializer,
prefix: "Dest",
})
}
}
Not sure what issue is but this might be related as there are some comments https://github.com/GREsau/schemars/blob/master/schemars_derive/src/attr/schemars_to_serde.rs#L25 And also some special logic here: https://github.com/GREsau/schemars/blob/master/schemars_derive/src/attr/mod.rs#L112 You can also use tools like https://github.com/dtolnay/cargo-expand to investigate what code it generates after running the macro but before the compiler checks.
Here's the expanded code but i don't understand it well enough to spot any errors: https://gist.github.com/fraxinas/5b4e5d2a58b3b42cb6db5fc47830c6cc