Attribute for defining examples (#23)
This commit is contained in:
parent
19b9bef395
commit
e259955809
4 changed files with 88 additions and 3 deletions
25
schemars/tests/examples.rs
Normal file
25
schemars/tests/examples.rs
Normal file
|
@ -0,0 +1,25 @@
|
|||
mod util;
|
||||
use schemars::JsonSchema;
|
||||
use serde::Serialize;
|
||||
use util::*;
|
||||
|
||||
#[derive(Default, Debug, JsonSchema, Serialize)]
|
||||
#[schemars(example = "Struct::default", example = "null")]
|
||||
pub struct Struct {
|
||||
#[schemars(example = "eight", example = "null")]
|
||||
foo: i32,
|
||||
bar: bool,
|
||||
#[schemars(example = "null")]
|
||||
baz: Option<&'static str>,
|
||||
}
|
||||
|
||||
fn eight() -> i32 {
|
||||
8
|
||||
}
|
||||
|
||||
fn null() -> () {}
|
||||
|
||||
#[test]
|
||||
fn examples() -> TestResult {
|
||||
test_default_generated_schema::<Struct>("examples")
|
||||
}
|
39
schemars/tests/expected/examples.json
Normal file
39
schemars/tests/expected/examples.json
Normal file
|
@ -0,0 +1,39 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "Struct",
|
||||
"examples": [
|
||||
{
|
||||
"bar": false,
|
||||
"baz": null,
|
||||
"foo": 0
|
||||
},
|
||||
null
|
||||
],
|
||||
"type": "object",
|
||||
"required": [
|
||||
"bar",
|
||||
"foo"
|
||||
],
|
||||
"properties": {
|
||||
"bar": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"baz": {
|
||||
"examples": [
|
||||
null
|
||||
],
|
||||
"type": [
|
||||
"string",
|
||||
"null"
|
||||
]
|
||||
},
|
||||
"foo": {
|
||||
"examples": [
|
||||
8,
|
||||
null
|
||||
],
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue