Implement JsonSchema for std::time types
This commit is contained in:
parent
155190c9ab
commit
e11d5e5a98
4 changed files with 95 additions and 0 deletions
|
@ -15,4 +15,5 @@ mod maps;
|
|||
mod primitives;
|
||||
mod sequences;
|
||||
mod serdejson;
|
||||
mod time;
|
||||
mod tuple;
|
||||
|
|
34
schemars/src/json_schema_impls/time.rs
Normal file
34
schemars/src/json_schema_impls/time.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
use crate::gen::SchemaGenerator;
|
||||
use crate::schema::*;
|
||||
use crate::JsonSchema;
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
impl JsonSchema for Duration {
|
||||
fn schema_name() -> String {
|
||||
"Duration".to_owned()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
let mut schema = SchemaObject::default();
|
||||
schema.instance_type = Some(InstanceType::Object.into());
|
||||
let properties = &mut schema.object().properties;
|
||||
properties.insert("secs".to_owned(), <u64>::json_schema(gen));
|
||||
properties.insert("nanos".to_owned(), <u32>::json_schema(gen));
|
||||
schema.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl JsonSchema for SystemTime {
|
||||
fn schema_name() -> String {
|
||||
"SystemTime".to_owned()
|
||||
}
|
||||
|
||||
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||
let mut schema = SchemaObject::default();
|
||||
schema.instance_type = Some(InstanceType::Object.into());
|
||||
let properties = &mut schema.object().properties;
|
||||
properties.insert("secs_since_epoch".to_owned(), <u64>::json_schema(gen));
|
||||
properties.insert("nanos_since_epoch".to_owned(), <u32>::json_schema(gen));
|
||||
schema.into()
|
||||
}
|
||||
}
|
45
schemars/tests/expected/duration_and_systemtime.json
Normal file
45
schemars/tests/expected/duration_and_systemtime.json
Normal file
|
@ -0,0 +1,45 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "MyStruct",
|
||||
"type": "object",
|
||||
"required": [
|
||||
"duration",
|
||||
"time"
|
||||
],
|
||||
"properties": {
|
||||
"duration": {
|
||||
"$ref": "#/definitions/Duration"
|
||||
},
|
||||
"time": {
|
||||
"$ref": "#/definitions/SystemTime"
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
"Duration": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"nanos": {
|
||||
"type": "integer",
|
||||
"format": "uint32"
|
||||
},
|
||||
"secs": {
|
||||
"type": "integer",
|
||||
"format": "uint64"
|
||||
}
|
||||
}
|
||||
},
|
||||
"SystemTime": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"nanos_since_epoch": {
|
||||
"type": "integer",
|
||||
"format": "uint32"
|
||||
},
|
||||
"secs_since_epoch": {
|
||||
"type": "integer",
|
||||
"format": "uint64"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
15
schemars/tests/time.rs
Normal file
15
schemars/tests/time.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
mod util;
|
||||
use schemars::JsonSchema;
|
||||
use util::*;
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
#[derive(Debug, JsonSchema)]
|
||||
struct MyStruct {
|
||||
duration: Duration,
|
||||
time: SystemTime,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duration_and_systemtime() -> TestResult {
|
||||
test_default_generated_schema::<MyStruct>("duration_and_systemtime")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue