diff --git a/lang/rust/avro/src/schema.rs b/lang/rust/avro/src/schema.rs index d911fa74635..e01f11c8f5c 100644 --- a/lang/rust/avro/src/schema.rs +++ b/lang/rust/avro/src/schema.rs @@ -142,13 +142,13 @@ pub enum Schema { #[derive(Clone, Debug, PartialEq)] pub struct MapSchema { pub types: Box, - pub custom_attributes: BTreeMap, + pub attributes: BTreeMap, } #[derive(Clone, Debug, PartialEq)] pub struct ArraySchema { pub items: Box, - pub custom_attributes: BTreeMap, + pub attributes: BTreeMap, } impl PartialEq for Schema { @@ -1101,7 +1101,9 @@ impl Schema { match self { Schema::Record(RecordSchema { attributes, .. }) | Schema::Enum(EnumSchema { attributes, .. }) - | Schema::Fixed(FixedSchema { attributes, .. }) => Some(attributes), + | Schema::Fixed(FixedSchema { attributes, .. }) + | Schema::Array(ArraySchema { attributes, .. }) + | Schema::Map(MapSchema { attributes, .. }) => Some(attributes), _ => None, } } @@ -1146,15 +1148,15 @@ impl Schema { pub fn map(types: Schema) -> Self { Schema::Map(MapSchema { types: Box::new(types), - custom_attributes: Default::default(), + attributes: Default::default(), }) } /// Returns a Schema::Map with the given types and custom attributes. - pub fn map_with_attributes(types: Schema, custom_attributes: BTreeMap) -> Self { + pub fn map_with_attributes(types: Schema, attributes: BTreeMap) -> Self { Schema::Map(MapSchema { types: Box::new(types), - custom_attributes, + attributes, }) } @@ -1162,18 +1164,15 @@ impl Schema { pub fn array(items: Schema) -> Self { Schema::Array(ArraySchema { items: Box::new(items), - custom_attributes: Default::default(), + attributes: Default::default(), }) } /// Returns a Schema::Array with the given items and custom attributes. - pub fn array_with_attributes( - items: Schema, - custom_attributes: BTreeMap, - ) -> Self { + pub fn array_with_attributes(items: Schema, attributes: BTreeMap) -> Self { Schema::Array(ArraySchema { items: Box::new(items), - custom_attributes, + attributes, }) } } @@ -1736,7 +1735,12 @@ impl Parser { .get("items") .ok_or(Error::GetArrayItemsField) .and_then(|items| self.parse(items, enclosing_namespace)) - .map(Schema::array) + .map(|items| { + Schema::array_with_attributes( + items, + self.get_custom_attributes(complex, vec!["items"]), + ) + }) } /// Parse a `serde_json::Value` representing a Avro map type into a @@ -1750,7 +1754,12 @@ impl Parser { .get("values") .ok_or(Error::GetMapValuesField) .and_then(|items| self.parse(items, enclosing_namespace)) - .map(Schema::map) + .map(|items| { + Schema::map_with_attributes( + items, + self.get_custom_attributes(complex, vec!["values"]), + ) + }) } /// Parse a `serde_json::Value` representing a Avro union type into a @@ -1860,19 +1869,19 @@ impl Serialize for Schema { Schema::Bytes => serializer.serialize_str("bytes"), Schema::String => serializer.serialize_str("string"), Schema::Array(ref inner) => { - let mut map = serializer.serialize_map(Some(2 + inner.custom_attributes.len()))?; + let mut map = serializer.serialize_map(Some(2 + inner.attributes.len()))?; map.serialize_entry("type", "array")?; map.serialize_entry("items", &*inner.items.clone())?; - for attr in &inner.custom_attributes { + for attr in &inner.attributes { map.serialize_entry(attr.0, attr.1)?; } map.end() } Schema::Map(ref inner) => { - let mut map = serializer.serialize_map(Some(2 + inner.custom_attributes.len()))?; + let mut map = serializer.serialize_map(Some(2 + inner.attributes.len()))?; map.serialize_entry("type", "map")?; map.serialize_entry("values", &*inner.types.clone())?; - for attr in &inner.custom_attributes { + for attr in &inner.attributes { map.serialize_entry(attr.0, attr.1)?; } map.end() @@ -6533,7 +6542,12 @@ mod tests { r#"{"field-id":"1","items":"long","type":"array"}"#, &serialized ); - assert_eq!(expected, Schema::parse_str(&serialized)?); + let actual_schema = Schema::parse_str(&serialized)?; + assert_eq!(expected, actual_schema); + assert_eq!( + expected.custom_attributes(), + actual_schema.custom_attributes() + ); Ok(()) } @@ -6551,7 +6565,12 @@ mod tests { r#"{"field-id":"1","type":"map","values":"long"}"#, &serialized ); - assert_eq!(expected, Schema::parse_str(&serialized)?); + let actual_schema = Schema::parse_str(&serialized)?; + assert_eq!(expected, actual_schema); + assert_eq!( + expected.custom_attributes(), + actual_schema.custom_attributes() + ); Ok(()) } diff --git a/lang/rust/avro/src/types.rs b/lang/rust/avro/src/types.rs index 97b99a81aca..028c66188c1 100644 --- a/lang/rust/avro/src/types.rs +++ b/lang/rust/avro/src/types.rs @@ -1283,7 +1283,7 @@ mod tests { Value::Array(vec![Value::Boolean(true)]), Schema::array(Schema::Long), false, - "Invalid value: Array([Boolean(true)]) for schema: Array(ArraySchema { items: Long, custom_attributes: {} }). Reason: Unsupported value-schema combination", + "Invalid value: Array([Boolean(true)]) for schema: Array(ArraySchema { items: Long, attributes: {} }). Reason: Unsupported value-schema combination", ), (Value::Record(vec![]), Schema::Null, false, "Invalid value: Record([]) for schema: Null. Reason: Unsupported value-schema combination"), (