schema_with
attribute
This commit is contained in:
parent
9d951b34ce
commit
3fd316063a
15 changed files with 538 additions and 51 deletions
|
@ -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 {
|
||||
|
|
|
@ -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"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
58
schemars/tests/expected/schema_with-enum-external.json
Normal file
58
schemars/tests/expected/schema_with-enum-external.json
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
41
schemars/tests/expected/schema_with-enum-internal.json
Normal file
41
schemars/tests/expected/schema_with-enum-internal.json
Normal 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"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
34
schemars/tests/expected/schema_with-enum-untagged.json
Normal file
34
schemars/tests/expected/schema_with-enum-untagged.json
Normal 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
|
||||
}
|
||||
]
|
||||
}
|
5
schemars/tests/expected/schema_with-newtype.json
Normal file
5
schemars/tests/expected/schema_with-newtype.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "Newtype",
|
||||
"type": "boolean"
|
||||
}
|
22
schemars/tests/expected/schema_with-struct.json
Normal file
22
schemars/tests/expected/schema_with-struct.json
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
19
schemars/tests/expected/schema_with-tuple.json
Normal file
19
schemars/tests/expected/schema_with-tuple.json
Normal 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
|
||||
}
|
92
schemars/tests/schema_with_enum.rs
Normal file
92
schemars/tests/schema_with_enum.rs
Normal 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")
|
||||
}
|
44
schemars/tests/schema_with_struct.rs
Normal file
44
schemars/tests/schema_with_struct.rs
Normal 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")
|
||||
}
|
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue