Add test for defaults in derived schemas
Currently ignored as defaults are not yet implemented.
This commit is contained in:
parent
1e9f36122d
commit
81eb53b590
2 changed files with 80 additions and 0 deletions
37
schemars/tests/default.rs
Normal file
37
schemars/tests/default.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
mod util;
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use util::*;
|
||||
|
||||
fn ten_and_true() -> MyStruct2 {
|
||||
MyStruct2 {
|
||||
my_int: 10,
|
||||
my_bool: true,
|
||||
}
|
||||
}
|
||||
|
||||
fn six() -> i32 {
|
||||
6
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize, Serialize, JsonSchema, Debug)]
|
||||
#[serde(default)]
|
||||
pub struct MyStruct {
|
||||
pub my_int: i32,
|
||||
pub my_bool: bool,
|
||||
pub my_struct2: MyStruct2,
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize, Serialize, JsonSchema, Debug)]
|
||||
#[serde(default = "ten_and_true")]
|
||||
pub struct MyStruct2 {
|
||||
#[serde(default = "six")]
|
||||
pub my_int: i32,
|
||||
pub my_bool: bool,
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore] // not yet implemented (https://github.com/GREsau/schemars/issues/6)
|
||||
fn schema_default_values() -> TestResult {
|
||||
test_default_generated_schema::<MyStruct>("default")
|
||||
}
|
43
schemars/tests/expected/default.json
Normal file
43
schemars/tests/expected/default.json
Normal file
|
@ -0,0 +1,43 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "MyStruct",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"my_bool": {
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
},
|
||||
"my_int": {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"default": 0
|
||||
},
|
||||
"my_struct2": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/MyStruct2"
|
||||
}
|
||||
],
|
||||
"default": {
|
||||
"my_bool": false,
|
||||
"my_int": 0
|
||||
}
|
||||
}
|
||||
},
|
||||
"definitions": {
|
||||
"MyStruct2": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"my_bool": {
|
||||
"type": "boolean",
|
||||
"default": true
|
||||
},
|
||||
"my_int": {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"default": 6
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue