diff --git a/typify-impl/src/merge.rs b/typify-impl/src/merge.rs index e93721f8..f3710be9 100644 --- a/typify-impl/src/merge.rs +++ b/typify-impl/src/merge.rs @@ -1075,8 +1075,8 @@ fn merge_so_object( // both. .filter_map(|(name, ab_schema)| { let resolved_schema = match ab_schema { - AOrB::A(a_schema) => filter_prop(name, a_schema, bb), - AOrB::B(b_schema) => filter_prop(name, b_schema, aa), + AOrB::A(a_schema) => filter_prop(name, a_schema, bb, defs), + AOrB::B(b_schema) => filter_prop(name, b_schema, aa, defs), AOrB::Both(a_schema, b_schema) => merge_schema(a_schema, b_schema, defs), }; match resolved_schema { @@ -1122,21 +1122,29 @@ fn merge_so_object( return Err(()); } + let pattern_properties = + merge_pattern_properties(aa, bb, additional_properties.as_ref(), defs); + let object_validation = ObjectValidation { required, properties, additional_properties: additional_properties.map(Box::new), max_properties, min_properties, - pattern_properties: Default::default(), // TODO - property_names: Default::default(), // TODO + pattern_properties, + property_names: Default::default(), // TODO }; Ok(Some(object_validation.into())) } } } -fn filter_prop(name: &str, prop_schema: &Schema, object_schema: &ObjectValidation) -> Schema { +fn filter_prop( + name: &str, + prop_schema: &Schema, + object_schema: &ObjectValidation, + defs: &BTreeMap, +) -> Schema { // We're only considering properties we *know* do not appear in the other // object's schema. assert!(!object_schema.properties.contains_key(name)); @@ -1145,16 +1153,77 @@ fn filter_prop(name: &str, prop_schema: &Schema, object_schema: &ObjectValidatio // propertyNames if that schema is specified. assert!(object_schema.property_names.is_none()); - // TODO We should first check patternProperties, but that's such a pain in - // the neck and so weird that I can't be bothered right now (until we hit - // some examples in the wild). A match here would exempt the property from - // the check below against additionalProperties. - assert!(object_schema.pattern_properties.is_empty()); + // The other object validates this property with the schema of every + // pattern that matches its name, and with additionalProperties only when + // no pattern matches. A pattern that does not compile matches nothing + // here, and conversion reports it where the pattern is used. + let mut matching = object_schema + .pattern_properties + .iter() + .filter(|(pattern, _)| { + regress::Regex::with_flags(pattern, "u").is_ok_and(|re| re.find(name).is_some()) + }) + .map(|(_, pattern_schema)| pattern_schema) + .peekable(); + + if matching.peek().is_none() { + return merge_additional(object_schema.additional_properties.as_deref(), prop_schema) + .unwrap_or(Schema::Bool(false)); + } - merge_additional(object_schema.additional_properties.as_deref(), prop_schema) + matching + .try_fold(prop_schema.clone(), |merged, pattern_schema| { + try_merge_schema(&merged, pattern_schema, defs) + }) .unwrap_or(Schema::Bool(false)) } +/// Merge the patternProperties of two objects. Struct and map generation read +/// patternProperties only for names that properties does not list, and +/// `filter_prop` applies the patterns to the listed names, so this works in +/// the same terms. A pattern on both sides takes both schemas. A pattern on one side +/// also takes the other side's additionalProperties, which is what validates +/// a name there when none of its patterns match. If a name can match a +/// pattern on each side without the two sides sharing that pattern, the +/// result is stricter than the intersection for that name. +fn merge_pattern_properties( + aa: &ObjectValidation, + bb: &ObjectValidation, + merged_additional: Option<&Schema>, + defs: &BTreeMap, +) -> schemars::Map { + let one_sided = |pattern_schema: &Schema, other: &ObjectValidation| { + merge_additional(other.additional_properties.as_deref(), pattern_schema) + .unwrap_or(Schema::Bool(false)) + }; + + aa.pattern_properties + .iter() + .map(|(pattern, a_schema)| { + let schema = match bb.pattern_properties.get(pattern) { + Some(b_schema) => merge_schema(a_schema, b_schema, defs), + None => one_sided(a_schema, bb), + }; + (pattern, schema) + }) + .chain( + bb.pattern_properties + .iter() + .filter(|(pattern, _)| !aa.pattern_properties.contains_key(*pattern)) + .map(|(pattern, b_schema)| (pattern, one_sided(b_schema, aa))), + ) + // As with properties, an unsatisfiable pattern adds nothing when + // additionalProperties already excludes every other name. + .filter(|(_, schema)| { + !matches!( + (schema, merged_additional), + (Schema::Bool(false), Some(Schema::Bool(false))) + ) + }) + .map(|(pattern, schema)| (pattern.clone(), schema)) + .collect() +} + fn merge_additional(additional: Option<&Schema>, prop_schema: &Schema) -> Result { match additional { // Anything is fine. @@ -1947,4 +2016,138 @@ mod tests { serde_json::to_string_pretty(&merged).unwrap(), ); } + + #[test] + fn test_merge_property_matching_pattern() { + // `count` is not named by `b`, but its name matches `b`'s pattern, so + // the pattern's schema applies to it instead of additionalProperties. + let a = json!({ + "type": "object", + "required": ["count"], + "properties": { + "count": { "type": "integer" } + } + }); + let b = json!({ + "patternProperties": { + "^[a-z]+$": { "minimum": 0 } + }, + "additionalProperties": false + }); + let ab = json!({ + "type": "object", + "required": ["count"], + "properties": { + "count": { "type": "integer", "minimum": 0 } + }, + "patternProperties": { + "^[a-z]+$": { "minimum": 0 } + }, + "additionalProperties": false + }); + + let a = serde_json::from_value(a).unwrap(); + let b = serde_json::from_value(b).unwrap(); + let ab: schemars::schema::Schema = serde_json::from_value(ab).unwrap(); + + let merged = try_merge_schema(&a, &b, &BTreeMap::default()).unwrap(); + assert_eq!( + merged, + ab, + "{}", + serde_json::to_string_pretty(&merged).unwrap(), + ); + } + + #[test] + fn test_merge_required_property_matching_no_pattern() { + // `Count` matches no pattern of `b`, so `b`'s additionalProperties + // decides, and `false` makes the required property unsatisfiable. + let a = json!({ + "type": "object", + "required": ["Count"], + "properties": { + "Count": { "type": "integer" } + } + }); + let b = json!({ + "patternProperties": { + "^[a-z]+$": { "type": "integer" } + }, + "additionalProperties": false + }); + + let a = serde_json::from_value(a).unwrap(); + let b = serde_json::from_value(b).unwrap(); + + assert!(try_merge_schema(&a, &b, &BTreeMap::default()).is_err()); + } + + #[test] + fn test_merge_pattern_against_closed_object() { + // `a` admits no names beyond `id`, so `b`'s pattern can match nothing + // in the merged object and is dropped. + let a = json!({ + "type": "object", + "properties": { + "id": { "type": "string" } + }, + "additionalProperties": false + }); + let b = json!({ + "patternProperties": { + "^x-": { "type": "string" } + } + }); + let ab = json!({ + "type": "object", + "properties": { + "id": { "type": "string" } + }, + "additionalProperties": false + }); + + let a = serde_json::from_value(a).unwrap(); + let b = serde_json::from_value(b).unwrap(); + let ab: schemars::schema::Schema = serde_json::from_value(ab).unwrap(); + + let merged = try_merge_schema(&a, &b, &BTreeMap::default()).unwrap(); + assert_eq!( + merged, + ab, + "{}", + serde_json::to_string_pretty(&merged).unwrap(), + ); + } + + #[test] + fn test_merge_invalid_pattern_matches_nothing() { + let a = json!({ + "type": "object", + "properties": { + "name": { "type": "string" } + } + }); + let b = json!({ + "patternProperties": { + "(": { "type": "integer" } + } + }); + let ab = json!({ + "type": "object", + "properties": { + "name": { "type": "string" } + }, + "patternProperties": { + "(": { "type": "integer" } + } + }); + + let a = serde_json::from_value(a).unwrap(); + let b = serde_json::from_value(b).unwrap(); + let ab: schemars::schema::Schema = serde_json::from_value(ab).unwrap(); + + let merged = try_merge_schema(&a, &b, &BTreeMap::default()).unwrap(); + assert_eq!(merged, ab); + } } diff --git a/typify/tests/schemas/merged-pattern-properties.json b/typify/tests/schemas/merged-pattern-properties.json new file mode 100644 index 00000000..11d1c684 --- /dev/null +++ b/typify/tests/schemas/merged-pattern-properties.json @@ -0,0 +1,77 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "names-base": { + "type": "object", + "properties": { + "names": { + "type": "array", + "items": { + "type": "string" + } + } + }, + "additionalProperties": true + }, + "string-map-over-base": { + "description": "the reduced schema from typify#882", + "patternProperties": { + "^([^.]+)$": { + "type": "string" + } + }, + "additionalProperties": false, + "$ref": "#/definitions/names-base" + }, + "counted-base": { + "type": "object", + "required": [ + "count" + ], + "properties": { + "count": { + "type": "integer" + } + } + }, + "counted-with-pattern": { + "description": "count matches the pattern, so it takes the pattern's minimum", + "allOf": [ + { + "$ref": "#/definitions/counted-base" + }, + { + "patternProperties": { + "^[a-z]+$": { + "type": "integer", + "minimum": 0 + } + } + } + ] + }, + "extension-map": { + "description": "both sides constrain the same pattern", + "allOf": [ + { + "type": "object", + "patternProperties": { + "^x-": { + "type": "string" + } + }, + "additionalProperties": false + }, + { + "type": "object", + "patternProperties": { + "^x-": { + "type": "string", + "maxLength": 10 + } + } + } + ] + } + } +} diff --git a/typify/tests/schemas/merged-pattern-properties.rs b/typify/tests/schemas/merged-pattern-properties.rs new file mode 100644 index 00000000..10f607f5 --- /dev/null +++ b/typify/tests/schemas/merged-pattern-properties.rs @@ -0,0 +1,385 @@ +#![deny(warnings)] +#[doc = "`CountedBase`"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] +pub struct CountedBase { + pub count: i64, +} +impl CountedBase { + pub fn builder() -> builder::CountedBase { + Default::default() + } +} +#[doc = "count matches the pattern, so it takes the pattern's minimum"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] +pub struct CountedWithPattern { + pub count: u64, +} +impl CountedWithPattern { + pub fn builder() -> builder::CountedWithPattern { + Default::default() + } +} +#[doc = "`ExtensionMap`"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] +#[serde(transparent)] +pub struct ExtensionMap(pub ::std::collections::HashMap); +impl ::std::ops::Deref for ExtensionMap { + type Target = ::std::collections::HashMap; + fn deref(&self) -> &::std::collections::HashMap { + &self.0 + } +} +impl ::std::convert::From + for ::std::collections::HashMap +{ + fn from(value: ExtensionMap) -> Self { + value.0 + } +} +impl ::std::convert::From<::std::collections::HashMap> + for ExtensionMap +{ + fn from(value: ::std::collections::HashMap) -> Self { + Self(value) + } +} +#[doc = "`ExtensionMapKey`"] +#[derive(:: serde :: Serialize, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[serde(transparent)] +pub struct ExtensionMapKey(::std::string::String); +impl ::std::ops::Deref for ExtensionMapKey { + type Target = ::std::string::String; + fn deref(&self) -> &::std::string::String { + &self.0 + } +} +impl ::std::convert::From for ::std::string::String { + fn from(value: ExtensionMapKey) -> Self { + value.0 + } +} +impl ::std::str::FromStr for ExtensionMapKey { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + ::std::convert::TryFrom::try_from(value) + } +} +impl ::std::convert::TryFrom<&str> for ExtensionMapKey { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + static PATTERN: ::std::sync::LazyLock<::regress::Regex> = + ::std::sync::LazyLock::new(|| ::regress::Regex::with_flags("^x-", "u").unwrap()); + if PATTERN.find(value).is_none() { + return Err("doesn't match pattern \"^x-\"".into()); + } + Ok(Self(value.to_string())) + } +} +impl ::std::convert::TryFrom<::std::string::String> for ExtensionMapKey { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + ::std::convert::TryFrom::try_from(value.as_str()) + } +} +impl<'de> ::serde::Deserialize<'de> for ExtensionMapKey { + fn deserialize(deserializer: D) -> ::std::result::Result + where + D: ::serde::Deserializer<'de>, + { + ::std::convert::TryFrom::try_from(::std::string::String::deserialize(deserializer)?) + .map_err(|e: self::error::ConversionError| { + ::custom(e.to_string()) + }) + } +} +#[doc = "`ExtensionMapValue`"] +#[derive(:: serde :: Serialize, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[serde(transparent)] +pub struct ExtensionMapValue(::std::string::String); +impl ::std::ops::Deref for ExtensionMapValue { + type Target = ::std::string::String; + fn deref(&self) -> &::std::string::String { + &self.0 + } +} +impl ::std::convert::From for ::std::string::String { + fn from(value: ExtensionMapValue) -> Self { + value.0 + } +} +impl ::std::str::FromStr for ExtensionMapValue { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + ::std::convert::TryFrom::try_from(value) + } +} +impl ::std::convert::TryFrom<&str> for ExtensionMapValue { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + if value.chars().count() > 10usize { + return Err("longer than 10 characters".into()); + } + Ok(Self(value.to_string())) + } +} +impl ::std::convert::TryFrom<::std::string::String> for ExtensionMapValue { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + ::std::convert::TryFrom::try_from(value.as_str()) + } +} +impl<'de> ::serde::Deserialize<'de> for ExtensionMapValue { + fn deserialize(deserializer: D) -> ::std::result::Result + where + D: ::serde::Deserializer<'de>, + { + ::std::convert::TryFrom::try_from(::std::string::String::deserialize(deserializer)?) + .map_err(|e: self::error::ConversionError| { + ::custom(e.to_string()) + }) + } +} +#[doc = "`NamesBase`"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug, Default)] +pub struct NamesBase { + #[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")] + pub names: ::std::vec::Vec<::std::string::String>, +} +impl NamesBase { + pub fn builder() -> builder::NamesBase { + Default::default() + } +} +#[doc = "the reduced schema from typify#882"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] +#[serde(transparent)] +pub struct StringMapOverBase( + pub ::std::collections::HashMap, +); +impl ::std::ops::Deref for StringMapOverBase { + type Target = ::std::collections::HashMap; + fn deref(&self) -> &::std::collections::HashMap { + &self.0 + } +} +impl ::std::convert::From + for ::std::collections::HashMap +{ + fn from(value: StringMapOverBase) -> Self { + value.0 + } +} +impl ::std::convert::From<::std::collections::HashMap> + for StringMapOverBase +{ + fn from( + value: ::std::collections::HashMap, + ) -> Self { + Self(value) + } +} +#[doc = "`StringMapOverBaseKey`"] +#[derive(:: serde :: Serialize, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[serde(transparent)] +pub struct StringMapOverBaseKey(::std::string::String); +impl ::std::ops::Deref for StringMapOverBaseKey { + type Target = ::std::string::String; + fn deref(&self) -> &::std::string::String { + &self.0 + } +} +impl ::std::convert::From for ::std::string::String { + fn from(value: StringMapOverBaseKey) -> Self { + value.0 + } +} +impl ::std::str::FromStr for StringMapOverBaseKey { + type Err = self::error::ConversionError; + fn from_str(value: &str) -> ::std::result::Result { + ::std::convert::TryFrom::try_from(value) + } +} +impl ::std::convert::TryFrom<&str> for StringMapOverBaseKey { + type Error = self::error::ConversionError; + fn try_from(value: &str) -> ::std::result::Result { + static PATTERN: ::std::sync::LazyLock<::regress::Regex> = + ::std::sync::LazyLock::new(|| ::regress::Regex::with_flags("^([^.]+)$", "u").unwrap()); + if PATTERN.find(value).is_none() { + return Err("doesn't match pattern \"^([^.]+)$\"".into()); + } + Ok(Self(value.to_string())) + } +} +impl ::std::convert::TryFrom<::std::string::String> for StringMapOverBaseKey { + type Error = self::error::ConversionError; + fn try_from( + value: ::std::string::String, + ) -> ::std::result::Result { + ::std::convert::TryFrom::try_from(value.as_str()) + } +} +impl<'de> ::serde::Deserialize<'de> for StringMapOverBaseKey { + fn deserialize(deserializer: D) -> ::std::result::Result + where + D: ::serde::Deserializer<'de>, + { + ::std::convert::TryFrom::try_from(::std::string::String::deserialize(deserializer)?) + .map_err(|e: self::error::ConversionError| { + ::custom(e.to_string()) + }) + } +} +#[doc = " Types for composing complex structures."] +pub mod builder { + #[derive(Clone, Debug)] + pub struct CountedBase { + count: ::std::result::Result, + } + impl ::std::default::Default for CountedBase { + fn default() -> Self { + Self { + count: Err("no value supplied for count".to_string()), + } + } + } + impl CountedBase { + pub fn count(mut self, value: T) -> Self + where + T: ::std::convert::TryInto, + T::Error: ::std::fmt::Display, + { + self.count = value + .try_into() + .map_err(|e| format!("error converting supplied value for count: {e}")); + self + } + } + impl ::std::convert::TryFrom for super::CountedBase { + type Error = super::error::ConversionError; + fn try_from( + value: CountedBase, + ) -> ::std::result::Result { + Ok(Self { + count: value.count?, + }) + } + } + impl ::std::convert::From for CountedBase { + fn from(value: super::CountedBase) -> Self { + Self { + count: Ok(value.count), + } + } + } + #[derive(Clone, Debug)] + pub struct CountedWithPattern { + count: ::std::result::Result, + } + impl ::std::default::Default for CountedWithPattern { + fn default() -> Self { + Self { + count: Err("no value supplied for count".to_string()), + } + } + } + impl CountedWithPattern { + pub fn count(mut self, value: T) -> Self + where + T: ::std::convert::TryInto, + T::Error: ::std::fmt::Display, + { + self.count = value + .try_into() + .map_err(|e| format!("error converting supplied value for count: {e}")); + self + } + } + impl ::std::convert::TryFrom for super::CountedWithPattern { + type Error = super::error::ConversionError; + fn try_from( + value: CountedWithPattern, + ) -> ::std::result::Result { + Ok(Self { + count: value.count?, + }) + } + } + impl ::std::convert::From for CountedWithPattern { + fn from(value: super::CountedWithPattern) -> Self { + Self { + count: Ok(value.count), + } + } + } + #[derive(Clone, Debug)] + pub struct NamesBase { + names: ::std::result::Result<::std::vec::Vec<::std::string::String>, ::std::string::String>, + } + impl ::std::default::Default for NamesBase { + fn default() -> Self { + Self { + names: Ok(Default::default()), + } + } + } + impl NamesBase { + pub fn names(mut self, value: T) -> Self + where + T: ::std::convert::TryInto<::std::vec::Vec<::std::string::String>>, + T::Error: ::std::fmt::Display, + { + self.names = value + .try_into() + .map_err(|e| format!("error converting supplied value for names: {e}")); + self + } + } + impl ::std::convert::TryFrom for super::NamesBase { + type Error = super::error::ConversionError; + fn try_from( + value: NamesBase, + ) -> ::std::result::Result { + Ok(Self { + names: value.names?, + }) + } + } + impl ::std::convert::From for NamesBase { + fn from(value: super::NamesBase) -> Self { + Self { + names: Ok(value.names), + } + } + } +} +#[doc = " Error types."] +pub mod error { + #[doc = r" Error from a `TryFrom` or `FromStr` implementation."] + pub struct ConversionError(::std::borrow::Cow<'static, str>); + impl ::std::error::Error for ConversionError {} + impl ::std::fmt::Display for ConversionError { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> { + ::std::fmt::Display::fmt(&self.0, f) + } + } + impl ::std::fmt::Debug for ConversionError { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> { + ::std::fmt::Debug::fmt(&self.0, f) + } + } + impl From<&'static str> for ConversionError { + fn from(value: &'static str) -> Self { + Self(value.into()) + } + } + impl From for ConversionError { + fn from(value: String) -> Self { + Self(value.into()) + } + } +} +fn main() {}