Require Visitors to implement Clone

This commit is contained in:
Graham Esau 2020-06-05 23:48:03 +01:00
parent 5b316fb31b
commit 475a751b70
4 changed files with 14 additions and 23 deletions

View file

@ -8,6 +8,7 @@
### Removed (**BREAKING CHANGES**): ### Removed (**BREAKING CHANGES**):
- `SchemaSettings::bool_schemas` - this has been superseded by the `ReplaceBoolSchemas` visitor - `SchemaSettings::bool_schemas` - this has been superseded by the `ReplaceBoolSchemas` visitor
- `SchemaSettings::allow_ref_siblings` - this has been superseded by the `RemoveRefSiblings` visitor - `SchemaSettings::allow_ref_siblings` - this has been superseded by the `RemoveRefSiblings` visitor
- `SchemaSettings` no longer implements `PartialEq`
### Fixed: ### Fixed:
- **BREAKING CHANGE** unknown items in `#[schemars(...)]` attributes now cause a compilation error (https://github.com/GREsau/schemars/issues/18) - **BREAKING CHANGE** unknown items in `#[schemars(...)]` attributes now cause a compilation error (https://github.com/GREsau/schemars/issues/18)

View file

@ -16,6 +16,7 @@ build = "build.rs"
schemars_derive = { version = "=0.7.6", optional = true, path = "../schemars_derive" } schemars_derive = { version = "=0.7.6", optional = true, path = "../schemars_derive" }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
dyn-clone = "1.0"
chrono = { version = "0.4", default-features = false, optional = true } chrono = { version = "0.4", default-features = false, optional = true }
indexmap = { version = "1.2", optional = true } indexmap = { version = "1.2", optional = true }

View file

@ -1,13 +1,12 @@
use crate::flatten::Merge; use crate::flatten::Merge;
use crate::schema::*; use crate::schema::*;
use crate::{visit::*, JsonSchema, Map}; use crate::{visit::*, JsonSchema, Map};
use std::sync::Arc;
/// Settings to customize how Schemas are generated. /// Settings to customize how Schemas are generated.
/// ///
/// The default settings currently conform to [JSON Schema Draft 7](https://json-schema.org/specification-links.html#draft-7), but this is liable to change in a future version of Schemars if support for other JSON Schema versions is added. /// The default settings currently conform to [JSON Schema Draft 7](https://json-schema.org/specification-links.html#draft-7), but this is liable to change in a future version of Schemars if support for other JSON Schema versions is added.
/// If you require your generated schemas to conform to draft 7, consider using the [`draft07`](#method.draft07) method. /// If you require your generated schemas to conform to draft 7, consider using the [`draft07`](#method.draft07) method.
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, Clone)]
pub struct SchemaSettings { pub struct SchemaSettings {
/// If `true`, schemas for [`Option<T>`](Option) will include a `nullable` property. /// If `true`, schemas for [`Option<T>`](Option) will include a `nullable` property.
/// ///
@ -46,7 +45,7 @@ impl SchemaSettings {
option_add_null_type: true, option_add_null_type: true,
definitions_path: "#/definitions/".to_owned(), definitions_path: "#/definitions/".to_owned(),
meta_schema: Some("http://json-schema.org/draft-07/schema#".to_owned()), meta_schema: Some("http://json-schema.org/draft-07/schema#".to_owned()),
visitors: Visitors(vec![Arc::new(RemoveRefSiblings)]), visitors: Visitors(vec![Box::new(RemoveRefSiblings)]),
_hidden: (), _hidden: (),
} }
} }
@ -74,8 +73,8 @@ impl SchemaSettings {
.to_owned(), .to_owned(),
), ),
visitors: Visitors(vec![ visitors: Visitors(vec![
Arc::new(RemoveRefSiblings), Box::new(RemoveRefSiblings),
Arc::new(ReplaceBoolSchemas { Box::new(ReplaceBoolSchemas {
skip_additional_properties: true, skip_additional_properties: true,
}), }),
]), ]),
@ -102,7 +101,7 @@ impl SchemaSettings {
/// TODO document /// TODO document
pub fn with_visitor(mut self, visitor: impl Visitor + 'static) -> Self { pub fn with_visitor(mut self, visitor: impl Visitor + 'static) -> Self {
self.visitors.0.push(Arc::new(visitor)); self.visitors.0.push(Box::new(visitor));
self self
} }
@ -114,20 +113,7 @@ impl SchemaSettings {
/// TODO document /// TODO document
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct Visitors(Vec<Arc<dyn Visitor>>); pub struct Visitors(Vec<Box<dyn Visitor>>);
impl PartialEq for Visitors {
fn eq(&self, other: &Self) -> bool {
if self.0.len() != other.0.len() {
return false;
}
self.0
.iter()
.zip(other.0.iter())
.all(|(a, b)| Arc::ptr_eq(a, b))
}
}
/// The main type used to generate JSON Schemas. /// The main type used to generate JSON Schemas.
/// ///
@ -338,7 +324,7 @@ impl SchemaGenerator {
} }
/// TODO document /// TODO document
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct ReplaceBoolSchemas { pub struct ReplaceBoolSchemas {
pub skip_additional_properties: bool, pub skip_additional_properties: bool,
} }
@ -375,7 +361,7 @@ impl Visitor for ReplaceBoolSchemas {
} }
/// TODO document /// TODO document
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct RemoveRefSiblings; pub struct RemoveRefSiblings;
impl Visitor for RemoveRefSiblings { impl Visitor for RemoveRefSiblings {

View file

@ -1,7 +1,8 @@
use crate::schema::{RootSchema, Schema, SchemaObject, SingleOrVec}; use crate::schema::{RootSchema, Schema, SchemaObject, SingleOrVec};
use dyn_clone::DynClone;
use std::fmt::Debug; use std::fmt::Debug;
pub trait Visitor: Debug { pub trait Visitor: Debug + DynClone {
fn visit_root_schema(&self, root: &mut RootSchema) { fn visit_root_schema(&self, root: &mut RootSchema) {
visit_root_schema(self, root) visit_root_schema(self, root)
} }
@ -15,6 +16,8 @@ pub trait Visitor: Debug {
} }
} }
dyn_clone::clone_trait_object!(Visitor);
pub fn visit_root_schema<V: Visitor + ?Sized>(v: &V, root: &mut RootSchema) { pub fn visit_root_schema<V: Visitor + ?Sized>(v: &V, root: &mut RootSchema) {
v.visit_schema_object(&mut root.schema); v.visit_schema_object(&mut root.schema);
visit_map_values(v, &mut root.definitions); visit_map_values(v, &mut root.definitions);