Skip to content

Commit

Permalink
Implement JsonSchema for Bound
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed Oct 30, 2019
1 parent 3f56d6b commit 5503f06
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
34 changes: 33 additions & 1 deletion schemars/src/json_schema_impls/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use serde_json::json;
use std::ops::{Range, RangeInclusive};
use std::ops::{Bound, Range, RangeInclusive};

impl<T: JsonSchema> JsonSchema for Option<T> {
no_ref_schema!();
Expand Down Expand Up @@ -100,6 +100,38 @@ impl<T: JsonSchema, E: JsonSchema> JsonSchema for Result<T, E> {
}
}

impl<T: JsonSchema> JsonSchema for Bound<T> {
fn schema_name() -> String {
format!("Bound_of_{}", T::schema_name())
}

fn json_schema(gen: &mut SchemaGenerator) -> Schema {
let mut included_schema = SchemaObject::default();
included_schema.instance_type = Some(InstanceType::Object.into());
included_schema.object().required.insert("Included".to_owned());
included_schema
.object()
.properties
.insert("Included".to_owned(), gen.subschema_for::<T>());

let mut excluded_schema = SchemaObject::default();
excluded_schema.instance_type = Some(InstanceType::Object.into());
excluded_schema.object().required.insert("Excluded".to_owned());
excluded_schema
.object()
.properties
.insert("Excluded".to_owned(), gen.subschema_for::<T>());

let mut unbounded_schema = SchemaObject::default();
unbounded_schema.instance_type = Some(InstanceType::String.into());
unbounded_schema.const_value = Some(json!("Unbounded"));

let mut schema = SchemaObject::default();
schema.subschemas().one_of = Some(vec![included_schema.into(), excluded_schema.into(), unbounded_schema.into()]);
schema.into()
}
}

impl<T: JsonSchema> JsonSchema for Range<T> {
fn schema_name() -> String {
format!("Range_of_{}", T::schema_name())
Expand Down
34 changes: 34 additions & 0 deletions schemars/tests/expected/range.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
"title": "MyStruct",
"type": "object",
"required": [
"bound",
"inclusive",
"range"
],
"properties": {
"bound": {
"$ref": "#/definitions/Bound_of_String"
},
"inclusive": {
"$ref": "#/definitions/Range_of_double"
},
Expand All @@ -15,6 +19,36 @@
}
},
"definitions": {
"Bound_of_String": {
"oneOf": [
{
"type": "object",
"required": [
"Included"
],
"properties": {
"Included": {
"type": "string"
}
}
},
{
"type": "object",
"required": [
"Excluded"
],
"properties": {
"Excluded": {
"type": "string"
}
}
},
{
"type": "string",
"const": "Unbounded"
}
]
},
"Range_of_double": {
"type": "object",
"required": [
Expand Down
3 changes: 2 additions & 1 deletion schemars/tests/range.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
mod util;
use schemars::JsonSchema;
use std::ops::{Bound, Range, RangeInclusive};
use util::*;
use std::ops::{Range, RangeInclusive};

#[derive(Debug, JsonSchema)]
struct MyStruct {
range: Range<usize>,
inclusive: RangeInclusive<f64>,
bound: Bound<String>
}

#[test]
Expand Down

0 comments on commit 5503f06

Please sign in to comment.