| 1 | use crate::prelude::*; |
| 2 | use std::marker::PhantomData; |
| 3 | |
| 4 | #[derive(Default)] |
| 5 | struct MyIterator; |
| 6 | |
| 7 | impl Iterator for MyIterator { |
| 8 | type Item = String; |
| 9 | |
| 10 | fn next(&mut self) -> Option<Self::Item> { |
| 11 | unimplemented!() |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | // The default trait bounds would require T/U to implement JsonSchema, which MyIterator does not. |
| 16 | #[derive(JsonSchema, Serialize, Deserialize, Default)] |
| 17 | #[schemars(bound = "T::Item: JsonSchema")] |
| 18 | pub struct MyContainer<T, U> |
| 19 | where |
| 20 | T: Iterator, |
| 21 | U: Iterator, |
| 22 | { |
| 23 | pub associated: T::Item, |
| 24 | #[schemars(bound = "U::Item: JsonSchema")] |
| 25 | pub associated2: U::Item, |
| 26 | pub phantom: PhantomData<T>, |
| 27 | #[serde(skip)] |
| 28 | pub _skipped: T, |
| 29 | } |
| 30 | |
| 31 | #[test] |
| 32 | fn manual_bound_set() { |
| 33 | test!(MyContainer<MyIterator, MyIterator>) |
| 34 | // TODO with better bounds, this assertion would work: |
| 35 | // .assert_identical::<MyContainer<core::slice::Iter<&str>>>() |
| 36 | .assert_snapshot() |
| 37 | .assert_allows_ser_roundtrip_default() |
| 38 | .assert_allows_ser_roundtrip([MyContainer { |
| 39 | associated: "test".to_owned(), |
| 40 | associated2: "test".to_owned(), |
| 41 | phantom: PhantomData, |
| 42 | _skipped: MyIterator, |
| 43 | }]) |
| 44 | .assert_matches_de_roundtrip(arbitrary_values()); |
| 45 | |
| 46 | assert_ne!( |
| 47 | <MyContainer<MyIterator, MyIterator>>::schema_id(), |
| 48 | <MyContainer<core::slice::Iter<i32>, MyIterator>>::schema_id() |
| 49 | ); |
| 50 | assert_ne!( |
| 51 | <MyContainer<MyIterator, MyIterator>>::schema_id(), |
| 52 | <MyContainer<MyIterator, core::slice::Iter<i32>>>::schema_id() |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | // `T` doesn't need to impl `JsonSchema`, but `U` does |
| 57 | #[derive(JsonSchema, Serialize, Deserialize, Default)] |
| 58 | pub struct MyContainer2<T, U> |
| 59 | where |
| 60 | T: Iterator, |
| 61 | { |
| 62 | pub u: Option<U>, |
| 63 | pub phantom: PhantomData<T>, |
| 64 | #[serde(skip)] |
| 65 | pub _skipped: T, |
| 66 | } |
| 67 | |
| 68 | #[test] |
| 69 | fn auto_bound() { |
| 70 | test!(MyContainer2<MyIterator, String>) |
| 71 | .assert_identical::<MyContainer2<core::slice::Iter<&str>, Box<str>>>() |
| 72 | .assert_snapshot() |
| 73 | .assert_allows_ser_roundtrip_default() |
| 74 | .assert_allows_ser_roundtrip([MyContainer2 { |
| 75 | u: Some("test".to_owned()), |
| 76 | phantom: PhantomData, |
| 77 | _skipped: MyIterator, |
| 78 | }]) |
| 79 | .assert_matches_de_roundtrip(arbitrary_values()); |
| 80 | } |