Allow specifying custom type bounds for derived `JsonSchema` instances
The automatic type bounds break down when using associated types. For example, for the following code:
trait MyTrait {
type Associated;
}
enum MyType {}
impl MyTrait for MyType {
type Associated = String;
}
#[derive(JsonSchema)]
struct MyContainer<T> where T: MyTrait {
field: T::Associated,
}
schemars will currently generate:
impl<T> JsonSchema for MyContainer<T> where T: MyTrait + JsonSchema {
// [snip]
}
where the correct bound would be:
impl<T> JsonSchema for MyContainer<T> where T: MyTrait, T::Associated: JsonSchema {
// [snip]
}
This PR adds an attribute #[schemars(bound = "T::Associated: JsonSchema")] that we can use to provide the correct bounds.
We might also want to explicitly filter out #[serde(bound)] clauses, to avoid accidentally adding bounds for existing users (which would be a breaking change).
Everything should be resolved now (apart from ser_bound() which I'm still not sure about how to fix...).
Feel free to skip d7e56b5 if you'd like to keep that as-is. I have tested against 1.37 (the current MSRV, as far as I can tell?) that everything still builds.
Thanks @teozkr, I've created a new change based on this one in #167. I made it use the #[serde(bound = ...)] value if it's not overridden by a #[schemars(bound = ...)], mainly just to simplify the implementation - I hope that's not too much of a problem.