Exclude skipped fields/variants from json schema

This commit is contained in:
Graham Esau 2019-09-12 19:07:25 +01:00
parent 709ba7b62e
commit 5de6bcfdef
5 changed files with 137 additions and 11 deletions

View file

@ -0,0 +1,23 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MyEnum",
"anyOf": [
{
"enum": [
"Included2"
]
},
{
"type": "object",
"properties": {
"Included1": {
"type": "number",
"format": "float"
}
},
"required": [
"Included1"
]
}
]
}

View file

@ -0,0 +1,18 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MyStruct",
"type": "object",
"properties": {
"included1": {
"type": "number",
"format": "float"
},
"included2": {
"type": "null"
}
},
"required": [
"included1",
"included2"
]
}

View file

@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "TupleStruct",
"type": "array",
"items": [
{
"type": "number",
"format": "float"
},
{
"type": "null"
}
],
"maxItems": 2,
"minItems": 2
}

58
schemars/tests/skip.rs Normal file
View file

@ -0,0 +1,58 @@
mod util;
use schemars::JsonSchema;
use util::*;
#[derive(Debug, JsonSchema)]
struct MyStruct {
#[schemars(skip)]
skipped1: i32,
#[serde(skip)]
skipped2: bool,
#[serde(skip_deserializing)]
skipped3: String,
#[serde(skip_serializing)]
included1: f32,
included2: (),
}
#[test]
fn skip_struct_fields() -> TestResult {
test_default_generated_schema::<MyStruct>("skip_struct_fields")
}
#[derive(Debug, JsonSchema)]
struct TupleStruct (
#[schemars(skip)]
i32,
#[serde(skip)]
bool,
#[serde(skip_deserializing)]
String,
#[serde(skip_serializing)]
f32,
(),
);
#[test]
fn skip_tuple_fields() -> TestResult {
test_default_generated_schema::<TupleStruct>("skip_tuple_fields")
}
#[derive(Debug, JsonSchema)]
pub enum MyEnum {
#[schemars(skip)]
Skipped1(i32),
#[serde(skip)]
Skipped2,
#[serde(skip_deserializing)]
Skipped3,
#[serde(skip_serializing)]
Included1(f32),
Included2,
}
#[test]
fn skip_enum_variants() -> TestResult {
test_default_generated_schema::<MyEnum>("skip_enum_variants")
}