Add #[schemars(bound = ...)] attribute

Based on https://github.com/GREsau/schemars/pull/162

Co-authored-by: teozkr <teo@nullable.se>
This commit is contained in:
Graham Esau 2022-08-14 13:58:33 +01:00 committed by Graham Esau
parent 6ada120cd3
commit 104dccca50
7 changed files with 76 additions and 14 deletions

29
schemars/tests/bound.rs Normal file
View file

@ -0,0 +1,29 @@
mod util;
use std::marker::PhantomData;
use schemars::JsonSchema;
use util::*;
struct MyIterator;
impl Iterator for MyIterator {
type Item = String;
fn next(&mut self) -> Option<Self::Item> {
unimplemented!()
}
}
// The default trait bounds would require T to implement JsonSchema,
// which MyIterator does not.
#[derive(JsonSchema)]
#[schemars(bound = "T::Item: JsonSchema", rename = "MyContainer")]
pub struct MyContainer<T> where T: Iterator {
pub associated: T::Item,
pub generic: PhantomData<T>,
}
#[test]
fn manual_bound_set() -> TestResult {
test_default_generated_schema::<MyContainer<MyIterator>>("bound")
}

View file

@ -0,0 +1,17 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MyContainer",
"type": "object",
"required": [
"associated",
"generic"
],
"properties": {
"associated": {
"type": "string"
},
"generic": {
"type": "null"
}
}
}