diff --git a/schemars/tests/default.rs b/schemars/tests/default.rs new file mode 100644 index 0000000..77ebe77 --- /dev/null +++ b/schemars/tests/default.rs @@ -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::("default") +} diff --git a/schemars/tests/expected/default.json b/schemars/tests/expected/default.json new file mode 100644 index 0000000..3610b54 --- /dev/null +++ b/schemars/tests/expected/default.json @@ -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 + } + } + } + } +} \ No newline at end of file