Merge branch 'master' into validate

This commit is contained in:
Graham Esau 2021-04-15 16:03:25 +01:00
commit b68132f17d
31 changed files with 440 additions and 143 deletions

8
schemars/tests/bytes.rs Normal file
View file

@ -0,0 +1,8 @@
mod util;
use bytes::{Bytes, BytesMut};
use util::*;
#[test]
fn bytes() -> TestResult {
test_default_generated_schema::<(Bytes, BytesMut)>("bytes")
}

View file

@ -0,0 +1,19 @@
mod util;
use ::schemars as not_schemars;
use util::*;
#[allow(unused_imports)]
use std as schemars;
#[derive(Debug, not_schemars::JsonSchema)]
#[schemars(crate = "not_schemars")]
pub struct Struct {
/// This is a document
foo: i32,
bar: bool,
}
#[test]
fn test_crate_alias() -> TestResult {
test_default_generated_schema::<Struct>("crate_alias")
}

View file

@ -14,8 +14,8 @@ pub struct Struct {
bar: bool,
}
// Outer container should always have additionalPropreties: false
// `Struct` variant should have additionalPropreties: false
// Outer container should always have additionalProperties: false
// `Struct` variant should have additionalProperties: false
#[derive(Debug, JsonSchema)]
#[schemars(rename_all = "camelCase", deny_unknown_fields)]
pub enum External {
@ -38,7 +38,7 @@ fn enum_external_tag() -> TestResult {
test_default_generated_schema::<External>("enum-external-duf")
}
// Only `Struct` variant should have additionalPropreties: false
// Only `Struct` variant should have additionalProperties: false
#[derive(Debug, JsonSchema)]
#[schemars(tag = "typeProperty", deny_unknown_fields)]
pub enum Internal {
@ -60,7 +60,7 @@ fn enum_internal_tag() -> TestResult {
test_default_generated_schema::<Internal>("enum-internal-duf")
}
// Only `Struct` variant should have additionalPropreties: false
// Only `Struct` variant should have additionalProperties: false
#[derive(Debug, JsonSchema)]
#[schemars(untagged, deny_unknown_fields)]
pub enum Untagged {
@ -82,7 +82,7 @@ fn enum_untagged() -> TestResult {
test_default_generated_schema::<Untagged>("enum-untagged-duf")
}
// Outer container and `Struct` variant should have additionalPropreties: false
// Outer container and `Struct` variant should have additionalProperties: false
#[derive(Debug, JsonSchema)]
#[schemars(tag = "t", content = "c", deny_unknown_fields)]
pub enum Adjacent {

View file

@ -0,0 +1,25 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Tuple_of_Array_of_uint8_and_Array_of_uint8",
"type": "array",
"items": [
{
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
},
{
"type": "array",
"items": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
}
}
],
"maxItems": 2,
"minItems": 2
}

View file

@ -0,0 +1,19 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Struct",
"type": "object",
"required": [
"bar",
"foo"
],
"properties": {
"foo": {
"description": "This is a document",
"type": "integer",
"format": "int32"
},
"bar": {
"type": "boolean"
}
}
}

View file

@ -0,0 +1,23 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "OuterEnum",
"anyOf": [
{
"type": "object",
"required": [
"InnerStruct"
],
"properties": {
"InnerStruct": {
"$ref": "#/definitions/InnerStruct"
}
},
"additionalProperties": false
}
],
"definitions": {
"InnerStruct": {
"type": "object"
}
}
}

View file

@ -0,0 +1,20 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "A",
"type": "object",
"required": [
"v",
"x"
],
"properties": {
"x": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
},
"v": {
"type": "integer",
"format": "int32"
}
}
}

View file

@ -74,6 +74,24 @@
"minItems": 2
}
}
},
{
"type": "object",
"required": [
"c",
"t"
],
"properties": {
"t": {
"type": "string",
"enum": [
"Unit"
]
},
"c": {
"type": "boolean"
}
}
}
]
}

View file

@ -56,6 +56,18 @@
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"unit"
],
"properties": {
"unit": {
"type": "boolean"
}
},
"additionalProperties": false
}
]
}

View file

@ -36,6 +36,23 @@
]
}
}
},
{
"type": [
"boolean",
"object"
],
"required": [
"typeProperty"
],
"properties": {
"typeProperty": {
"type": "string",
"enum": [
"Unit"
]
}
}
}
]
}

View file

@ -29,6 +29,9 @@
],
"maxItems": 2,
"minItems": 2
},
{
"type": "boolean"
}
]
}

67
schemars/tests/macro.rs Normal file
View file

@ -0,0 +1,67 @@
mod util;
use schemars::JsonSchema;
use util::*;
macro_rules! build_struct {
(
$id:ident { $($t:tt)* }
) => {
#[derive(Debug, JsonSchema)]
pub struct $id {
x: u8,
$($t)*
}
};
}
build_struct!(A { v: i32 });
#[test]
fn macro_built_struct() -> TestResult {
test_default_generated_schema::<A>("macro_built_struct")
}
macro_rules! build_enum {
(
$(#[$outer_derive:meta])*
$outer:ident {
$($(#[$inner_derive:meta])*
$inner:ident {
$( $(#[$field_attribute:meta])*
$field:ident : $ty:ty),*
})*
}
) => {
$(
$(#[$inner_derive])*
pub struct $inner {
$(
$(#[$field_attribute])*
pub $field: $ty
),*
}
)*
$(#[$outer_derive])*
pub enum $outer {
$(
$inner($inner)
),*
}
}
}
build_enum!(
#[derive(Debug, JsonSchema)]
OuterEnum {
#[derive(Debug, JsonSchema)]
InnerStruct {}
}
);
#[test]
fn macro_built_enum() -> TestResult {
test_default_generated_schema::<OuterEnum>("macro_built_enum")
}

View file

@ -2,8 +2,6 @@ mod util;
use schemars::JsonSchema;
use util::*;
// FIXME determine whether schema_with should be allowed on unit variants
fn schema_fn(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
<bool>::json_schema(gen)
}
@ -23,8 +21,8 @@ pub enum External {
#[schemars(schema_with = "schema_fn")] DoesntImplementJsonSchema,
i32,
),
// #[schemars(schema_with = "schema_fn")]
// Unit,
#[schemars(schema_with = "schema_fn")]
Unit,
}
#[test]
@ -40,8 +38,8 @@ pub enum Internal {
foo: DoesntImplementJsonSchema,
},
NewType(#[schemars(schema_with = "schema_fn")] DoesntImplementJsonSchema),
// #[schemars(schema_with = "schema_fn")]
// Unit,
#[schemars(schema_with = "schema_fn")]
Unit,
}
#[test]
@ -61,8 +59,8 @@ pub enum Untagged {
#[schemars(schema_with = "schema_fn")] DoesntImplementJsonSchema,
i32,
),
// #[schemars(schema_with = "schema_fn")]
// Unit,
#[schemars(schema_with = "schema_fn")]
Unit,
}
#[test]
@ -82,8 +80,8 @@ pub enum Adjacent {
#[schemars(schema_with = "schema_fn")] DoesntImplementJsonSchema,
i32,
),
// #[schemars(schema_with = "schema_fn")]
// Unit,
#[schemars(schema_with = "schema_fn")]
Unit,
}
#[test]