Skip to content

Commit b0ee3ab

Browse files
committed
Make serde_with schemars support extensible
Implementing JsonSchema directly on serde_with::Schema<T, TA> works fine when within serde_with. Once you are outside the crate, however, the orphan rules prevent you from adding impls of the form impl JsonSchema for Schema<T, MyCustomType> { ... } We would like users to be able to make that impl (or something equivalent) so we need to find a way to make it work. We make this work by adding a new layer of trait indirection. This commit introduces an new JsonSchemaAs<T> trait, adds a blanket impl<T, TA> JsonSchema for Schema<T, TA> where TA: JsonSchemaAs<T> and then changes all the existing impls to be for JsonSchemaAs<T>. Now, when a user wants to add json schema support for their own type they can implement JsonSchemaAs<T> for their custom type and the orphan rules will allow it. As a bonus, this seems to have made the extraordinarily confusing "reached recursion limit" errors that I have been encountering disappear. Which is nice.
1 parent 170b6e1 commit b0ee3ab

File tree

2 files changed

+196
-60
lines changed

2 files changed

+196
-60
lines changed

serde_with/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ pub mod json;
331331
mod key_value_map;
332332
pub mod rust;
333333
#[cfg(feature = "schemars_0_8")]
334-
mod schemars_0_8;
334+
pub mod schemars_0_8;
335335
pub mod ser;
336336
#[cfg(feature = "std")]
337337
mod serde_conv;

0 commit comments

Comments
 (0)