schema_with attribute

This commit is contained in:
Graham Esau 2020-05-15 17:11:28 +01:00
parent 9d951b34ce
commit 3fd316063a
15 changed files with 538 additions and 51 deletions

View file

@ -2,6 +2,9 @@ mod util;
use schemars::{JsonSchema, Map};
use util::*;
// Ensure that schemars_derive uses the full path to std::string::String
pub struct String;
#[derive(Debug, JsonSchema)]
pub struct UnitStruct;
@ -15,7 +18,7 @@ pub struct Struct {
#[schemars(rename_all = "camelCase")]
pub enum External {
UnitOne,
StringMap(Map<String, String>),
StringMap(Map<&'static str, &'static str>),
UnitStructNewType(UnitStruct),
StructNewType(Struct),
Struct {
@ -37,7 +40,7 @@ fn enum_external_tag() -> TestResult {
#[schemars(tag = "typeProperty")]
pub enum Internal {
UnitOne,
StringMap(Map<String, String>),
StringMap(Map<&'static str, &'static str>),
UnitStructNewType(UnitStruct),
StructNewType(Struct),
Struct {
@ -58,7 +61,7 @@ fn enum_internal_tag() -> TestResult {
#[schemars(untagged)]
pub enum Untagged {
UnitOne,
StringMap(Map<String, String>),
StringMap(Map<&'static str, &'static str>),
UnitStructNewType(UnitStruct),
StructNewType(Struct),
Struct {
@ -79,7 +82,7 @@ fn enum_untagged() -> TestResult {
#[schemars(tag = "t", content = "c")]
pub enum Adjacent {
UnitOne,
StringMap(Map<String, String>),
StringMap(Map<&'static str, &'static str>),
UnitStructNewType(UnitStruct),
StructNewType(Struct),
Struct {

View file

@ -0,0 +1,79 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Adjacent",
"anyOf": [
{
"type": "object",
"required": [
"c",
"t"
],
"properties": {
"c": {
"type": "object",
"required": [
"foo"
],
"properties": {
"foo": {
"type": "boolean"
}
}
},
"t": {
"type": "string",
"enum": [
"Struct"
]
}
}
},
{
"type": "object",
"required": [
"c",
"t"
],
"properties": {
"c": {
"type": "boolean"
},
"t": {
"type": "string",
"enum": [
"NewType"
]
}
}
},
{
"type": "object",
"required": [
"c",
"t"
],
"properties": {
"c": {
"type": "array",
"items": [
{
"type": "boolean"
},
{
"type": "integer",
"format": "int32"
}
],
"maxItems": 2,
"minItems": 2
},
"t": {
"type": "string",
"enum": [
"Tuple"
]
}
}
}
]
}

View file

@ -0,0 +1,58 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "External",
"anyOf": [
{
"type": "object",
"required": [
"struct"
],
"properties": {
"struct": {
"type": "object",
"required": [
"foo"
],
"properties": {
"foo": {
"type": "boolean"
}
}
}
}
},
{
"type": "object",
"required": [
"newType"
],
"properties": {
"newType": {
"type": "boolean"
}
}
},
{
"type": "object",
"required": [
"tuple"
],
"properties": {
"tuple": {
"type": "array",
"items": [
{
"type": "boolean"
},
{
"type": "integer",
"format": "int32"
}
],
"maxItems": 2,
"minItems": 2
}
}
}
]
}

View file

@ -0,0 +1,41 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Internal",
"anyOf": [
{
"type": "object",
"required": [
"foo",
"typeProperty"
],
"properties": {
"foo": {
"type": "boolean"
},
"typeProperty": {
"type": "string",
"enum": [
"Struct"
]
}
}
},
{
"type": [
"boolean",
"object"
],
"required": [
"typeProperty"
],
"properties": {
"typeProperty": {
"type": "string",
"enum": [
"NewType"
]
}
}
}
]
}

View file

@ -0,0 +1,34 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Untagged",
"anyOf": [
{
"type": "object",
"required": [
"foo"
],
"properties": {
"foo": {
"type": "boolean"
}
}
},
{
"type": "boolean"
},
{
"type": "array",
"items": [
{
"type": "boolean"
},
{
"type": "integer",
"format": "int32"
}
],
"maxItems": 2,
"minItems": 2
}
]
}

View file

@ -0,0 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Newtype",
"type": "boolean"
}

View file

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

View file

@ -0,0 +1,19 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Tuple",
"type": "array",
"items": [
{
"type": "boolean"
},
{
"type": "integer",
"format": "int32"
},
{
"type": "boolean"
}
],
"maxItems": 3,
"minItems": 3
}

View file

@ -0,0 +1,92 @@
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)
}
#[derive(Debug)]
pub struct DoesntImplementJsonSchema;
#[derive(Debug, JsonSchema)]
#[schemars(rename_all = "camelCase")]
pub enum External {
Struct {
#[schemars(schema_with = "schema_fn")]
foo: DoesntImplementJsonSchema,
},
NewType(#[schemars(schema_with = "schema_fn")] DoesntImplementJsonSchema),
Tuple(
#[schemars(schema_with = "schema_fn")] DoesntImplementJsonSchema,
i32,
),
// #[schemars(schema_with = "schema_fn")]
// Unit,
}
#[test]
fn enum_external_tag() -> TestResult {
test_default_generated_schema::<External>("schema_with-enum-external")
}
#[derive(Debug, JsonSchema)]
#[schemars(tag = "typeProperty")]
pub enum Internal {
Struct {
#[schemars(schema_with = "schema_fn")]
foo: DoesntImplementJsonSchema,
},
NewType(#[schemars(schema_with = "schema_fn")] DoesntImplementJsonSchema),
// #[schemars(schema_with = "schema_fn")]
// Unit,
}
#[test]
fn enum_internal_tag() -> TestResult {
test_default_generated_schema::<Internal>("schema_with-enum-internal")
}
#[derive(Debug, JsonSchema)]
#[schemars(untagged)]
pub enum Untagged {
Struct {
#[schemars(schema_with = "schema_fn")]
foo: DoesntImplementJsonSchema,
},
NewType(#[schemars(schema_with = "schema_fn")] DoesntImplementJsonSchema),
Tuple(
#[schemars(schema_with = "schema_fn")] DoesntImplementJsonSchema,
i32,
),
// #[schemars(schema_with = "schema_fn")]
// Unit,
}
#[test]
fn enum_untagged() -> TestResult {
test_default_generated_schema::<Untagged>("schema_with-enum-untagged")
}
#[derive(Debug, JsonSchema)]
#[schemars(tag = "t", content = "c")]
pub enum Adjacent {
Struct {
#[schemars(schema_with = "schema_fn")]
foo: DoesntImplementJsonSchema,
},
NewType(#[schemars(schema_with = "schema_fn")] DoesntImplementJsonSchema),
Tuple(
#[schemars(schema_with = "schema_fn")] DoesntImplementJsonSchema,
i32,
),
// #[schemars(schema_with = "schema_fn")]
// Unit,
}
#[test]
fn enum_adjacent_tagged() -> TestResult {
test_default_generated_schema::<Adjacent>("schema_with-enum-adjacent-tagged")
}

View file

@ -0,0 +1,44 @@
mod util;
use schemars::JsonSchema;
use util::*;
fn schema_fn(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
<bool>::json_schema(gen)
}
#[derive(Debug)]
struct DoesntImplementJsonSchema;
#[derive(Debug, JsonSchema)]
pub struct Struct {
#[schemars(schema_with = "schema_fn")]
foo: DoesntImplementJsonSchema,
bar: i32,
#[schemars(schema_with = "schema_fn")]
baz: DoesntImplementJsonSchema,
}
#[test]
fn struct_normal() -> TestResult {
test_default_generated_schema::<Struct>("schema_with-struct")
}
#[derive(Debug, JsonSchema)]
pub struct Tuple(
#[schemars(schema_with = "schema_fn")] DoesntImplementJsonSchema,
i32,
#[schemars(schema_with = "schema_fn")] DoesntImplementJsonSchema,
);
#[test]
fn struct_tuple() -> TestResult {
test_default_generated_schema::<Tuple>("schema_with-tuple")
}
#[derive(Debug, JsonSchema)]
pub struct Newtype(#[schemars(schema_with = "schema_fn")] DoesntImplementJsonSchema);
#[test]
fn struct_newtype() -> TestResult {
test_default_generated_schema::<Newtype>("schema_with-newtype")
}

View file

@ -2,11 +2,14 @@ mod util;
use schemars::JsonSchema;
use util::*;
// Ensure that schemars_derive uses the full path to std::string::String
pub struct String;
#[derive(Debug, JsonSchema)]
pub struct Struct {
foo: i32,
bar: bool,
baz: Option<String>,
baz: Option<&'static str>,
}
#[test]
@ -15,7 +18,7 @@ fn struct_normal() -> TestResult {
}
#[derive(Debug, JsonSchema)]
pub struct Tuple(i32, bool, Option<String>);
pub struct Tuple(i32, bool, Option<&'static str>);
#[test]
fn struct_tuple() -> TestResult {