Do not implement JsonSchema for internal types by default.

It will be implemented only if the derive_json_schema feature is enabled
This commit is contained in:
Graham Esau 2019-10-21 21:45:29 +01:00
parent f8737be9c4
commit 5c307b92fb
2 changed files with 30 additions and 13 deletions

View file

@ -19,9 +19,16 @@ chrono = { version = "0.4", default-features = false, optional = true }
[dev-dependencies] [dev-dependencies]
pretty_assertions = "0.6.1" pretty_assertions = "0.6.1"
[features]
derive_json_schema = []
[[test]] [[test]]
name = "chrono" name = "chrono"
required-features = ["chrono"] required-features = ["chrono"]
[[test]]
name = "schema_for_schema"
required-features = ["derive_json_schema"]
[package.metadata.docs.rs] [package.metadata.docs.rs]
all-features = true all-features = true

View file

@ -1,11 +1,14 @@
use crate as schemars; use crate as schemars;
use crate::{JsonSchema, Map, Set}; #[cfg(feature = "derive_json_schema")]
use crate::JsonSchema;
use crate::{Map, Set};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
/// A JSON Schema. /// A JSON Schema.
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "derive_json_schema", derive(JsonSchema))]
#[serde(untagged)] #[serde(untagged)]
pub enum Schema { pub enum Schema {
/// A trivial boolean JSON Schema. /// A trivial boolean JSON Schema.
@ -51,7 +54,8 @@ impl From<bool> for Schema {
} }
/// A JSON Schema object. /// A JSON Schema object.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "derive_json_schema", derive(JsonSchema))]
#[serde(rename_all = "camelCase", default)] #[serde(rename_all = "camelCase", default)]
pub struct SchemaObject { pub struct SchemaObject {
/// Properties which annotate the [`SchemaObject`] which typically have no effect when an object is being validated against the schema. /// Properties which annotate the [`SchemaObject`] which typically have no effect when an object is being validated against the schema.
@ -177,7 +181,8 @@ impl From<Schema> for SchemaObject {
} }
/// Properties which annotate a [`SchemaObject`] which typically have no effect when an object is being validated against the schema. /// Properties which annotate a [`SchemaObject`] which typically have no effect when an object is being validated against the schema.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "derive_json_schema", derive(JsonSchema))]
#[serde(rename_all = "camelCase", default)] #[serde(rename_all = "camelCase", default)]
pub struct Metadata { pub struct Metadata {
/// The `$schema` keyword. /// The `$schema` keyword.
@ -237,7 +242,8 @@ fn is_false(b: &bool) -> bool {
} }
/// Properties of a [`SchemaObject`] which define validation assertions in terms of other schemas. /// Properties of a [`SchemaObject`] which define validation assertions in terms of other schemas.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "derive_json_schema", derive(JsonSchema))]
#[serde(rename_all = "camelCase", default)] #[serde(rename_all = "camelCase", default)]
pub struct SubschemaValidation { pub struct SubschemaValidation {
/// The `allOf` keyword. /// The `allOf` keyword.
@ -278,7 +284,8 @@ pub struct SubschemaValidation {
} }
/// Properties of a [`SchemaObject`] which define validation assertions for numbers. /// Properties of a [`SchemaObject`] which define validation assertions for numbers.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "derive_json_schema", derive(JsonSchema))]
#[serde(rename_all = "camelCase", default)] #[serde(rename_all = "camelCase", default)]
pub struct NumberValidation { pub struct NumberValidation {
/// The `multipleOf` keyword. /// The `multipleOf` keyword.
@ -309,7 +316,8 @@ pub struct NumberValidation {
} }
/// Properties of a [`SchemaObject`] which define validation assertions for strings. /// Properties of a [`SchemaObject`] which define validation assertions for strings.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "derive_json_schema", derive(JsonSchema))]
#[serde(rename_all = "camelCase", default)] #[serde(rename_all = "camelCase", default)]
pub struct StringValidation { pub struct StringValidation {
/// The `maxLength` keyword. /// The `maxLength` keyword.
@ -330,7 +338,8 @@ pub struct StringValidation {
} }
/// Properties of a [`SchemaObject`] which define validation assertions for arrays. /// Properties of a [`SchemaObject`] which define validation assertions for arrays.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "derive_json_schema", derive(JsonSchema))]
#[serde(rename_all = "camelCase", default)] #[serde(rename_all = "camelCase", default)]
pub struct ArrayValidation { pub struct ArrayValidation {
/// The `items` keyword. /// The `items` keyword.
@ -366,7 +375,8 @@ pub struct ArrayValidation {
} }
/// Properties of a [`SchemaObject`] which define validation assertions for objects. /// Properties of a [`SchemaObject`] which define validation assertions for objects.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "derive_json_schema", derive(JsonSchema))]
#[serde(rename_all = "camelCase", default)] #[serde(rename_all = "camelCase", default)]
pub struct ObjectValidation { pub struct ObjectValidation {
/// The `maxProperties` keyword. /// The `maxProperties` keyword.
@ -409,9 +419,8 @@ pub struct ObjectValidation {
/// The possible types of values in JSON Schema documents. /// The possible types of values in JSON Schema documents.
/// ///
/// See [JSON Schema 4.2.1. Instance Data Model](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-4.2.1). /// See [JSON Schema 4.2.1. Instance Data Model](https://tools.ietf.org/html/draft-handrews-json-schema-02#section-4.2.1).
#[derive( #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, JsonSchema, #[cfg_attr(feature = "derive_json_schema", derive(JsonSchema))]
)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub enum InstanceType { pub enum InstanceType {
Null, Null,
@ -426,7 +435,8 @@ pub enum InstanceType {
/// A type which can be serialized as a single item, or multiple items. /// A type which can be serialized as a single item, or multiple items.
/// ///
/// In some contexts, a `Single` may be semantically distinct from a `Vec` containing only item. /// In some contexts, a `Single` may be semantically distinct from a `Vec` containing only item.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, JsonSchema)] #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "derive_json_schema", derive(JsonSchema))]
#[serde(untagged)] #[serde(untagged)]
pub enum SingleOrVec<T> { pub enum SingleOrVec<T> {
Single(Box<T>), Single(Box<T>),