diff --git a/schemars/src/_private.rs b/schemars/src/_private.rs index 1799f41..b914699 100644 --- a/schemars/src/_private.rs +++ b/schemars/src/_private.rs @@ -6,12 +6,11 @@ use crate::JsonSchema; // Helper for generating schemas for flattened `Option` fields. pub fn json_schema_for_flatten( gen: &mut SchemaGenerator, - required: Option, + required: bool, ) -> Schema { let mut schema = T::_schemars_private_non_optional_json_schema(gen); - let required = required.unwrap_or_else(|| !T::_schemars_private_is_option()); - if !required { + if T::_schemars_private_is_option() && !required { if let Schema::Object(SchemaObject { object: Some(ref mut object_validation), .. diff --git a/schemars/src/schema.rs b/schemars/src/schema.rs index e9a0124..d358a44 100644 --- a/schemars/src/schema.rs +++ b/schemars/src/schema.rs @@ -231,7 +231,11 @@ impl SchemaObject { self.reference.is_some() } - // TODO document + /// Returns `true` if `self` accepts values of the given type, according to the [`instance_type`] field. + /// + /// This is a basic check that always returns `true` if no `instance_type` is specified on the schema, + /// and does not check any subschemas. Because of this, both `{}` and `{"not": {}}` accept any type according + /// to this method. pub fn has_type(&self, ty: InstanceType) -> bool { self.instance_type .as_ref() @@ -522,7 +526,20 @@ impl From> for SingleOrVec { } impl SingleOrVec { - // TODO document + /// Returns `true` if `self` is either a `Single` equal to `x`, or a `Vec` containing `x`. + /// + /// # Examples + /// + /// ``` + /// let s = SingleOrVec::Single(10); + /// assert!(s.contains(&10)); + /// assert!(!s.contains(&20)); + /// + /// let v = SingleOrVec::Vec(vec![10, 20]); + /// assert!(s.contains(&10)); + /// assert!(s.contains(&20)); + /// assert!(!s.contains(&30)); + /// ``` pub fn contains(&self, x: &T) -> bool { match self { SingleOrVec::Single(s) => s.deref() == x, diff --git a/schemars_derive/src/attr/validation.rs b/schemars_derive/src/attr/validation.rs index 9b807b3..1dccc6d 100644 --- a/schemars_derive/src/attr/validation.rs +++ b/schemars_derive/src/attr/validation.rs @@ -22,7 +22,6 @@ pub struct ValidationAttrs { impl ValidationAttrs { pub fn new(attrs: &[syn::Attribute], errors: &Ctxt) -> Self { - // TODO allow setting "validate" attributes through #[schemars(...)] ValidationAttrs::default() .populate(attrs, "schemars", false, errors) .populate(attrs, "validate", true, errors) diff --git a/schemars_derive/src/schema_exprs.rs b/schemars_derive/src/schema_exprs.rs index b85e64d..c50f332 100644 --- a/schemars_derive/src/schema_exprs.rs +++ b/schemars_derive/src/schema_exprs.rs @@ -489,11 +489,7 @@ fn expr_for_struct( .map(|field| { let (ty, type_def) = type_for_field_schema(field); - let required = if field.validation_attrs.required { - quote!(Some(true)) - } else { - quote!(None) - }; + let required = field.validation_attrs.required; let args = quote!(gen, #required); let mut schema_expr = quote_spanned! {ty.span()=>