schemars icon indicating copy to clipboard operation
schemars copied to clipboard

Allow specifying custom type bounds for derived `JsonSchema` instances

Open nightkr opened this issue 3 years ago • 2 comments

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.

nightkr avatar Jul 13 '22 15:07 nightkr

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).

nightkr avatar Jul 13 '22 15:07 nightkr

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.

nightkr avatar Jul 14 '22 11:07 nightkr

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.

GREsau avatar Aug 14 '22 13:08 GREsau