Implement JsonSchema for chrono types
Requires chrono feature.
This commit is contained in:
parent
7285dde99a
commit
a236d7aee0
9 changed files with 178 additions and 28 deletions
18
schemars/tests/chrono.rs
Normal file
18
schemars/tests/chrono.rs
Normal file
|
@ -0,0 +1,18 @@
|
|||
mod util;
|
||||
use chrono::prelude::*;
|
||||
use schemars::JsonSchema;
|
||||
use util::*;
|
||||
|
||||
#[derive(Debug, JsonSchema)]
|
||||
struct ChronoTypes {
|
||||
weekday: Weekday,
|
||||
date_time: DateTime<Utc>,
|
||||
naive_date: NaiveDate,
|
||||
naive_date_time: NaiveDateTime,
|
||||
naive_time: NaiveTime,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chrono_types() -> TestResult {
|
||||
test_default_generated_schema::<ChronoTypes>("chrono-types")
|
||||
}
|
42
schemars/tests/expected/chrono-types.json
Normal file
42
schemars/tests/expected/chrono-types.json
Normal file
|
@ -0,0 +1,42 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "ChronoTypes",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"date_time": {
|
||||
"type": "string",
|
||||
"format": "date-time"
|
||||
},
|
||||
"naive_date": {
|
||||
"type": "string",
|
||||
"format": "date"
|
||||
},
|
||||
"naive_date_time": {
|
||||
"type": "string",
|
||||
"format": "partial-date-time"
|
||||
},
|
||||
"naive_time": {
|
||||
"type": "string",
|
||||
"format": "partial-date-time"
|
||||
},
|
||||
"weekday": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"Mon",
|
||||
"Tue",
|
||||
"Wed",
|
||||
"Thu",
|
||||
"Fri",
|
||||
"Sat",
|
||||
"Sun"
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"date_time",
|
||||
"naive_date",
|
||||
"naive_date_time",
|
||||
"naive_time",
|
||||
"weekday"
|
||||
]
|
||||
}
|
|
@ -88,6 +88,10 @@
|
|||
"items": {},
|
||||
"nullable": true
|
||||
},
|
||||
"format": {
|
||||
"type": "string",
|
||||
"nullable": true
|
||||
},
|
||||
"items": {
|
||||
"anyOf": [
|
||||
{
|
||||
|
|
|
@ -24,6 +24,17 @@
|
|||
"integer"
|
||||
]
|
||||
},
|
||||
"Ref": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"$ref": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"$ref"
|
||||
]
|
||||
},
|
||||
"Schema": {
|
||||
"anyOf": [
|
||||
{
|
||||
|
@ -113,6 +124,16 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
"format": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"items": {
|
||||
"anyOf": [
|
||||
{
|
||||
|
@ -181,17 +202,6 @@
|
|||
},
|
||||
"additionalProperties": true
|
||||
},
|
||||
"Ref": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"$ref": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"$ref"
|
||||
]
|
||||
},
|
||||
"SingleOrVec_For_InstanceType": {
|
||||
"anyOf": [
|
||||
{
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use pretty_assertions::assert_eq;
|
||||
use schemars::{gen::SchemaSettings, schema_for, JsonSchema};
|
||||
use schemars::{gen::SchemaSettings, schema::Schema, schema_for, JsonSchema};
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::panic;
|
||||
|
@ -8,32 +8,36 @@ pub type TestResult = Result<(), Box<dyn Error>>;
|
|||
|
||||
#[allow(dead_code)] // https://github.com/rust-lang/rust/issues/46379
|
||||
pub fn test_generated_schema<T: JsonSchema>(file: &str, settings: SchemaSettings) -> TestResult {
|
||||
let expected_json = fs::read_to_string(format!("tests/expected/{}.json", file))?;
|
||||
let expected = serde_json::from_str(&expected_json)?;
|
||||
|
||||
let actual = settings.into_generator().into_root_schema_for::<T>()?;
|
||||
|
||||
if actual != expected {
|
||||
let actual_json = serde_json::to_string_pretty(&actual)?;
|
||||
fs::write(format!("tests/actual/{}.json", file), actual_json)?;
|
||||
}
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
Ok(())
|
||||
test_schema(&actual, file)
|
||||
}
|
||||
|
||||
#[allow(dead_code)] // https://github.com/rust-lang/rust/issues/46379
|
||||
pub fn test_default_generated_schema<T: JsonSchema>(file: &str) -> TestResult {
|
||||
let expected_json = fs::read_to_string(format!("tests/expected/{}.json", file))?;
|
||||
let expected = serde_json::from_str(&expected_json)?;
|
||||
|
||||
let actual = schema_for!(T)?;
|
||||
test_schema(&actual, file)
|
||||
}
|
||||
|
||||
fn test_schema(actual: &Schema, file: &str) -> TestResult {
|
||||
let expected_json = match fs::read_to_string(format!("tests/expected/{}.json", file)) {
|
||||
Ok(j) => j,
|
||||
Err(e) => {
|
||||
write_actual_to_file(&actual, file)?;
|
||||
return Err(Box::from(e));
|
||||
}
|
||||
};
|
||||
let expected = &serde_json::from_str(&expected_json)?;
|
||||
|
||||
if actual != expected {
|
||||
let actual_json = serde_json::to_string_pretty(&actual)?;
|
||||
fs::write(format!("tests/actual/{}.json", file), actual_json)?;
|
||||
write_actual_to_file(actual, file)?;
|
||||
}
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_actual_to_file(schema: &Schema, file: &str) -> TestResult {
|
||||
let actual_json = serde_json::to_string_pretty(&schema)?;
|
||||
fs::write(format!("tests/actual/{}.json", file), actual_json)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue