Skip to main content

apache_avro/schema/
mod.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Logic for parsing and interacting with schemas in Avro format.
19
20mod builders;
21mod name;
22mod parser;
23mod record;
24mod resolve;
25mod union;
26
27pub(crate) use crate::schema::resolve::{
28    ResolvedOwnedSchema, resolve_names, resolve_names_with_schemata,
29};
30pub use crate::schema::{
31    name::{Alias, Aliases, Name, Names, NamesRef, Namespace, NamespaceRef},
32    record::{RecordField, RecordFieldBuilder, RecordSchema, RecordSchemaBuilder},
33    resolve::ResolvedSchema,
34    union::{UnionSchema, UnionSchemaBuilder},
35};
36use crate::{
37    AvroResult,
38    error::{Details, Error},
39    schema::parser::Parser,
40    schema_equality, types,
41};
42use digest::Digest;
43use serde::{
44    Serialize, Serializer,
45    ser::{SerializeMap, SerializeSeq},
46};
47use serde_json::{Map, Value as JsonValue};
48use std::borrow::Cow;
49use std::fmt::Formatter;
50use std::num::NonZero;
51use std::{
52    collections::{BTreeMap, HashMap, HashSet},
53    fmt,
54    fmt::Debug,
55    hash::Hash,
56    io::Read,
57};
58use strum::{Display, EnumDiscriminants};
59
60/// Represents documentation for complex Avro schemas.
61pub type Documentation = Option<String>;
62
63/// Represents an Avro schema fingerprint.
64///
65/// More information about Avro schema fingerprints can be found in the
66/// [Avro Schema Fingerprint documentation](https://avro.apache.org/docs/++version++/specification/#schema-fingerprints)
67pub struct SchemaFingerprint {
68    pub bytes: Vec<u8>,
69}
70
71impl fmt::Display for SchemaFingerprint {
72    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
73        write!(
74            f,
75            "{}",
76            self.bytes
77                .iter()
78                .map(|byte| format!("{byte:02x}"))
79                .collect::<Vec<String>>()
80                .join("")
81        )
82    }
83}
84
85/// Represents any valid Avro schema
86/// More information about Avro schemas can be found in the
87/// [Avro Specification](https://avro.apache.org/docs/++version++/specification/#schema-declaration)
88#[derive(Clone, Debug, EnumDiscriminants, Display)]
89#[strum_discriminants(name(SchemaKind), derive(Hash, Ord, PartialOrd))]
90pub enum Schema {
91    /// A `null` Avro schema.
92    Null,
93    /// A `boolean` Avro schema.
94    Boolean,
95    /// An `int` Avro schema.
96    Int,
97    /// A `long` Avro schema.
98    Long,
99    /// A `float` Avro schema.
100    Float,
101    /// A `double` Avro schema.
102    Double,
103    /// A `bytes` Avro schema.
104    ///
105    /// `Bytes` represents a sequence of 8-bit unsigned bytes.
106    Bytes,
107    /// A `string` Avro schema.
108    ///
109    /// `String` represents a unicode character sequence.
110    String,
111    /// An `array` Avro schema.
112    ///
113    /// All items will have the same schema.
114    Array(ArraySchema),
115    /// A `map` Avro schema.
116    ///
117    /// Keys are always a `Schema::String` and all values will have the same schema.
118    Map(MapSchema),
119    /// A `union` Avro schema.
120    Union(UnionSchema),
121    /// A `record` Avro schema.
122    Record(RecordSchema),
123    /// An `enum` Avro schema.
124    Enum(EnumSchema),
125    /// A `fixed` Avro schema.
126    Fixed(FixedSchema),
127    /// Logical type which represents `Decimal` values.
128    ///
129    /// The underlying type is serialized and deserialized as `Schema::Bytes` or `Schema::Fixed`.
130    Decimal(DecimalSchema),
131    /// Logical type which represents `Decimal` values without predefined scale.
132    ///
133    /// The underlying type is serialized and deserialized as `Schema::Bytes`
134    BigDecimal,
135    /// A universally unique identifier, annotating a string, bytes or fixed.
136    Uuid(UuidSchema),
137    /// Logical type which represents the number of days since the unix epoch.
138    ///
139    /// Serialization format is `Schema::Int`.
140    Date,
141    /// The time of day in number of milliseconds after midnight.
142    ///
143    /// This type has no reference to any calendar, time zone or date in particular.
144    TimeMillis,
145    /// The time of day in number of microseconds after midnight.
146    ///
147    /// This type has no reference to any calendar, time zone or date in particular.
148    TimeMicros,
149    /// An instant in time represented as the number of milliseconds after the UNIX epoch.
150    TimestampMillis,
151    /// An instant in time represented as the number of microseconds after the UNIX epoch.
152    TimestampMicros,
153    /// An instant in time represented as the number of nanoseconds after the UNIX epoch.
154    TimestampNanos,
155    /// An instant in localtime represented as the number of milliseconds after the UNIX epoch.
156    LocalTimestampMillis,
157    /// An instant in local time represented as the number of microseconds after the UNIX epoch.
158    LocalTimestampMicros,
159    /// An instant in local time represented as the number of nanoseconds after the UNIX epoch.
160    LocalTimestampNanos,
161    /// An amount of time defined by a number of months, days and milliseconds.
162    Duration(FixedSchema),
163    /// A reference to another schema.
164    Ref { name: Name },
165}
166
167#[derive(Clone, PartialEq)]
168pub struct MapSchema {
169    pub types: Box<Schema>,
170    pub attributes: BTreeMap<String, JsonValue>,
171}
172
173impl Debug for MapSchema {
174    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
175        let mut debug = f.debug_struct("MapSchema");
176        debug.field("types", &self.types);
177        if !self.attributes.is_empty() {
178            debug.field("attributes", &self.attributes);
179        }
180        if self.attributes.is_empty() {
181            debug.finish_non_exhaustive()
182        } else {
183            debug.finish()
184        }
185    }
186}
187
188#[derive(Clone, PartialEq)]
189pub struct ArraySchema {
190    pub items: Box<Schema>,
191    pub attributes: BTreeMap<String, JsonValue>,
192}
193
194impl Debug for ArraySchema {
195    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
196        let mut debug = f.debug_struct("ArraySchema");
197        debug.field("items", &self.items);
198        if !self.attributes.is_empty() {
199            debug.field("attributes", &self.attributes);
200        }
201        if self.attributes.is_empty() {
202            debug.finish_non_exhaustive()
203        } else {
204            debug.finish()
205        }
206    }
207}
208
209impl PartialEq for Schema {
210    /// Assess equality of two `Schema` based on [Parsing Canonical Form].
211    ///
212    /// [Parsing Canonical Form]:
213    /// https://avro.apache.org/docs/1.11.1/specification/#parsing-canonical-form-for-schemas
214    fn eq(&self, other: &Self) -> bool {
215        schema_equality::compare_schemata(self, other)
216    }
217}
218
219impl SchemaKind {
220    pub fn is_primitive(self) -> bool {
221        matches!(
222            self,
223            SchemaKind::Null
224                | SchemaKind::Boolean
225                | SchemaKind::Int
226                | SchemaKind::Long
227                | SchemaKind::Double
228                | SchemaKind::Float
229                | SchemaKind::Bytes
230                | SchemaKind::String,
231        )
232    }
233}
234
235impl From<&types::Value> for SchemaKind {
236    fn from(value: &types::Value) -> Self {
237        use crate::types::Value;
238        match value {
239            Value::Null => Self::Null,
240            Value::Boolean(_) => Self::Boolean,
241            Value::Int(_) => Self::Int,
242            Value::Long(_) => Self::Long,
243            Value::Float(_) => Self::Float,
244            Value::Double(_) => Self::Double,
245            Value::Bytes(_) => Self::Bytes,
246            Value::String(_) => Self::String,
247            Value::Array(_) => Self::Array,
248            Value::Map(_) => Self::Map,
249            Value::Union(_, _) => Self::Union,
250            Value::Record(_) => Self::Record,
251            Value::Enum(_, _) => Self::Enum,
252            Value::Fixed(_, _) => Self::Fixed,
253            Value::Decimal { .. } => Self::Decimal,
254            Value::BigDecimal(_) => Self::BigDecimal,
255            Value::Uuid(_) => Self::Uuid,
256            Value::Date(_) => Self::Date,
257            Value::TimeMillis(_) => Self::TimeMillis,
258            Value::TimeMicros(_) => Self::TimeMicros,
259            Value::TimestampMillis(_) => Self::TimestampMillis,
260            Value::TimestampMicros(_) => Self::TimestampMicros,
261            Value::TimestampNanos(_) => Self::TimestampNanos,
262            Value::LocalTimestampMillis(_) => Self::LocalTimestampMillis,
263            Value::LocalTimestampMicros(_) => Self::LocalTimestampMicros,
264            Value::LocalTimestampNanos(_) => Self::LocalTimestampNanos,
265            Value::Duration { .. } => Self::Duration,
266        }
267    }
268}
269
270/// A description of an Enum schema.
271#[derive(bon::Builder, Clone)]
272pub struct EnumSchema {
273    /// The name of the schema
274    pub name: Name,
275    /// The aliases of the schema
276    #[builder(default)]
277    pub aliases: Aliases,
278    /// The documentation of the schema
279    #[builder(default)]
280    pub doc: Documentation,
281    /// The set of symbols of the schema
282    pub symbols: Vec<String>,
283    /// An optional default symbol used for compatibility
284    pub default: Option<String>,
285    /// The custom attributes of the schema
286    #[builder(default = BTreeMap::new())]
287    pub attributes: BTreeMap<String, JsonValue>,
288}
289
290impl Debug for EnumSchema {
291    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
292        let mut debug = f.debug_struct("EnumSchema");
293        debug.field("name", &self.name);
294        if let Some(aliases) = &self.aliases {
295            debug.field("aliases", aliases);
296        }
297        if let Some(doc) = &self.doc {
298            debug.field("doc", doc);
299        }
300        debug.field("symbols", &self.symbols);
301        if let Some(default) = &self.default {
302            debug.field("default", default);
303        }
304        if !self.attributes.is_empty() {
305            debug.field("attributes", &self.attributes);
306        }
307        if self.aliases.is_none()
308            || self.doc.is_none()
309            || self.default.is_none()
310            || self.attributes.is_empty()
311        {
312            debug.finish_non_exhaustive()
313        } else {
314            debug.finish()
315        }
316    }
317}
318
319/// A description of a Fixed schema.
320#[derive(bon::Builder, Clone)]
321pub struct FixedSchema {
322    /// The name of the schema
323    pub name: Name,
324    /// The aliases of the schema
325    #[builder(default)]
326    pub aliases: Aliases,
327    /// The documentation of the schema
328    #[builder(default)]
329    pub doc: Documentation,
330    /// The size of the fixed schema
331    pub size: usize,
332    /// The custom attributes of the schema
333    #[builder(default = BTreeMap::new())]
334    pub attributes: BTreeMap<String, JsonValue>,
335}
336
337impl Debug for FixedSchema {
338    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
339        let mut debug = f.debug_struct("FixedSchema");
340        debug.field("name", &self.name);
341        if let Some(aliases) = &self.aliases {
342            debug.field("aliases", aliases);
343        }
344        if let Some(doc) = &self.doc {
345            debug.field("doc", doc);
346        }
347        debug.field("size", &self.size);
348        if !self.attributes.is_empty() {
349            debug.field("attributes", &self.attributes);
350        }
351        if self.aliases.is_none() || self.doc.is_none() || !self.attributes.is_empty() {
352            debug.finish_non_exhaustive()
353        } else {
354            debug.finish()
355        }
356    }
357}
358
359impl FixedSchema {
360    fn serialize_to_map<S>(&self, mut map: S::SerializeMap) -> Result<S::SerializeMap, S::Error>
361    where
362        S: Serializer,
363    {
364        map.serialize_entry("type", "fixed")?;
365        if let Some(n) = self.name.namespace() {
366            map.serialize_entry("namespace", n)?;
367        }
368        map.serialize_entry("name", &self.name.name())?;
369        if let Some(docstr) = self.doc.as_ref() {
370            map.serialize_entry("doc", docstr)?;
371        }
372        map.serialize_entry("size", &self.size)?;
373
374        if let Some(aliases) = self.aliases.as_ref() {
375            map.serialize_entry("aliases", aliases)?;
376        }
377
378        for attr in &self.attributes {
379            map.serialize_entry(attr.0, attr.1)?;
380        }
381
382        Ok(map)
383    }
384
385    /// Create a new `FixedSchema` copying only the size.
386    ///
387    /// All other fields are `None` or empty.
388    pub(crate) fn copy_only_size(&self) -> Self {
389        Self {
390            name: Name::invalid_empty_name(),
391            aliases: None,
392            doc: None,
393            size: self.size,
394            attributes: Default::default(),
395        }
396    }
397}
398
399/// A description of a Decimal schema.
400///
401/// `scale` defaults to 0 and is an integer greater than or equal to 0 and `precision` is an
402/// integer greater than 0.
403#[derive(Debug, Clone)]
404pub struct DecimalSchema {
405    /// The number of digits in the unscaled value
406    pub precision: Precision,
407    /// The number of digits to the right of the decimal point
408    pub scale: Scale,
409    /// The inner schema of the decimal (fixed or bytes)
410    pub inner: InnerDecimalSchema,
411}
412
413/// The inner schema of the Decimal type.
414#[derive(Debug, Clone)]
415pub enum InnerDecimalSchema {
416    Bytes,
417    Fixed(FixedSchema),
418}
419
420impl TryFrom<Schema> for InnerDecimalSchema {
421    type Error = Error;
422
423    fn try_from(value: Schema) -> Result<Self, Self::Error> {
424        match value {
425            Schema::Bytes => Ok(InnerDecimalSchema::Bytes),
426            Schema::Fixed(fixed) => Ok(InnerDecimalSchema::Fixed(fixed)),
427            _ => Err(Details::ResolveDecimalSchema(value.into()).into()),
428        }
429    }
430}
431
432/// The inner schema of the Uuid type.
433#[derive(Debug, Clone)]
434pub enum UuidSchema {
435    /// [`Schema::Bytes`] with size of 16.
436    ///
437    /// This is not according to specification, but was what happened in `0.21.0` and earlier when
438    /// a schema with logical type `uuid` and inner type `fixed` was used.
439    Bytes,
440    /// [`Schema::String`].
441    String,
442    /// [`Schema::Fixed`] with size of 16.
443    Fixed(FixedSchema),
444}
445
446pub(crate) type Precision = NonZero<usize>;
447pub(crate) type Scale = usize;
448
449impl Schema {
450    /// Converts `self` into its [Parsing Canonical Form].
451    ///
452    /// [Parsing Canonical Form]:
453    /// https://avro.apache.org/docs/++version++/specification/#parsing-canonical-form-for-schemas
454    pub fn canonical_form(&self) -> String {
455        let json = serde_json::to_value(self).unwrap_or_else(|e| {
456            unreachable!("Cannot parse Schema from JSON that was just generated: {e}")
457        });
458        let mut defined_names = HashSet::new();
459        parsing_canonical_form(&json, &mut defined_names)
460    }
461
462    /// Returns the [Parsing Canonical Form] of `self` that is self contained (not dependent on
463    /// any definitions in `schemata`)
464    ///
465    /// If you require a self contained schema including `default` and `doc` attributes, see [`denormalize`][Schema::denormalize].
466    ///
467    /// [Parsing Canonical Form]:
468    /// https://avro.apache.org/docs/++version++/specification/#parsing-canonical-form-for-schemas
469    pub fn independent_canonical_form(&self, schemata: &[Schema]) -> Result<String, Error> {
470        let mut this = self.clone();
471        this.denormalize(schemata)?;
472        Ok(this.canonical_form())
473    }
474
475    /// Generate the [fingerprint] of the schema's [Parsing Canonical Form].
476    ///
477    /// # Example
478    /// ```
479    /// use apache_avro::rabin::Rabin;
480    /// use apache_avro::{Schema, Error};
481    /// use md5::Md5;
482    /// use sha2::Sha256;
483    ///
484    /// fn main() -> Result<(), Error> {
485    ///     let raw_schema = r#"
486    ///         {
487    ///             "type": "record",
488    ///             "name": "test",
489    ///             "fields": [
490    ///                 {"name": "a", "type": "long", "default": 42},
491    ///                 {"name": "b", "type": "string"}
492    ///             ]
493    ///         }
494    ///     "#;
495    ///     let schema = Schema::parse_str(raw_schema)?;
496    ///     println!("{}", schema.fingerprint::<Sha256>());
497    ///     println!("{}", schema.fingerprint::<Md5>());
498    ///     println!("{}", schema.fingerprint::<Rabin>());
499    ///     Ok(())
500    /// }
501    /// ```
502    ///
503    /// [Parsing Canonical Form]:
504    /// https://avro.apache.org/docs/++version++/specification/#parsing-canonical-form-for-schemas
505    /// [fingerprint]:
506    /// https://avro.apache.org/docs/++version++/specification/#schema-fingerprints
507    pub fn fingerprint<D: Digest>(&self) -> SchemaFingerprint {
508        let mut d = D::new();
509        d.update(self.canonical_form());
510        SchemaFingerprint {
511            bytes: d.finalize().to_vec(),
512        }
513    }
514
515    /// Create a `Schema` from a string representing a JSON Avro schema.
516    pub fn parse_str(input: &str) -> Result<Schema, Error> {
517        let mut parser = Parser::default();
518        parser.parse_str(input)
519    }
520
521    /// Create an array of `Schema`'s from a list of named JSON Avro schemas (Record, Enum, and
522    /// Fixed).
523    ///
524    /// It is allowed that the schemas have cross-dependencies; these will be resolved
525    /// during parsing.
526    ///
527    /// If two of the input schemas have the same fullname, an Error will be returned.
528    pub fn parse_list(input: impl IntoIterator<Item = impl AsRef<str>>) -> AvroResult<Vec<Schema>> {
529        let input = input.into_iter();
530        let input_len = input.size_hint().0;
531        let mut input_schemas: HashMap<Name, JsonValue> = HashMap::with_capacity(input_len);
532        let mut input_order: Vec<Name> = Vec::with_capacity(input_len);
533        for json in input {
534            let json = json.as_ref();
535            let schema: JsonValue = serde_json::from_str(json).map_err(Details::ParseSchemaJson)?;
536            if let JsonValue::Object(inner) = &schema {
537                // Only clone the values needed for the name parsing, can be a significant time/memory
538                // save on large schemas
539                let mut name_json = Map::with_capacity(2);
540                if let Some(v) = inner.get("name") {
541                    name_json.insert("name".into(), v.clone());
542                }
543                if let Some(v) = inner.get("namespace") {
544                    name_json.insert("namespace".into(), v.clone());
545                }
546                let name = Name::parse(&mut name_json, None)?;
547                let previous_value = input_schemas.insert(name.clone(), schema);
548                if previous_value.is_some() {
549                    return Err(Details::NameCollision(name.fullname(None)).into());
550                }
551                input_order.push(name);
552            } else {
553                return Err(Details::GetNameField.into());
554            }
555        }
556        let mut parser = Parser::new(
557            input_schemas,
558            input_order,
559            HashMap::with_capacity(input_len),
560        );
561        parser.parse_list()
562    }
563
564    /// Create a `Schema` from a string representing a JSON Avro schema,
565    /// along with an array of `Schema`'s from a list of named JSON Avro schemas (Record, Enum, and
566    /// Fixed).
567    ///
568    /// It is allowed that the schemas have cross-dependencies; these will be resolved
569    /// during parsing.
570    ///
571    /// If two of the named input schemas have the same fullname, an Error will be returned.
572    ///
573    /// # Arguments
574    /// * `schema` - the JSON string of the schema to parse
575    /// * `schemata` - a slice of additional schemas that is used to resolve cross-references
576    pub fn parse_str_with_list(
577        schema: &str,
578        schemata: impl IntoIterator<Item = impl AsRef<str>>,
579    ) -> AvroResult<(Schema, Vec<Schema>)> {
580        let schemata = schemata.into_iter();
581        let schemata_len = schemata.size_hint().0;
582        let mut input_schemas: HashMap<Name, JsonValue> = HashMap::with_capacity(schemata_len);
583        let mut input_order: Vec<Name> = Vec::with_capacity(schemata_len);
584        for json in schemata {
585            let json = json.as_ref();
586            let schema: JsonValue = serde_json::from_str(json).map_err(Details::ParseSchemaJson)?;
587            if let JsonValue::Object(inner) = &schema {
588                // Only clone the values needed for the name parsing, can be a significant time/memory
589                // save on large schemas
590                let mut name_json = Map::with_capacity(2);
591                if let Some(v) = inner.get("name") {
592                    name_json.insert("name".into(), v.clone());
593                }
594                if let Some(v) = inner.get("namespace") {
595                    name_json.insert("namespace".into(), v.clone());
596                }
597                let name = Name::parse(&mut name_json, None)?;
598                if let Some(_previous) = input_schemas.insert(name.clone(), schema) {
599                    return Err(Details::NameCollision(name.fullname(None)).into());
600                }
601                input_order.push(name);
602            } else {
603                return Err(Details::GetNameField.into());
604            }
605        }
606        let mut parser = Parser::new(
607            input_schemas,
608            input_order,
609            HashMap::with_capacity(schemata_len),
610        );
611        parser.parse_input_schemas()?;
612
613        let value = serde_json::from_str(schema).map_err(Details::ParseSchemaJson)?;
614        let schema = parser.parse(value, None)?;
615        let schemata = parser.parse_list()?;
616        Ok((schema, schemata))
617    }
618
619    /// Create a `Schema` from a reader which implements [`Read`].
620    pub fn parse_reader(reader: &mut (impl Read + ?Sized)) -> AvroResult<Schema> {
621        let mut buf = String::new();
622        match reader.read_to_string(&mut buf) {
623            Ok(_) => Self::parse_str(&buf),
624            Err(e) => Err(Details::ReadSchemaFromReader(e).into()),
625        }
626    }
627
628    /// Parses an Avro schema from JSON.
629    pub fn parse(value: JsonValue) -> AvroResult<Schema> {
630        let mut parser = Parser::default();
631        parser.parse(value, None)
632    }
633
634    /// Parses an Avro schema from JSON.
635    /// Any `Schema::Ref`s must be known in the `names` map.
636    pub(crate) fn parse_with_names(value: JsonValue, names: Names) -> AvroResult<Schema> {
637        let mut parser = Parser::new(HashMap::with_capacity(1), Vec::with_capacity(1), names);
638        parser.parse(value, None)
639    }
640
641    /// Returns the custom attributes (metadata) if the schema supports them.
642    pub fn custom_attributes(&self) -> Option<&BTreeMap<String, JsonValue>> {
643        match self {
644            Schema::Record(RecordSchema { attributes, .. })
645            | Schema::Enum(EnumSchema { attributes, .. })
646            | Schema::Fixed(FixedSchema { attributes, .. })
647            | Schema::Array(ArraySchema { attributes, .. })
648            | Schema::Map(MapSchema { attributes, .. })
649            | Schema::Decimal(DecimalSchema {
650                inner: InnerDecimalSchema::Fixed(FixedSchema { attributes, .. }),
651                ..
652            })
653            | Schema::Uuid(UuidSchema::Fixed(FixedSchema { attributes, .. })) => Some(attributes),
654            Schema::Duration(FixedSchema { attributes, .. }) => Some(attributes),
655            _ => None,
656        }
657    }
658
659    /// Returns whether the schema represents a named type according to the avro specification
660    pub fn is_named(&self) -> bool {
661        matches!(
662            self,
663            Schema::Ref { .. }
664                | Schema::Record(_)
665                | Schema::Enum(_)
666                | Schema::Fixed(_)
667                | Schema::Decimal(DecimalSchema {
668                    inner: InnerDecimalSchema::Fixed(_),
669                    ..
670                })
671                | Schema::Uuid(UuidSchema::Fixed(_))
672                | Schema::Duration(_)
673        )
674    }
675
676    /// Returns the name of the schema if it has one.
677    pub fn name(&self) -> Option<&Name> {
678        match self {
679            Schema::Ref { name, .. }
680            | Schema::Record(RecordSchema { name, .. })
681            | Schema::Enum(EnumSchema { name, .. })
682            | Schema::Fixed(FixedSchema { name, .. })
683            | Schema::Decimal(DecimalSchema {
684                inner: InnerDecimalSchema::Fixed(FixedSchema { name, .. }),
685                ..
686            })
687            | Schema::Uuid(UuidSchema::Fixed(FixedSchema { name, .. }))
688            | Schema::Duration(FixedSchema { name, .. }) => Some(name),
689            _ => None,
690        }
691    }
692
693    /// Returns the namespace of the schema if it has one.
694    pub fn namespace(&self) -> NamespaceRef<'_> {
695        self.name().and_then(|n| n.namespace())
696    }
697
698    /// Returns the aliases of the schema if it has ones.
699    pub fn aliases(&self) -> Option<&Vec<Alias>> {
700        match self {
701            Schema::Record(RecordSchema { aliases, .. })
702            | Schema::Enum(EnumSchema { aliases, .. })
703            | Schema::Fixed(FixedSchema { aliases, .. })
704            | Schema::Decimal(DecimalSchema {
705                inner: InnerDecimalSchema::Fixed(FixedSchema { aliases, .. }),
706                ..
707            })
708            | Schema::Uuid(UuidSchema::Fixed(FixedSchema { aliases, .. })) => aliases.as_ref(),
709            Schema::Duration(FixedSchema { aliases, .. }) => aliases.as_ref(),
710            _ => None,
711        }
712    }
713
714    /// Returns the doc of the schema if it has one.
715    pub fn doc(&self) -> Option<&String> {
716        match self {
717            Schema::Record(RecordSchema { doc, .. })
718            | Schema::Enum(EnumSchema { doc, .. })
719            | Schema::Fixed(FixedSchema { doc, .. })
720            | Schema::Decimal(DecimalSchema {
721                inner: InnerDecimalSchema::Fixed(FixedSchema { doc, .. }),
722                ..
723            })
724            | Schema::Uuid(UuidSchema::Fixed(FixedSchema { doc, .. })) => doc.as_ref(),
725            Schema::Duration(FixedSchema { doc, .. }) => doc.as_ref(),
726            _ => None,
727        }
728    }
729
730    /// Remove all external references from the schema.
731    ///
732    /// `schemata` must contain all externally referenced schemas.
733    ///
734    /// # Errors
735    /// Will return a [`Details::SchemaResolutionError`] if it fails to find
736    /// a referenced schema. This will put the schema in a partly denormalized state.
737    pub fn denormalize(&mut self, schemata: &[Schema]) -> AvroResult<()> {
738        self.denormalize_inner(schemata, &mut HashSet::new())
739    }
740
741    fn denormalize_inner(
742        &mut self,
743        schemata: &[Schema],
744        defined_names: &mut HashSet<Name>,
745    ) -> AvroResult<()> {
746        // If this name already exists in this schema we can reference it.
747        // This makes the denormalized form as small as possible and prevent infinite loops for recursive types.
748        if let Some(name) = self.name()
749            && defined_names.contains(name)
750        {
751            *self = Schema::Ref { name: name.clone() };
752            return Ok(());
753        }
754        match self {
755            Schema::Ref { name } => {
756                let replacement_schema = schemata
757                    .iter()
758                    .find(|s| s.name().map(|n| *n == *name).unwrap_or(false));
759                if let Some(schema) = replacement_schema {
760                    let mut denorm = schema.clone();
761                    denorm.denormalize_inner(schemata, defined_names)?;
762                    *self = denorm;
763                } else {
764                    return Err(Details::SchemaResolutionError(name.clone()).into());
765                }
766            }
767            Schema::Record(record_schema) => {
768                defined_names.insert(record_schema.name.clone());
769                for field in &mut record_schema.fields {
770                    field.schema.denormalize_inner(schemata, defined_names)?;
771                }
772            }
773            Schema::Array(array_schema) => {
774                array_schema
775                    .items
776                    .denormalize_inner(schemata, defined_names)?;
777            }
778            Schema::Map(map_schema) => {
779                map_schema
780                    .types
781                    .denormalize_inner(schemata, defined_names)?;
782            }
783            Schema::Union(union_schema) => {
784                for schema in &mut union_schema.schemas {
785                    schema.denormalize_inner(schemata, defined_names)?;
786                }
787            }
788            schema if schema.is_named() => {
789                defined_names.insert(schema.name().expect("Schema is named").clone());
790            }
791            _ => (),
792        }
793        Ok(())
794    }
795
796    /// Derive a name for this schema.
797    ///
798    /// The name is a valid schema name and will be unique if the named
799    /// schemas in this schema have unique names.
800    pub(crate) fn unique_normalized_name(&self) -> Cow<'static, str> {
801        match self {
802            Schema::Null => Cow::Borrowed("n"),
803            Schema::Boolean => Cow::Borrowed("B"),
804            Schema::Int => Cow::Borrowed("i"),
805            Schema::Long => Cow::Borrowed("l"),
806            Schema::Float => Cow::Borrowed("f"),
807            Schema::Double => Cow::Borrowed("d"),
808            Schema::Bytes => Cow::Borrowed("b"),
809            Schema::String => Cow::Borrowed("s"),
810            Schema::Array(array) => {
811                Cow::Owned(format!("a_{}", array.items.unique_normalized_name()))
812            }
813            Schema::Map(map) => Cow::Owned(format!("m_{}", map.types.unique_normalized_name())),
814            Schema::Union(union) => {
815                let mut name = format!("u{}", union.schemas.len());
816                for schema in &union.schemas {
817                    name.push('_');
818                    name.push_str(&schema.unique_normalized_name());
819                }
820                Cow::Owned(name)
821            }
822            Schema::BigDecimal => Cow::Borrowed("bd"),
823            Schema::Date => Cow::Borrowed("D"),
824            Schema::TimeMillis => Cow::Borrowed("t"),
825            Schema::TimeMicros => Cow::Borrowed("tm"),
826            Schema::TimestampMillis => Cow::Borrowed("T"),
827            Schema::TimestampMicros => Cow::Borrowed("TM"),
828            Schema::TimestampNanos => Cow::Borrowed("TN"),
829            Schema::LocalTimestampMillis => Cow::Borrowed("L"),
830            Schema::LocalTimestampMicros => Cow::Borrowed("LM"),
831            Schema::LocalTimestampNanos => Cow::Borrowed("LN"),
832            Schema::Decimal(DecimalSchema {
833                inner: InnerDecimalSchema::Bytes,
834                precision,
835                scale,
836            }) => Cow::Owned(format!("db_{precision}_{scale}")),
837            Schema::Uuid(UuidSchema::Bytes) => Cow::Borrowed("ub"),
838            Schema::Uuid(UuidSchema::String) => Cow::Borrowed("us"),
839            Schema::Record(RecordSchema { name, .. })
840            | Schema::Enum(EnumSchema { name, .. })
841            | Schema::Fixed(FixedSchema { name, .. })
842            | Schema::Decimal(DecimalSchema {
843                inner: InnerDecimalSchema::Fixed(FixedSchema { name, .. }),
844                ..
845            })
846            | Schema::Uuid(UuidSchema::Fixed(FixedSchema { name, .. }))
847            | Schema::Duration(FixedSchema { name, .. })
848            | Schema::Ref { name } => {
849                let name: String = name
850                    .to_string()
851                    .replace('_', "__")
852                    .chars()
853                    .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
854                    .collect();
855                Cow::Owned(format!("r{}_{}", name.len(), name))
856            }
857        }
858    }
859}
860
861impl Serialize for Schema {
862    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
863    where
864        S: Serializer,
865    {
866        match &self {
867            Schema::Ref { name } => serializer.serialize_str(&name.fullname(None)),
868            Schema::Null => serializer.serialize_str("null"),
869            Schema::Boolean => serializer.serialize_str("boolean"),
870            Schema::Int => serializer.serialize_str("int"),
871            Schema::Long => serializer.serialize_str("long"),
872            Schema::Float => serializer.serialize_str("float"),
873            Schema::Double => serializer.serialize_str("double"),
874            Schema::Bytes => serializer.serialize_str("bytes"),
875            Schema::String => serializer.serialize_str("string"),
876            Schema::Array(ArraySchema { items, attributes }) => {
877                let mut map = serializer.serialize_map(Some(2 + attributes.len()))?;
878                map.serialize_entry("type", "array")?;
879                map.serialize_entry("items", items)?;
880                for (key, value) in attributes {
881                    map.serialize_entry(key, value)?;
882                }
883                map.end()
884            }
885            Schema::Map(MapSchema { types, attributes }) => {
886                let mut map = serializer.serialize_map(Some(2 + attributes.len()))?;
887                map.serialize_entry("type", "map")?;
888                map.serialize_entry("values", types)?;
889                for (key, value) in attributes {
890                    map.serialize_entry(key, value)?;
891                }
892                map.end()
893            }
894            Schema::Union(inner) => {
895                let variants = inner.variants();
896                let mut seq = serializer.serialize_seq(Some(variants.len()))?;
897                for v in variants {
898                    seq.serialize_element(v)?;
899                }
900                seq.end()
901            }
902            Schema::Record(RecordSchema {
903                name,
904                aliases,
905                doc,
906                fields,
907                attributes,
908                lookup: _lookup,
909            }) => {
910                let mut map = serializer.serialize_map(None)?;
911                map.serialize_entry("type", "record")?;
912                if let Some(ref n) = name.namespace() {
913                    map.serialize_entry("namespace", n)?;
914                }
915                map.serialize_entry("name", &name.name())?;
916                if let Some(docstr) = doc {
917                    map.serialize_entry("doc", docstr)?;
918                }
919                if let Some(aliases) = aliases {
920                    map.serialize_entry("aliases", aliases)?;
921                }
922                map.serialize_entry("fields", fields)?;
923                for attr in attributes {
924                    map.serialize_entry(attr.0, attr.1)?;
925                }
926                map.end()
927            }
928            Schema::Enum(EnumSchema {
929                name,
930                symbols,
931                aliases,
932                attributes,
933                default,
934                doc,
935            }) => {
936                let mut map = serializer.serialize_map(None)?;
937                map.serialize_entry("type", "enum")?;
938                if let Some(ref n) = name.namespace() {
939                    map.serialize_entry("namespace", n)?;
940                }
941                map.serialize_entry("name", &name.name())?;
942                map.serialize_entry("symbols", symbols)?;
943
944                if let Some(aliases) = aliases {
945                    map.serialize_entry("aliases", aliases)?;
946                }
947                if let Some(default) = default {
948                    map.serialize_entry("default", default)?;
949                }
950                if let Some(doc) = doc {
951                    map.serialize_entry("doc", doc)?;
952                }
953                for attr in attributes {
954                    map.serialize_entry(attr.0, attr.1)?;
955                }
956                map.end()
957            }
958            Schema::Fixed(fixed_schema) => {
959                let mut map = serializer.serialize_map(None)?;
960                map = fixed_schema.serialize_to_map::<S>(map)?;
961                map.end()
962            }
963            Schema::Decimal(DecimalSchema {
964                scale,
965                precision,
966                inner,
967            }) => {
968                let mut map = serializer.serialize_map(None)?;
969                match inner {
970                    InnerDecimalSchema::Fixed(fixed_schema) => {
971                        map = fixed_schema.serialize_to_map::<S>(map)?;
972                    }
973                    InnerDecimalSchema::Bytes => {
974                        map.serialize_entry("type", "bytes")?;
975                    }
976                }
977                map.serialize_entry("logicalType", "decimal")?;
978                map.serialize_entry("scale", scale)?;
979                map.serialize_entry("precision", precision)?;
980                map.end()
981            }
982
983            Schema::BigDecimal => {
984                let mut map = serializer.serialize_map(None)?;
985                map.serialize_entry("type", "bytes")?;
986                map.serialize_entry("logicalType", "big-decimal")?;
987                map.end()
988            }
989            Schema::Uuid(inner) => {
990                let mut map = serializer.serialize_map(None)?;
991                match inner {
992                    UuidSchema::Bytes => {
993                        map.serialize_entry("type", "bytes")?;
994                    }
995                    UuidSchema::String => {
996                        map.serialize_entry("type", "string")?;
997                    }
998                    UuidSchema::Fixed(fixed_schema) => {
999                        map = fixed_schema.serialize_to_map::<S>(map)?;
1000                    }
1001                }
1002                map.serialize_entry("logicalType", "uuid")?;
1003                map.end()
1004            }
1005            Schema::Date => {
1006                let mut map = serializer.serialize_map(None)?;
1007                map.serialize_entry("type", "int")?;
1008                map.serialize_entry("logicalType", "date")?;
1009                map.end()
1010            }
1011            Schema::TimeMillis => {
1012                let mut map = serializer.serialize_map(None)?;
1013                map.serialize_entry("type", "int")?;
1014                map.serialize_entry("logicalType", "time-millis")?;
1015                map.end()
1016            }
1017            Schema::TimeMicros => {
1018                let mut map = serializer.serialize_map(None)?;
1019                map.serialize_entry("type", "long")?;
1020                map.serialize_entry("logicalType", "time-micros")?;
1021                map.end()
1022            }
1023            Schema::TimestampMillis => {
1024                let mut map = serializer.serialize_map(None)?;
1025                map.serialize_entry("type", "long")?;
1026                map.serialize_entry("logicalType", "timestamp-millis")?;
1027                map.end()
1028            }
1029            Schema::TimestampMicros => {
1030                let mut map = serializer.serialize_map(None)?;
1031                map.serialize_entry("type", "long")?;
1032                map.serialize_entry("logicalType", "timestamp-micros")?;
1033                map.end()
1034            }
1035            Schema::TimestampNanos => {
1036                let mut map = serializer.serialize_map(None)?;
1037                map.serialize_entry("type", "long")?;
1038                map.serialize_entry("logicalType", "timestamp-nanos")?;
1039                map.end()
1040            }
1041            Schema::LocalTimestampMillis => {
1042                let mut map = serializer.serialize_map(None)?;
1043                map.serialize_entry("type", "long")?;
1044                map.serialize_entry("logicalType", "local-timestamp-millis")?;
1045                map.end()
1046            }
1047            Schema::LocalTimestampMicros => {
1048                let mut map = serializer.serialize_map(None)?;
1049                map.serialize_entry("type", "long")?;
1050                map.serialize_entry("logicalType", "local-timestamp-micros")?;
1051                map.end()
1052            }
1053            Schema::LocalTimestampNanos => {
1054                let mut map = serializer.serialize_map(None)?;
1055                map.serialize_entry("type", "long")?;
1056                map.serialize_entry("logicalType", "local-timestamp-nanos")?;
1057                map.end()
1058            }
1059            Schema::Duration(fixed) => {
1060                let map = serializer.serialize_map(None)?;
1061
1062                let mut map = fixed.serialize_to_map::<S>(map)?;
1063                map.serialize_entry("logicalType", "duration")?;
1064                map.end()
1065            }
1066        }
1067    }
1068}
1069
1070/// Parses a valid Avro schema into [the Parsing Canonical Form].
1071///
1072/// [the Parsing Canonical Form](https://avro.apache.org/docs/++version++/specification/#parsing-canonical-form-for-schemas)
1073fn parsing_canonical_form(schema: &JsonValue, defined_names: &mut HashSet<String>) -> String {
1074    match schema {
1075        JsonValue::Object(map) => pcf_map(map, defined_names),
1076        JsonValue::String(s) => pcf_string(s),
1077        JsonValue::Array(v) => pcf_array(v, defined_names),
1078        json => panic!("got invalid JSON value for canonical form of schema: {json}"),
1079    }
1080}
1081
1082fn pcf_map(schema: &Map<String, JsonValue>, defined_names: &mut HashSet<String>) -> String {
1083    let typ = schema.get("type").and_then(|v| v.as_str());
1084    let name = if is_named_type(typ) {
1085        let ns = schema.get("namespace").and_then(|v| v.as_str());
1086        let raw_name = schema.get("name").and_then(|v| v.as_str());
1087        Some(format!(
1088            "{}{}",
1089            ns.map_or("".to_string(), |n| { format!("{n}.") }),
1090            raw_name.unwrap_or_default()
1091        ))
1092    } else {
1093        None
1094    };
1095
1096    //if this is already a defined type, early return
1097    if let Some(ref n) = name {
1098        if defined_names.contains(n) {
1099            return pcf_string(n);
1100        } else {
1101            defined_names.insert(n.clone());
1102        }
1103    }
1104
1105    let mut fields = Vec::new();
1106    for (k, v) in schema {
1107        // Reduce primitive types to their simple form. ([PRIMITIVE] rule)
1108        if schema.len() == 1 && k == "type" {
1109            // Invariant: function is only callable from a valid schema, so this is acceptable.
1110            if let JsonValue::String(s) = v {
1111                return pcf_string(s);
1112            }
1113        }
1114
1115        // Strip out unused fields ([STRIP] rule)
1116        if field_ordering_position(k).is_none()
1117            || k == "default"
1118            || k == "doc"
1119            || k == "aliases"
1120            || k == "logicalType"
1121            || k == "order"
1122        {
1123            continue;
1124        }
1125
1126        // Fully qualify the name, if it isn't already ([FULLNAMES] rule).
1127        if k == "name"
1128            && let Some(ref n) = name
1129        {
1130            fields.push(("name", format!("{}:{}", pcf_string(k), pcf_string(n))));
1131            continue;
1132        }
1133
1134        // Strip off quotes surrounding "size" type, if they exist ([INTEGERS] rule).
1135        if k == "size" || k == "precision" || k == "scale" {
1136            let i = match v.as_str() {
1137                Some(s) => s.parse::<i64>().expect("Only valid schemas are accepted!"),
1138                None => v.as_i64().unwrap(),
1139            };
1140            fields.push((k, format!("{}:{}", pcf_string(k), i)));
1141            continue;
1142        }
1143
1144        // For anything else, recursively process the result.
1145        fields.push((
1146            k,
1147            format!(
1148                "{}:{}",
1149                pcf_string(k),
1150                parsing_canonical_form(v, defined_names)
1151            ),
1152        ));
1153    }
1154
1155    // Sort the fields by their canonical ordering ([ORDER] rule).
1156    fields.sort_unstable_by_key(|(k, _)| field_ordering_position(k).unwrap());
1157    let inter = fields
1158        .into_iter()
1159        .map(|(_, v)| v)
1160        .collect::<Vec<_>>()
1161        .join(",");
1162    format!("{{{inter}}}")
1163}
1164
1165fn is_named_type(typ: Option<&str>) -> bool {
1166    matches!(
1167        typ,
1168        Some("record") | Some("enum") | Some("fixed") | Some("ref")
1169    )
1170}
1171
1172fn pcf_array(arr: &[JsonValue], defined_names: &mut HashSet<String>) -> String {
1173    let inter = arr
1174        .iter()
1175        .map(|a| parsing_canonical_form(a, defined_names))
1176        .collect::<Vec<String>>()
1177        .join(",");
1178    format!("[{inter}]")
1179}
1180
1181fn pcf_string(s: &str) -> String {
1182    format!(r#""{s}""#)
1183}
1184
1185const RESERVED_FIELDS: &[&str] = &[
1186    "name",
1187    "type",
1188    "fields",
1189    "symbols",
1190    "items",
1191    "values",
1192    "size",
1193    "logicalType",
1194    "order",
1195    "doc",
1196    "aliases",
1197    "default",
1198    "precision",
1199    "scale",
1200];
1201
1202// Used to define the ordering and inclusion of fields.
1203fn field_ordering_position(field: &str) -> Option<usize> {
1204    RESERVED_FIELDS
1205        .iter()
1206        .position(|&f| f == field)
1207        .map(|pos| pos + 1)
1208}
1209
1210#[cfg(test)]
1211mod tests {
1212    use super::*;
1213    use crate::util::{DEFAULT_MAX_ALLOCATION_BYTES, max_allocation_bytes};
1214    use crate::writer::datum::GenericDatumWriter;
1215    use crate::{error::Details, rabin::Rabin, reader::datum::GenericDatumReader};
1216    use apache_avro_test_helper::{
1217        TestResult,
1218        logger::{assert_logged, assert_not_logged},
1219    };
1220    use serde::{Deserialize, Serialize};
1221    use serde_json::json;
1222
1223    #[test]
1224    fn test_invalid_schema() {
1225        assert!(Schema::parse_str("invalid").is_err());
1226    }
1227
1228    #[test]
1229    fn avro_rs_640_test_fixed_size_above_allocation_limit_is_rejected_at_parse() -> TestResult {
1230        // A fixed schema's size is an allocation directive for decoders, so
1231        // parsing must bound it ("schema parsing is safe" is a stated
1232        // contract) instead of letting a hostile schema declare a
1233        // terabyte-scale allocation.
1234        let min_disallowed = max_allocation_bytes(DEFAULT_MAX_ALLOCATION_BYTES) + 1;
1235        let result = Schema::parse_str(&format!(
1236            r#"\{{"type": "fixed", "name": "huge", "size": {min_disallowed}}}"#
1237        ));
1238        assert!(result.is_err(), "expected a parse error, got {result:?}");
1239
1240        // A reasonable size must still parse.
1241        let schema = Schema::parse_str(r#"{"type": "fixed", "name": "ok", "size": 16}"#)?;
1242        assert!(matches!(
1243            schema,
1244            Schema::Fixed(FixedSchema { size: 16, .. })
1245        ));
1246
1247        Ok(())
1248    }
1249
1250    #[test]
1251    fn test_primitive_schema() -> TestResult {
1252        assert_eq!(Schema::Null, Schema::parse_str(r#""null""#)?);
1253        assert_eq!(Schema::Int, Schema::parse_str(r#""int""#)?);
1254        assert_eq!(Schema::Double, Schema::parse_str(r#""double""#)?);
1255        Ok(())
1256    }
1257
1258    #[test]
1259    fn test_array_schema() -> TestResult {
1260        let schema = Schema::parse_str(r#"{"type": "array", "items": "string"}"#)?;
1261        assert_eq!(Schema::array(Schema::String).build(), schema);
1262        Ok(())
1263    }
1264
1265    #[test]
1266    fn test_map_schema() -> TestResult {
1267        let schema = Schema::parse_str(r#"{"type": "map", "values": "double"}"#)?;
1268        assert_eq!(Schema::map(Schema::Double).build(), schema);
1269        Ok(())
1270    }
1271
1272    #[test]
1273    fn test_union_schema() -> TestResult {
1274        let schema = Schema::parse_str(r#"["null", "int"]"#)?;
1275        assert_eq!(
1276            Schema::Union(UnionSchema::new(vec![Schema::Null, Schema::Int])?),
1277            schema
1278        );
1279        Ok(())
1280    }
1281
1282    #[test]
1283    fn test_union_unsupported_schema() {
1284        let schema = Schema::parse_str(r#"["null", ["null", "int"], "string"]"#);
1285        assert!(schema.is_err());
1286    }
1287
1288    #[test]
1289    fn test_multi_union_schema() -> TestResult {
1290        let schema = Schema::parse_str(r#"["null", "int", "float", "string", "bytes"]"#);
1291        assert!(schema.is_ok());
1292        let schema = schema?;
1293        assert_eq!(SchemaKind::from(&schema), SchemaKind::Union);
1294        let union_schema = match schema {
1295            Schema::Union(u) => u,
1296            _ => unreachable!(),
1297        };
1298        assert_eq!(union_schema.variants().len(), 5);
1299        let mut variants = union_schema.variants().iter();
1300        assert_eq!(SchemaKind::from(variants.next().unwrap()), SchemaKind::Null);
1301        assert_eq!(SchemaKind::from(variants.next().unwrap()), SchemaKind::Int);
1302        assert_eq!(
1303            SchemaKind::from(variants.next().unwrap()),
1304            SchemaKind::Float
1305        );
1306        assert_eq!(
1307            SchemaKind::from(variants.next().unwrap()),
1308            SchemaKind::String
1309        );
1310        assert_eq!(
1311            SchemaKind::from(variants.next().unwrap()),
1312            SchemaKind::Bytes
1313        );
1314        assert_eq!(variants.next(), None);
1315
1316        Ok(())
1317    }
1318
1319    // AVRO-3248
1320    #[test]
1321    fn test_union_of_records() -> TestResult {
1322        // A and B are the same except the name.
1323        let schema_str_a = r#"{
1324            "name": "A",
1325            "type": "record",
1326            "fields": [
1327                {"name": "field_one", "type": "float"}
1328            ]
1329        }"#;
1330
1331        let schema_str_b = r#"{
1332            "name": "B",
1333            "type": "record",
1334            "fields": [
1335                {"name": "field_one", "type": "float"}
1336            ]
1337        }"#;
1338
1339        // we get Details::GetNameField if we put ["A", "B"] directly here.
1340        let schema_str_c = r#"{
1341            "name": "C",
1342            "type": "record",
1343            "fields": [
1344                {"name": "field_one",  "type": ["A", "B"]}
1345            ]
1346        }"#;
1347
1348        let schema_c = Schema::parse_list([schema_str_a, schema_str_b, schema_str_c])?
1349            .last()
1350            .unwrap()
1351            .clone();
1352
1353        let schema_c_expected = Schema::Record(
1354            RecordSchema::builder()
1355                .try_name("C")?
1356                .fields(vec![
1357                    RecordField::builder()
1358                        .name("field_one".to_string())
1359                        .schema(Schema::Union(UnionSchema::new(vec![
1360                            Schema::Ref {
1361                                name: Name::new("A")?,
1362                            },
1363                            Schema::Ref {
1364                                name: Name::new("B")?,
1365                            },
1366                        ])?))
1367                        .build(),
1368                ])
1369                .build(),
1370        );
1371
1372        assert_eq!(schema_c, schema_c_expected);
1373        Ok(())
1374    }
1375
1376    #[test]
1377    fn avro_rs_104_test_root_union_of_records() -> TestResult {
1378        // A and B are the same except the name.
1379        let schema_str_a = r#"{
1380            "name": "A",
1381            "type": "record",
1382            "fields": [
1383                {"name": "field_one", "type": "float"}
1384            ]
1385        }"#;
1386
1387        let schema_str_b = r#"{
1388            "name": "B",
1389            "type": "record",
1390            "fields": [
1391                {"name": "field_one", "type": "float"}
1392            ]
1393        }"#;
1394
1395        let schema_str_c = r#"["A", "B"]"#;
1396
1397        let (schema_c, schemata) =
1398            Schema::parse_str_with_list(schema_str_c, [schema_str_a, schema_str_b])?;
1399
1400        let schema_a_expected = Schema::Record(RecordSchema {
1401            name: Name::new("A")?,
1402            aliases: None,
1403            doc: None,
1404            fields: vec![
1405                RecordField::builder()
1406                    .name("field_one".to_string())
1407                    .schema(Schema::Float)
1408                    .build(),
1409            ],
1410            lookup: BTreeMap::from_iter(vec![("field_one".to_string(), 0)]),
1411            attributes: Default::default(),
1412        });
1413
1414        let schema_b_expected = Schema::Record(RecordSchema {
1415            name: Name::new("B")?,
1416            aliases: None,
1417            doc: None,
1418            fields: vec![
1419                RecordField::builder()
1420                    .name("field_one".to_string())
1421                    .schema(Schema::Float)
1422                    .build(),
1423            ],
1424            lookup: BTreeMap::from_iter(vec![("field_one".to_string(), 0)]),
1425            attributes: Default::default(),
1426        });
1427
1428        let schema_c_expected = Schema::Union(UnionSchema::new(vec![
1429            Schema::Ref {
1430                name: Name::new("A")?,
1431            },
1432            Schema::Ref {
1433                name: Name::new("B")?,
1434            },
1435        ])?);
1436
1437        assert_eq!(schema_c, schema_c_expected);
1438        assert_eq!(schemata[0], schema_a_expected);
1439        assert_eq!(schemata[1], schema_b_expected);
1440
1441        Ok(())
1442    }
1443
1444    #[test]
1445    fn avro_rs_104_test_root_union_of_records_name_collision() -> TestResult {
1446        // A and B are exactly the same.
1447        let schema_str_a1 = r#"{
1448            "name": "A",
1449            "type": "record",
1450            "fields": [
1451                {"name": "field_one", "type": "float"}
1452            ]
1453        }"#;
1454
1455        let schema_str_a2 = r#"{
1456            "name": "A",
1457            "type": "record",
1458            "fields": [
1459                {"name": "field_one", "type": "float"}
1460            ]
1461        }"#;
1462
1463        let schema_str_c = r#"["A", "A"]"#;
1464
1465        match Schema::parse_str_with_list(schema_str_c, [schema_str_a1, schema_str_a2]) {
1466            Ok(_) => unreachable!("Expected an error that the name is already defined"),
1467            Err(e) => assert_eq!(
1468                e.to_string(),
1469                r#"Two schemas with the same fullname were given: "A""#
1470            ),
1471        }
1472
1473        Ok(())
1474    }
1475
1476    #[test]
1477    fn avro_rs_104_test_root_union_of_records_no_name() -> TestResult {
1478        let schema_str_a = r#"{
1479            "name": "A",
1480            "type": "record",
1481            "fields": [
1482                {"name": "field_one", "type": "float"}
1483            ]
1484        }"#;
1485
1486        // B has no name field.
1487        let schema_str_b = r#"{
1488            "type": "record",
1489            "fields": [
1490                {"name": "field_one", "type": "float"}
1491            ]
1492        }"#;
1493
1494        let schema_str_c = r#"["A", "A"]"#;
1495
1496        match Schema::parse_str_with_list(schema_str_c, [schema_str_a, schema_str_b]) {
1497            Ok(_) => unreachable!("Expected an error that schema_str_b is missing a name field"),
1498            Err(e) => assert_eq!(e.to_string(), "No `name` field"),
1499        }
1500
1501        Ok(())
1502    }
1503
1504    #[test]
1505    fn avro_3584_test_recursion_records() -> TestResult {
1506        // A and B are the same except the name.
1507        let schema_str_a = r#"{
1508            "name": "A",
1509            "type": "record",
1510            "fields": [ {"name": "field_one", "type": "B"} ]
1511        }"#;
1512
1513        let schema_str_b = r#"{
1514            "name": "B",
1515            "type": "record",
1516            "fields": [ {"name": "field_one", "type": "A"} ]
1517        }"#;
1518
1519        let list = Schema::parse_list([schema_str_a, schema_str_b])?;
1520
1521        let schema_a = list.first().unwrap().clone();
1522
1523        match schema_a {
1524            Schema::Record(RecordSchema { fields, .. }) => {
1525                let f1 = fields.first();
1526
1527                let ref_schema = Schema::Ref {
1528                    name: Name::new("B")?,
1529                };
1530                assert_eq!(ref_schema, f1.unwrap().schema);
1531            }
1532            _ => panic!("Expected a record schema!"),
1533        }
1534
1535        Ok(())
1536    }
1537
1538    #[test]
1539    fn test_avro_3248_nullable_record() -> TestResult {
1540        use std::iter::FromIterator;
1541
1542        let schema_str_a = r#"{
1543            "name": "A",
1544            "type": "record",
1545            "fields": [
1546                {"name": "field_one", "type": "float"}
1547            ]
1548        }"#;
1549
1550        // we get Details::GetNameField if we put ["null", "B"] directly here.
1551        let schema_str_option_a = r#"{
1552            "name": "OptionA",
1553            "type": "record",
1554            "fields": [
1555                {"name": "field_one",  "type": ["null", "A"], "default": null}
1556            ]
1557        }"#;
1558
1559        let schema_option_a = Schema::parse_list([schema_str_a, schema_str_option_a])?
1560            .last()
1561            .unwrap()
1562            .clone();
1563
1564        let schema_option_a_expected = Schema::Record(RecordSchema {
1565            name: Name::new("OptionA")?,
1566            aliases: None,
1567            doc: None,
1568            fields: vec![
1569                RecordField::builder()
1570                    .name("field_one".to_string())
1571                    .default(JsonValue::Null)
1572                    .schema(Schema::Union(UnionSchema::new(vec![
1573                        Schema::Null,
1574                        Schema::Ref {
1575                            name: Name::new("A")?,
1576                        },
1577                    ])?))
1578                    .build(),
1579            ],
1580            lookup: BTreeMap::from_iter(vec![("field_one".to_string(), 0)]),
1581            attributes: Default::default(),
1582        });
1583
1584        assert_eq!(schema_option_a, schema_option_a_expected);
1585
1586        Ok(())
1587    }
1588
1589    #[test]
1590    fn test_record_schema() -> TestResult {
1591        let parsed = Schema::parse_str(
1592            r#"
1593            {
1594                "type": "record",
1595                "name": "test",
1596                "fields": [
1597                    {"name": "a", "type": "long", "default": 42},
1598                    {"name": "b", "type": "string"}
1599                ]
1600            }
1601        "#,
1602        )?;
1603
1604        let mut lookup = BTreeMap::new();
1605        lookup.insert("a".to_owned(), 0);
1606        lookup.insert("b".to_owned(), 1);
1607
1608        let expected = Schema::Record(RecordSchema {
1609            name: Name::new("test")?,
1610            aliases: None,
1611            doc: None,
1612            fields: vec![
1613                RecordField::builder()
1614                    .name("a".to_string())
1615                    .default(JsonValue::Number(42i64.into()))
1616                    .schema(Schema::Long)
1617                    .build(),
1618                RecordField::builder()
1619                    .name("b".to_string())
1620                    .schema(Schema::String)
1621                    .build(),
1622            ],
1623            lookup,
1624            attributes: Default::default(),
1625        });
1626
1627        assert_eq!(parsed, expected);
1628
1629        Ok(())
1630    }
1631
1632    #[test]
1633    fn test_avro_3302_record_schema_with_currently_parsing_schema() -> TestResult {
1634        let schema = Schema::parse_str(
1635            r#"
1636            {
1637                "type": "record",
1638                "name": "test",
1639                "fields": [{
1640                    "name": "recordField",
1641                    "type": {
1642                        "type": "record",
1643                        "name": "Node",
1644                        "fields": [
1645                            {"name": "label", "type": "string"},
1646                            {"name": "children", "type": {"type": "array", "items": "Node"}}
1647                        ]
1648                    }
1649                }]
1650            }
1651        "#,
1652        )?;
1653
1654        let mut lookup = BTreeMap::new();
1655        lookup.insert("recordField".to_owned(), 0);
1656
1657        let mut node_lookup = BTreeMap::new();
1658        node_lookup.insert("children".to_owned(), 1);
1659        node_lookup.insert("label".to_owned(), 0);
1660
1661        let expected = Schema::Record(RecordSchema {
1662            name: Name::new("test")?,
1663            aliases: None,
1664            doc: None,
1665            fields: vec![
1666                RecordField::builder()
1667                    .name("recordField".to_string())
1668                    .schema(Schema::Record(RecordSchema {
1669                        name: Name::new("Node")?,
1670                        aliases: None,
1671                        doc: None,
1672                        fields: vec![
1673                            RecordField::builder()
1674                                .name("label".to_string())
1675                                .schema(Schema::String)
1676                                .build(),
1677                            RecordField::builder()
1678                                .name("children".to_string())
1679                                .schema(
1680                                    Schema::array(Schema::Ref {
1681                                        name: Name::new("Node")?,
1682                                    })
1683                                    .build(),
1684                                )
1685                                .build(),
1686                        ],
1687                        lookup: node_lookup,
1688                        attributes: Default::default(),
1689                    }))
1690                    .build(),
1691            ],
1692            lookup,
1693            attributes: Default::default(),
1694        });
1695        assert_eq!(schema, expected);
1696
1697        let canonical_form = &schema.canonical_form();
1698        let expected = r#"{"name":"test","type":"record","fields":[{"name":"recordField","type":{"name":"Node","type":"record","fields":[{"name":"label","type":"string"},{"name":"children","type":{"type":"array","items":"Node"}}]}}]}"#;
1699        assert_eq!(canonical_form, &expected);
1700
1701        Ok(())
1702    }
1703
1704    // https://github.com/flavray/avro-rs/pull/99#issuecomment-1016948451
1705    #[test]
1706    fn test_parsing_of_recursive_type_enum() -> TestResult {
1707        let schema = r#"
1708    {
1709        "type": "record",
1710        "name": "User",
1711        "namespace": "office",
1712        "fields": [
1713            {
1714              "name": "details",
1715              "type": [
1716                {
1717                  "type": "record",
1718                  "name": "Employee",
1719                  "fields": [
1720                    {
1721                      "name": "gender",
1722                      "type": {
1723                        "type": "enum",
1724                        "name": "Gender",
1725                        "symbols": [
1726                          "male",
1727                          "female"
1728                        ]
1729                      },
1730                      "default": "female"
1731                    }
1732                  ]
1733                },
1734                {
1735                  "type": "record",
1736                  "name": "Manager",
1737                  "fields": [
1738                    {
1739                      "name": "gender",
1740                      "type": "Gender"
1741                    }
1742                  ]
1743                }
1744              ]
1745            }
1746          ]
1747        }
1748        "#;
1749
1750        let schema = Schema::parse_str(schema)?;
1751        let schema_str = schema.canonical_form();
1752        let expected = r#"{"name":"office.User","type":"record","fields":[{"name":"details","type":[{"name":"office.Employee","type":"record","fields":[{"name":"gender","type":{"name":"office.Gender","type":"enum","symbols":["male","female"]}}]},{"name":"office.Manager","type":"record","fields":[{"name":"gender","type":"office.Gender"}]}]}]}"#;
1753        assert_eq!(schema_str, expected);
1754
1755        Ok(())
1756    }
1757
1758    #[test]
1759    fn test_parsing_of_recursive_type_fixed() -> TestResult {
1760        let schema = r#"
1761    {
1762        "type": "record",
1763        "name": "User",
1764        "namespace": "office",
1765        "fields": [
1766            {
1767              "name": "details",
1768              "type": [
1769                {
1770                  "type": "record",
1771                  "name": "Employee",
1772                  "fields": [
1773                    {
1774                      "name": "id",
1775                      "type": {
1776                        "type": "fixed",
1777                        "name": "EmployeeId",
1778                        "size": 16
1779                      },
1780                      "default": "0123456789abcdef"
1781                    }
1782                  ]
1783                },
1784                {
1785                  "type": "record",
1786                  "name": "Manager",
1787                  "fields": [
1788                    {
1789                      "name": "id",
1790                      "type": "EmployeeId"
1791                    }
1792                  ]
1793                }
1794              ]
1795            }
1796          ]
1797        }
1798        "#;
1799
1800        let schema = Schema::parse_str(schema)?;
1801        let schema_str = schema.canonical_form();
1802        let expected = r#"{"name":"office.User","type":"record","fields":[{"name":"details","type":[{"name":"office.Employee","type":"record","fields":[{"name":"id","type":{"name":"office.EmployeeId","type":"fixed","size":16}}]},{"name":"office.Manager","type":"record","fields":[{"name":"id","type":"office.EmployeeId"}]}]}]}"#;
1803        assert_eq!(schema_str, expected);
1804
1805        Ok(())
1806    }
1807
1808    #[test]
1809    fn test_avro_3302_record_schema_with_currently_parsing_schema_aliases() -> TestResult {
1810        let schema = Schema::parse_str(
1811            r#"
1812            {
1813              "type": "record",
1814              "name": "LongList",
1815              "aliases": ["LinkedLongs"],
1816              "fields" : [
1817                {"name": "value", "type": "long"},
1818                {"name": "next", "type": ["null", "LinkedLongs"]}
1819              ]
1820            }
1821        "#,
1822        )?;
1823
1824        let mut lookup = BTreeMap::new();
1825        lookup.insert("value".to_owned(), 0);
1826        lookup.insert("next".to_owned(), 1);
1827
1828        let expected = Schema::Record(RecordSchema {
1829            name: Name::new("LongList")?,
1830            aliases: Some(vec![Alias::new("LinkedLongs").unwrap()]),
1831            doc: None,
1832            fields: vec![
1833                RecordField::builder()
1834                    .name("value".to_string())
1835                    .schema(Schema::Long)
1836                    .build(),
1837                RecordField::builder()
1838                    .name("next".to_string())
1839                    .schema(Schema::Union(UnionSchema::new(vec![
1840                        Schema::Null,
1841                        Schema::Ref {
1842                            name: Name::new("LongList")?,
1843                        },
1844                    ])?))
1845                    .build(),
1846            ],
1847            lookup,
1848            attributes: Default::default(),
1849        });
1850        assert_eq!(schema, expected);
1851
1852        let canonical_form = &schema.canonical_form();
1853        let expected = r#"{"name":"LongList","type":"record","fields":[{"name":"value","type":"long"},{"name":"next","type":["null","LongList"]}]}"#;
1854        assert_eq!(canonical_form, &expected);
1855
1856        Ok(())
1857    }
1858
1859    #[test]
1860    fn test_avro_3370_record_schema_with_currently_parsing_schema_named_record() -> TestResult {
1861        let schema = Schema::parse_str(
1862            r#"
1863            {
1864              "type" : "record",
1865              "name" : "record",
1866              "fields" : [
1867                 { "name" : "value", "type" : "long" },
1868                 { "name" : "next", "type" : "record" }
1869             ]
1870            }
1871        "#,
1872        )?;
1873
1874        let mut lookup = BTreeMap::new();
1875        lookup.insert("value".to_owned(), 0);
1876        lookup.insert("next".to_owned(), 1);
1877
1878        let expected = Schema::Record(RecordSchema {
1879            name: Name::new("record")?,
1880            aliases: None,
1881            doc: None,
1882            fields: vec![
1883                RecordField::builder()
1884                    .name("value".to_string())
1885                    .schema(Schema::Long)
1886                    .build(),
1887                RecordField::builder()
1888                    .name("next".to_string())
1889                    .schema(Schema::Ref {
1890                        name: Name::new("record")?,
1891                    })
1892                    .build(),
1893            ],
1894            lookup,
1895            attributes: Default::default(),
1896        });
1897        assert_eq!(schema, expected);
1898
1899        let canonical_form = &schema.canonical_form();
1900        let expected = r#"{"name":"record","type":"record","fields":[{"name":"value","type":"long"},{"name":"next","type":"record"}]}"#;
1901        assert_eq!(canonical_form, &expected);
1902
1903        Ok(())
1904    }
1905
1906    #[test]
1907    fn test_avro_3370_record_schema_with_currently_parsing_schema_named_enum() -> TestResult {
1908        let schema = Schema::parse_str(
1909            r#"
1910            {
1911              "type" : "record",
1912              "name" : "record",
1913              "fields" : [
1914                 {
1915                    "name" : "enum",
1916                    "type": {
1917                        "name" : "enum",
1918                        "type" : "enum",
1919                        "symbols": ["one", "two", "three"]
1920                    }
1921                 },
1922                 { "name" : "next", "type" : "enum" }
1923             ]
1924            }
1925        "#,
1926        )?;
1927
1928        let mut lookup = BTreeMap::new();
1929        lookup.insert("enum".to_owned(), 0);
1930        lookup.insert("next".to_owned(), 1);
1931
1932        let expected = Schema::Record(RecordSchema {
1933            name: Name::new("record")?,
1934            aliases: None,
1935            doc: None,
1936            fields: vec![
1937                RecordField::builder()
1938                    .name("enum".to_string())
1939                    .schema(Schema::Enum(
1940                        EnumSchema::builder()
1941                            .name(Name::new("enum")?)
1942                            .symbols(vec![
1943                                "one".to_string(),
1944                                "two".to_string(),
1945                                "three".to_string(),
1946                            ])
1947                            .build(),
1948                    ))
1949                    .build(),
1950                RecordField::builder()
1951                    .name("next".to_string())
1952                    .schema(Schema::Ref {
1953                        name: Name::new("enum")?,
1954                    })
1955                    .build(),
1956            ],
1957            lookup,
1958            attributes: Default::default(),
1959        });
1960        assert_eq!(schema, expected);
1961
1962        let canonical_form = &schema.canonical_form();
1963        let expected = r#"{"name":"record","type":"record","fields":[{"name":"enum","type":{"name":"enum","type":"enum","symbols":["one","two","three"]}},{"name":"next","type":"enum"}]}"#;
1964        assert_eq!(canonical_form, &expected);
1965
1966        Ok(())
1967    }
1968
1969    #[test]
1970    fn test_avro_3370_record_schema_with_currently_parsing_schema_named_fixed() -> TestResult {
1971        let schema = Schema::parse_str(
1972            r#"
1973            {
1974              "type" : "record",
1975              "name" : "record",
1976              "fields" : [
1977                 {
1978                    "name": "fixed",
1979                    "type": {
1980                        "type" : "fixed",
1981                        "name" : "fixed",
1982                        "size": 456
1983                    }
1984                 },
1985                 { "name" : "next", "type" : "fixed" }
1986             ]
1987            }
1988        "#,
1989        )?;
1990
1991        let mut lookup = BTreeMap::new();
1992        lookup.insert("fixed".to_owned(), 0);
1993        lookup.insert("next".to_owned(), 1);
1994
1995        let expected = Schema::Record(RecordSchema {
1996            name: Name::new("record")?,
1997            aliases: None,
1998            doc: None,
1999            fields: vec![
2000                RecordField::builder()
2001                    .name("fixed".to_string())
2002                    .schema(Schema::Fixed(FixedSchema {
2003                        name: Name::new("fixed")?,
2004                        aliases: None,
2005                        doc: None,
2006                        size: 456,
2007                        attributes: Default::default(),
2008                    }))
2009                    .build(),
2010                RecordField::builder()
2011                    .name("next".to_string())
2012                    .schema(Schema::Ref {
2013                        name: Name::new("fixed")?,
2014                    })
2015                    .build(),
2016            ],
2017            lookup,
2018            attributes: Default::default(),
2019        });
2020        assert_eq!(schema, expected);
2021
2022        let canonical_form = &schema.canonical_form();
2023        let expected = r#"{"name":"record","type":"record","fields":[{"name":"fixed","type":{"name":"fixed","type":"fixed","size":456}},{"name":"next","type":"fixed"}]}"#;
2024        assert_eq!(canonical_form, &expected);
2025
2026        Ok(())
2027    }
2028
2029    #[test]
2030    fn test_enum_schema() -> TestResult {
2031        let schema = Schema::parse_str(
2032            r#"{"type": "enum", "name": "Suit", "symbols": ["diamonds", "spades", "clubs", "hearts"]}"#,
2033        )?;
2034
2035        let expected = Schema::Enum(EnumSchema {
2036            name: Name::new("Suit")?,
2037            aliases: None,
2038            doc: None,
2039            symbols: vec![
2040                "diamonds".to_owned(),
2041                "spades".to_owned(),
2042                "clubs".to_owned(),
2043                "hearts".to_owned(),
2044            ],
2045            default: None,
2046            attributes: Default::default(),
2047        });
2048
2049        assert_eq!(expected, schema);
2050
2051        Ok(())
2052    }
2053
2054    #[test]
2055    fn test_enum_schema_duplicate() -> TestResult {
2056        // Duplicate "diamonds"
2057        let schema = Schema::parse_str(
2058            r#"{"type": "enum", "name": "Suit", "symbols": ["diamonds", "spades", "clubs", "diamonds"]}"#,
2059        );
2060        assert!(schema.is_err());
2061
2062        Ok(())
2063    }
2064
2065    #[test]
2066    fn test_enum_schema_name() -> TestResult {
2067        // Invalid name "0000" does not match [A-Za-z_][A-Za-z0-9_]*
2068        let schema = Schema::parse_str(
2069            r#"{"type": "enum", "name": "Enum", "symbols": ["0000", "variant"]}"#,
2070        );
2071        assert!(schema.is_err());
2072
2073        Ok(())
2074    }
2075
2076    #[test]
2077    fn test_fixed_schema() -> TestResult {
2078        let schema = Schema::parse_str(r#"{"type": "fixed", "name": "test", "size": 16}"#)?;
2079
2080        let expected = Schema::Fixed(FixedSchema {
2081            name: Name::new("test")?,
2082            aliases: None,
2083            doc: None,
2084            size: 16_usize,
2085            attributes: Default::default(),
2086        });
2087
2088        assert_eq!(expected, schema);
2089
2090        Ok(())
2091    }
2092
2093    #[test]
2094    fn test_fixed_schema_with_documentation() -> TestResult {
2095        let schema = Schema::parse_str(
2096            r#"{"type": "fixed", "name": "test", "size": 16, "doc": "FixedSchema documentation"}"#,
2097        )?;
2098
2099        let expected = Schema::Fixed(FixedSchema {
2100            name: Name::new("test")?,
2101            aliases: None,
2102            doc: Some(String::from("FixedSchema documentation")),
2103            size: 16_usize,
2104            attributes: Default::default(),
2105        });
2106
2107        assert_eq!(expected, schema);
2108
2109        Ok(())
2110    }
2111
2112    #[test]
2113    fn test_no_documentation() -> TestResult {
2114        let schema = Schema::parse_str(
2115            r#"{"type": "enum", "name": "Coin", "symbols": ["heads", "tails"]}"#,
2116        )?;
2117
2118        let doc = match schema {
2119            Schema::Enum(EnumSchema { doc, .. }) => doc,
2120            _ => unreachable!(),
2121        };
2122
2123        assert!(doc.is_none());
2124
2125        Ok(())
2126    }
2127
2128    #[test]
2129    fn test_documentation() -> TestResult {
2130        let schema = Schema::parse_str(
2131            r#"{"type": "enum", "name": "Coin", "doc": "Some documentation", "symbols": ["heads", "tails"]}"#,
2132        )?;
2133
2134        let doc = match schema {
2135            Schema::Enum(EnumSchema { doc, .. }) => doc,
2136            _ => None,
2137        };
2138
2139        assert_eq!("Some documentation".to_owned(), doc.unwrap());
2140
2141        Ok(())
2142    }
2143
2144    // Tests to ensure Schema is Send + Sync. These tests don't need to _do_ anything, if they can
2145    // compile, they pass.
2146    #[test]
2147    fn test_schema_is_send() {
2148        fn send<S: Send>(_s: S) {}
2149
2150        let schema = Schema::Null;
2151        send(schema);
2152    }
2153
2154    #[test]
2155    fn test_schema_is_sync() {
2156        fn sync<S: Sync>(_s: S) {}
2157
2158        let schema = Schema::Null;
2159        sync(&schema);
2160        sync(schema);
2161    }
2162
2163    #[test]
2164    fn test_schema_fingerprint() -> TestResult {
2165        use crate::rabin::Rabin;
2166        use md5::Md5;
2167        use sha2::Sha256;
2168
2169        let raw_schema = r#"
2170    {
2171        "type": "record",
2172        "name": "test",
2173        "fields": [
2174            {"name": "a", "type": "long", "default": 42},
2175            {"name": "b", "type": "string"},
2176            {"name": "c", "type": {"type": "long", "logicalType": "timestamp-micros"}}
2177        ]
2178    }
2179"#;
2180
2181        let schema = Schema::parse_str(raw_schema)?;
2182        assert_eq!(
2183            "7eb3b28d73dfc99bdd9af1848298b40804a2f8ad5d2642be2ecc2ad34842b987",
2184            format!("{}", schema.fingerprint::<Sha256>())
2185        );
2186
2187        assert_eq!(
2188            "cb11615e412ee5d872620d8df78ff6ae",
2189            format!("{}", schema.fingerprint::<Md5>())
2190        );
2191        assert_eq!(
2192            "92f2ccef718c6754",
2193            format!("{}", schema.fingerprint::<Rabin>())
2194        );
2195
2196        Ok(())
2197    }
2198
2199    #[test]
2200    fn test_canonical_form_strips_field_order() -> TestResult {
2201        // Test vector 026 from the specification's share/test/data/schema-tests.txt
2202        let schema = Schema::parse_str(
2203            r#"{
2204                "fields": [
2205                    {"type": "boolean", "aliases": [], "name": "f1", "default": true},
2206                    {"order": "descending", "name": "f2", "doc": "Hello", "type": "int"}
2207                ],
2208                "type": "record",
2209                "name": "foo"
2210            }"#,
2211        )?;
2212        assert_eq!(
2213            schema.canonical_form(),
2214            r#"{"name":"foo","type":"record","fields":[{"name":"f1","type":"boolean"},{"name":"f2","type":"int"}]}"#
2215        );
2216        // -4860222112080293046 in schema-tests.txt, as little-endian bytes
2217        assert_eq!(
2218            format!("{}", schema.fingerprint::<Rabin>()),
2219            "4a93f457bd058dbc"
2220        );
2221
2222        Ok(())
2223    }
2224
2225    #[test]
2226    fn test_logical_types() -> TestResult {
2227        let schema = Schema::parse_str(r#"{"type": "int", "logicalType": "date"}"#)?;
2228        assert_eq!(schema, Schema::Date);
2229
2230        let schema = Schema::parse_str(r#"{"type": "long", "logicalType": "timestamp-micros"}"#)?;
2231        assert_eq!(schema, Schema::TimestampMicros);
2232
2233        Ok(())
2234    }
2235
2236    #[test]
2237    fn test_nullable_logical_type() -> TestResult {
2238        let schema = Schema::parse_str(
2239            r#"{"type": ["null", {"type": "long", "logicalType": "timestamp-micros"}]}"#,
2240        )?;
2241        assert_eq!(
2242            schema,
2243            Schema::Union(UnionSchema::new(vec![
2244                Schema::Null,
2245                Schema::TimestampMicros,
2246            ])?)
2247        );
2248
2249        Ok(())
2250    }
2251
2252    #[test]
2253    fn test_avro_3374_preserve_namespace_for_primitive() -> TestResult {
2254        let schema = Schema::parse_str(
2255            r#"
2256            {
2257              "type" : "record",
2258              "name" : "ns.int",
2259              "fields" : [
2260                {"name" : "value", "type" : "int"},
2261                {"name" : "next", "type" : [ "null", "ns.int" ]}
2262              ]
2263            }
2264            "#,
2265        )?;
2266
2267        let json = schema.canonical_form();
2268        assert_eq!(
2269            json,
2270            r#"{"name":"ns.int","type":"record","fields":[{"name":"value","type":"int"},{"name":"next","type":["null","ns.int"]}]}"#
2271        );
2272
2273        Ok(())
2274    }
2275
2276    #[test]
2277    fn test_avro_3433_preserve_schema_refs_in_json() -> TestResult {
2278        let schema = r#"
2279    {
2280      "name": "test.test",
2281      "type": "record",
2282      "fields": [
2283        {
2284          "name": "bar",
2285          "type": { "name": "test.foo", "type": "record", "fields": [{ "name": "id", "type": "long" }] }
2286        },
2287        { "name": "baz", "type": "test.foo" }
2288      ]
2289    }
2290    "#;
2291
2292        let schema = Schema::parse_str(schema)?;
2293
2294        let expected = r#"{"name":"test.test","type":"record","fields":[{"name":"bar","type":{"name":"test.foo","type":"record","fields":[{"name":"id","type":"long"}]}},{"name":"baz","type":"test.foo"}]}"#;
2295        assert_eq!(schema.canonical_form(), expected);
2296
2297        Ok(())
2298    }
2299
2300    #[test]
2301    fn test_read_namespace_from_name() -> TestResult {
2302        let schema = r#"
2303    {
2304      "name": "space.name",
2305      "type": "record",
2306      "fields": [
2307        {
2308          "name": "num",
2309          "type": "int"
2310        }
2311      ]
2312    }
2313    "#;
2314
2315        let schema = Schema::parse_str(schema)?;
2316        if let Schema::Record(RecordSchema { name, .. }) = schema {
2317            assert_eq!(name.name(), "name");
2318            assert_eq!(name.namespace(), Some("space"));
2319        } else {
2320            panic!("Expected a record schema!");
2321        }
2322
2323        Ok(())
2324    }
2325
2326    #[test]
2327    fn test_namespace_from_name_has_priority_over_from_field() -> TestResult {
2328        let schema = r#"
2329    {
2330      "name": "space1.name",
2331      "namespace": "space2",
2332      "type": "record",
2333      "fields": [
2334        {
2335          "name": "num",
2336          "type": "int"
2337        }
2338      ]
2339    }
2340    "#;
2341
2342        let schema = Schema::parse_str(schema)?;
2343        if let Schema::Record(RecordSchema { name, .. }) = schema {
2344            assert_eq!(name.namespace(), Some("space1"));
2345        } else {
2346            panic!("Expected a record schema!");
2347        }
2348
2349        Ok(())
2350    }
2351
2352    #[test]
2353    fn test_namespace_from_field() -> TestResult {
2354        let schema = r#"
2355    {
2356      "name": "name",
2357      "namespace": "space2",
2358      "type": "record",
2359      "fields": [
2360        {
2361          "name": "num",
2362          "type": "int"
2363        }
2364      ]
2365    }
2366    "#;
2367
2368        let schema = Schema::parse_str(schema)?;
2369        if let Schema::Record(RecordSchema { name, .. }) = schema {
2370            assert_eq!(name.namespace(), Some("space2"));
2371        } else {
2372            panic!("Expected a record schema!");
2373        }
2374
2375        Ok(())
2376    }
2377
2378    fn assert_avro_3512_aliases(aliases: &Aliases) {
2379        match aliases {
2380            Some(aliases) => {
2381                assert_eq!(aliases.len(), 3);
2382                assert_eq!(aliases[0], Alias::new("space.b").unwrap());
2383                assert_eq!(aliases[1], Alias::new("x.y").unwrap());
2384                assert_eq!(aliases[2], Alias::new(".c").unwrap());
2385            }
2386            None => {
2387                panic!("'aliases' must be Some");
2388            }
2389        }
2390    }
2391
2392    #[test]
2393    fn avro_3512_alias_with_null_namespace_record() -> TestResult {
2394        let schema = Schema::parse_str(
2395            r#"
2396            {
2397              "type": "record",
2398              "name": "a",
2399              "namespace": "space",
2400              "aliases": ["b", "x.y", ".c"],
2401              "fields" : [
2402                {"name": "time", "type": "long"}
2403              ]
2404            }
2405        "#,
2406        )?;
2407
2408        if let Schema::Record(RecordSchema { ref aliases, .. }) = schema {
2409            assert_avro_3512_aliases(aliases);
2410        } else {
2411            panic!("The Schema should be a record: {schema:?}");
2412        }
2413
2414        Ok(())
2415    }
2416
2417    #[test]
2418    fn avro_3512_alias_with_null_namespace_enum() -> TestResult {
2419        let schema = Schema::parse_str(
2420            r#"
2421            {
2422              "type": "enum",
2423              "name": "a",
2424              "namespace": "space",
2425              "aliases": ["b", "x.y", ".c"],
2426              "symbols" : [
2427                "symbol1", "symbol2"
2428              ]
2429            }
2430        "#,
2431        )?;
2432
2433        if let Schema::Enum(EnumSchema { ref aliases, .. }) = schema {
2434            assert_avro_3512_aliases(aliases);
2435        } else {
2436            panic!("The Schema should be an enum: {schema:?}");
2437        }
2438
2439        Ok(())
2440    }
2441
2442    #[test]
2443    fn avro_3512_alias_with_null_namespace_fixed() -> TestResult {
2444        let schema = Schema::parse_str(
2445            r#"
2446            {
2447              "type": "fixed",
2448              "name": "a",
2449              "namespace": "space",
2450              "aliases": ["b", "x.y", ".c"],
2451              "size" : 12
2452            }
2453        "#,
2454        )?;
2455
2456        if let Schema::Fixed(FixedSchema { ref aliases, .. }) = schema {
2457            assert_avro_3512_aliases(aliases);
2458        } else {
2459            panic!("The Schema should be a fixed: {schema:?}");
2460        }
2461
2462        Ok(())
2463    }
2464
2465    #[test]
2466    fn avro_3518_serialize_aliases_record() -> TestResult {
2467        let schema = Schema::parse_str(
2468            r#"
2469            {
2470              "type": "record",
2471              "name": "a",
2472              "namespace": "space",
2473              "aliases": ["b", "x.y", ".c"],
2474              "fields" : [
2475                {
2476                    "name": "time",
2477                    "type": "long",
2478                    "doc": "The documentation is serialized",
2479                    "default": 123,
2480                    "aliases": ["time1", "ns.time2"]
2481                }
2482              ]
2483            }
2484        "#,
2485        )?;
2486
2487        let value = serde_json::to_value(&schema)?;
2488        let serialized = serde_json::to_string(&value)?;
2489        assert_eq!(
2490            r#"{"aliases":["space.b","x.y","c"],"fields":[{"aliases":["time1","ns.time2"],"default":123,"doc":"The documentation is serialized","name":"time","type":"long"}],"name":"a","namespace":"space","type":"record"}"#,
2491            &serialized
2492        );
2493        assert_eq!(schema, Schema::parse_str(&serialized)?);
2494
2495        Ok(())
2496    }
2497
2498    #[test]
2499    fn avro_3518_serialize_aliases_enum() -> TestResult {
2500        let schema = Schema::parse_str(
2501            r#"
2502            {
2503              "type": "enum",
2504              "name": "a",
2505              "namespace": "space",
2506              "aliases": ["b", "x.y", ".c"],
2507              "symbols" : [
2508                "symbol1", "symbol2"
2509              ]
2510            }
2511        "#,
2512        )?;
2513
2514        let value = serde_json::to_value(&schema)?;
2515        let serialized = serde_json::to_string(&value)?;
2516        assert_eq!(
2517            r#"{"aliases":["space.b","x.y","c"],"name":"a","namespace":"space","symbols":["symbol1","symbol2"],"type":"enum"}"#,
2518            &serialized
2519        );
2520        assert_eq!(schema, Schema::parse_str(&serialized)?);
2521
2522        Ok(())
2523    }
2524
2525    #[test]
2526    fn avro_3518_serialize_aliases_fixed() -> TestResult {
2527        let schema = Schema::parse_str(
2528            r#"
2529            {
2530              "type": "fixed",
2531              "name": "a",
2532              "namespace": "space",
2533              "aliases": ["b", "x.y", ".c"],
2534              "size" : 12
2535            }
2536        "#,
2537        )?;
2538
2539        let value = serde_json::to_value(&schema)?;
2540        let serialized = serde_json::to_string(&value)?;
2541        assert_eq!(
2542            r#"{"aliases":["space.b","x.y","c"],"name":"a","namespace":"space","size":12,"type":"fixed"}"#,
2543            &serialized
2544        );
2545        assert_eq!(schema, Schema::parse_str(&serialized)?);
2546
2547        Ok(())
2548    }
2549
2550    #[test]
2551    fn avro_3130_parse_anonymous_union_type() -> TestResult {
2552        let schema_str = r#"
2553        {
2554            "type": "record",
2555            "name": "AccountEvent",
2556            "fields": [
2557                {"type":
2558                  ["null",
2559                   { "name": "accountList",
2560                      "type": {
2561                        "type": "array",
2562                        "items": "long"
2563                      }
2564                  }
2565                  ],
2566                 "name":"NullableLongArray"
2567               }
2568            ]
2569        }
2570        "#;
2571        let schema = Schema::parse_str(schema_str)?;
2572
2573        if let Schema::Record(RecordSchema { name, fields, .. }) = schema {
2574            assert_eq!(name, Name::new("AccountEvent")?);
2575
2576            let field = &fields[0];
2577            assert_eq!(&field.name, "NullableLongArray");
2578
2579            if let Schema::Union(ref union) = field.schema {
2580                assert_eq!(union.schemas[0], Schema::Null);
2581
2582                if let Schema::Array(ref array_schema) = union.schemas[1] {
2583                    if let Schema::Long = *array_schema.items {
2584                        // OK
2585                    } else {
2586                        panic!("Expected a Schema::Array of type Long");
2587                    }
2588                } else {
2589                    panic!("Expected Schema::Array");
2590                }
2591            } else {
2592                panic!("Expected Schema::Union");
2593            }
2594        } else {
2595            panic!("Expected Schema::Record");
2596        }
2597
2598        Ok(())
2599    }
2600
2601    #[test]
2602    fn avro_custom_attributes_schema_without_attributes() -> TestResult {
2603        let schemata_str = [
2604            r#"
2605            {
2606                "type": "record",
2607                "name": "Rec",
2608                "doc": "A Record schema without custom attributes",
2609                "fields": []
2610            }
2611            "#,
2612            r#"
2613            {
2614                "type": "enum",
2615                "name": "Enum",
2616                "doc": "An Enum schema without custom attributes",
2617                "symbols": []
2618            }
2619            "#,
2620            r#"
2621            {
2622                "type": "fixed",
2623                "name": "Fixed",
2624                "doc": "A Fixed schema without custom attributes",
2625                "size": 0
2626            }
2627            "#,
2628        ];
2629        for schema_str in schemata_str.iter() {
2630            let schema = Schema::parse_str(schema_str)?;
2631            assert_eq!(schema.custom_attributes(), Some(&Default::default()));
2632        }
2633
2634        Ok(())
2635    }
2636
2637    const CUSTOM_ATTRS_SUFFIX: &str = r#"
2638            "string_key": "value",
2639            "number_key": 1.23,
2640            "null_key": null,
2641            "array_key": [1, 2, 3],
2642            "object_key": {
2643                "key": "value"
2644            }
2645        "#;
2646
2647    #[test]
2648    fn avro_3609_custom_attributes_schema_with_attributes() -> TestResult {
2649        let schemata_str = [
2650            r#"
2651            {
2652                "type": "record",
2653                "name": "Rec",
2654                "namespace": "ns",
2655                "doc": "A Record schema with custom attributes",
2656                "fields": [],
2657                {{{}}}
2658            }
2659            "#,
2660            r#"
2661            {
2662                "type": "enum",
2663                "name": "Enum",
2664                "namespace": "ns",
2665                "doc": "An Enum schema with custom attributes",
2666                "symbols": [],
2667                {{{}}}
2668            }
2669            "#,
2670            r#"
2671            {
2672                "type": "fixed",
2673                "name": "Fixed",
2674                "namespace": "ns",
2675                "doc": "A Fixed schema with custom attributes",
2676                "size": 2,
2677                {{{}}}
2678            }
2679            "#,
2680        ];
2681
2682        for schema_str in schemata_str.iter() {
2683            let schema = Schema::parse_str(
2684                schema_str
2685                    .to_owned()
2686                    .replace("{{{}}}", CUSTOM_ATTRS_SUFFIX)
2687                    .as_str(),
2688            )?;
2689
2690            assert_eq!(
2691                schema.custom_attributes(),
2692                Some(&expected_custom_attributes())
2693            );
2694        }
2695
2696        Ok(())
2697    }
2698
2699    fn expected_custom_attributes() -> BTreeMap<String, JsonValue> {
2700        let mut expected_attributes: BTreeMap<String, JsonValue> = Default::default();
2701        expected_attributes.insert(
2702            "string_key".to_string(),
2703            JsonValue::String("value".to_string()),
2704        );
2705        expected_attributes.insert("number_key".to_string(), json!(1.23));
2706        expected_attributes.insert("null_key".to_string(), JsonValue::Null);
2707        expected_attributes.insert(
2708            "array_key".to_string(),
2709            JsonValue::Array(vec![json!(1), json!(2), json!(3)]),
2710        );
2711        let mut object_value: HashMap<String, JsonValue> = HashMap::new();
2712        object_value.insert("key".to_string(), JsonValue::String("value".to_string()));
2713        expected_attributes.insert("object_key".to_string(), json!(object_value));
2714        expected_attributes
2715    }
2716
2717    #[test]
2718    fn avro_3609_custom_attributes_record_field_without_attributes() -> TestResult {
2719        let schema_str = String::from(
2720            r#"
2721            {
2722                "type": "record",
2723                "name": "Rec",
2724                "doc": "A Record schema without custom attributes",
2725                "fields": [
2726                    {
2727                        "name": "field_one",
2728                        "type": "float",
2729                        {{{}}}
2730                    }
2731                ]
2732            }
2733        "#,
2734        );
2735
2736        let schema = Schema::parse_str(schema_str.replace("{{{}}}", CUSTOM_ATTRS_SUFFIX).as_str())?;
2737
2738        match schema {
2739            Schema::Record(RecordSchema { name, fields, .. }) => {
2740                assert_eq!(name, Name::new("Rec")?);
2741                assert_eq!(fields.len(), 1);
2742                let field = &fields[0];
2743                assert_eq!(&field.name, "field_one");
2744                assert_eq!(field.custom_attributes, expected_custom_attributes());
2745            }
2746            _ => panic!("Expected Schema::Record"),
2747        }
2748
2749        Ok(())
2750    }
2751
2752    #[test]
2753    fn avro_3625_null_is_first() -> TestResult {
2754        let schema_str = String::from(
2755            r#"
2756            {
2757                "type": "record",
2758                "name": "union_schema_test",
2759                "fields": [
2760                    {"name": "a", "type": ["null", "long"], "default": null}
2761                ]
2762            }
2763        "#,
2764        );
2765
2766        let schema = Schema::parse_str(&schema_str)?;
2767
2768        match schema {
2769            Schema::Record(RecordSchema { name, fields, .. }) => {
2770                assert_eq!(name, Name::new("union_schema_test")?);
2771                assert_eq!(fields.len(), 1);
2772                let field = &fields[0];
2773                assert_eq!(&field.name, "a");
2774                assert_eq!(&field.default, &Some(JsonValue::Null));
2775                match &field.schema {
2776                    Schema::Union(union) => {
2777                        assert_eq!(union.variants().len(), 2);
2778                        assert!(union.is_nullable());
2779                        assert_eq!(union.variants()[0], Schema::Null);
2780                        assert_eq!(union.variants()[1], Schema::Long);
2781                    }
2782                    _ => panic!("Expected Schema::Union"),
2783                }
2784            }
2785            _ => panic!("Expected Schema::Record"),
2786        }
2787
2788        Ok(())
2789    }
2790
2791    #[test]
2792    fn avro_3625_null_is_last() -> TestResult {
2793        let schema_str = String::from(
2794            r#"
2795            {
2796                "type": "record",
2797                "name": "union_schema_test",
2798                "fields": [
2799                    {"name": "a", "type": ["long","null"], "default": 123}
2800                ]
2801            }
2802        "#,
2803        );
2804
2805        let schema = Schema::parse_str(&schema_str)?;
2806
2807        match schema {
2808            Schema::Record(RecordSchema { name, fields, .. }) => {
2809                assert_eq!(name, Name::new("union_schema_test")?);
2810                assert_eq!(fields.len(), 1);
2811                let field = &fields[0];
2812                assert_eq!(&field.name, "a");
2813                assert_eq!(&field.default, &Some(json!(123)));
2814                match &field.schema {
2815                    Schema::Union(union) => {
2816                        assert_eq!(union.variants().len(), 2);
2817                        assert_eq!(union.variants()[0], Schema::Long);
2818                        assert_eq!(union.variants()[1], Schema::Null);
2819                    }
2820                    _ => panic!("Expected Schema::Union"),
2821                }
2822            }
2823            _ => panic!("Expected Schema::Record"),
2824        }
2825
2826        Ok(())
2827    }
2828
2829    #[test]
2830    fn avro_3625_null_is_the_middle() -> TestResult {
2831        let schema_str = String::from(
2832            r#"
2833            {
2834                "type": "record",
2835                "name": "union_schema_test",
2836                "fields": [
2837                    {"name": "a", "type": ["long","null","int"], "default": 123}
2838                ]
2839            }
2840        "#,
2841        );
2842
2843        let schema = Schema::parse_str(&schema_str)?;
2844
2845        match schema {
2846            Schema::Record(RecordSchema { name, fields, .. }) => {
2847                assert_eq!(name, Name::new("union_schema_test")?);
2848                assert_eq!(fields.len(), 1);
2849                let field = &fields[0];
2850                assert_eq!(&field.name, "a");
2851                assert_eq!(&field.default, &Some(json!(123)));
2852                match &field.schema {
2853                    Schema::Union(union) => {
2854                        assert_eq!(union.variants().len(), 3);
2855                        assert_eq!(union.variants()[0], Schema::Long);
2856                        assert_eq!(union.variants()[1], Schema::Null);
2857                        assert_eq!(union.variants()[2], Schema::Int);
2858                    }
2859                    _ => panic!("Expected Schema::Union"),
2860                }
2861            }
2862            _ => panic!("Expected Schema::Record"),
2863        }
2864
2865        Ok(())
2866    }
2867
2868    #[test]
2869    fn avro_3649_default_notintfirst() -> TestResult {
2870        let schema_str = String::from(
2871            r#"
2872            {
2873                "type": "record",
2874                "name": "union_schema_test",
2875                "fields": [
2876                    {"name": "a", "type": ["string", "int"], "default": 123}
2877                ]
2878            }
2879        "#,
2880        );
2881
2882        let schema = Schema::parse_str(&schema_str)?;
2883
2884        match schema {
2885            Schema::Record(RecordSchema { name, fields, .. }) => {
2886                assert_eq!(name, Name::new("union_schema_test")?);
2887                assert_eq!(fields.len(), 1);
2888                let field = &fields[0];
2889                assert_eq!(&field.name, "a");
2890                assert_eq!(&field.default, &Some(json!(123)));
2891                match &field.schema {
2892                    Schema::Union(union) => {
2893                        assert_eq!(union.variants().len(), 2);
2894                        assert_eq!(union.variants()[0], Schema::String);
2895                        assert_eq!(union.variants()[1], Schema::Int);
2896                    }
2897                    _ => panic!("Expected Schema::Union"),
2898                }
2899            }
2900            _ => panic!("Expected Schema::Record"),
2901        }
2902
2903        Ok(())
2904    }
2905
2906    #[test]
2907    fn avro_3709_parsing_of_record_field_aliases() -> TestResult {
2908        let schema = r#"
2909        {
2910          "name": "rec",
2911          "type": "record",
2912          "fields": [
2913            {
2914              "name": "num",
2915              "type": "int",
2916              "aliases": ["num1", "num2"]
2917            }
2918          ]
2919        }
2920        "#;
2921
2922        let schema = Schema::parse_str(schema)?;
2923        if let Schema::Record(RecordSchema { fields, .. }) = schema {
2924            let num_field = &fields[0];
2925            assert_eq!(num_field.name, "num");
2926            assert_eq!(
2927                num_field.aliases,
2928                vec!["num1".to_string(), "num2".to_string()]
2929            );
2930        } else {
2931            panic!("Expected a record schema!");
2932        }
2933
2934        Ok(())
2935    }
2936
2937    #[test]
2938    fn avro_3735_parse_enum_namespace() -> TestResult {
2939        let schema = r#"
2940        {
2941            "type": "record",
2942            "name": "Foo",
2943            "namespace": "name.space",
2944            "fields":
2945            [
2946                {
2947                    "name": "barInit",
2948                    "type":
2949                    {
2950                        "type": "enum",
2951                        "name": "Bar",
2952                        "symbols":
2953                        [
2954                            "bar0",
2955                            "bar1"
2956                        ]
2957                    }
2958                },
2959                {
2960                    "name": "barUse",
2961                    "type": "Bar"
2962                }
2963            ]
2964        }
2965        "#;
2966
2967        #[derive(
2968            Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Clone, serde::Deserialize, serde::Serialize,
2969        )]
2970        pub enum Bar {
2971            #[serde(rename = "bar0")]
2972            Bar0,
2973            #[serde(rename = "bar1")]
2974            Bar1,
2975        }
2976
2977        #[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)]
2978        pub struct Foo {
2979            #[serde(rename = "barInit")]
2980            pub bar_init: Bar,
2981            #[serde(rename = "barUse")]
2982            pub bar_use: Bar,
2983        }
2984
2985        let schema = Schema::parse_str(schema)?;
2986
2987        let foo = Foo {
2988            bar_init: Bar::Bar0,
2989            bar_use: Bar::Bar1,
2990        };
2991
2992        let avro_value = crate::to_value(foo)?;
2993        avro_value.validate(&schema)?;
2994
2995        let mut writer = crate::Writer::new(&schema, Vec::new())?;
2996
2997        // schema validation happens here
2998        writer.append_value(avro_value)?;
2999
3000        Ok(())
3001    }
3002
3003    #[test]
3004    fn avro_3755_deserialize() -> TestResult {
3005        #[derive(
3006            Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Clone, serde::Deserialize, serde::Serialize,
3007        )]
3008        pub enum Bar {
3009            #[serde(rename = "bar0")]
3010            Bar0,
3011            #[serde(rename = "bar1")]
3012            Bar1,
3013            #[serde(rename = "bar2")]
3014            Bar2,
3015        }
3016
3017        #[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)]
3018        pub struct Foo {
3019            #[serde(rename = "barInit")]
3020            pub bar_init: Bar,
3021            #[serde(rename = "barUse")]
3022            pub bar_use: Bar,
3023        }
3024
3025        let writer_schema = r#"{
3026            "type": "record",
3027            "name": "Foo",
3028            "fields":
3029            [
3030                {
3031                    "name": "barInit",
3032                    "type":
3033                    {
3034                        "type": "enum",
3035                        "name": "Bar",
3036                        "symbols":
3037                        [
3038                            "bar0",
3039                            "bar1"
3040                        ]
3041                    }
3042                },
3043                {
3044                    "name": "barUse",
3045                    "type": "Bar"
3046                }
3047            ]
3048            }"#;
3049
3050        let reader_schema = r#"{
3051            "type": "record",
3052            "name": "Foo",
3053            "namespace": "name.space",
3054            "fields":
3055            [
3056                {
3057                    "name": "barInit",
3058                    "type":
3059                    {
3060                        "type": "enum",
3061                        "name": "Bar",
3062                        "symbols":
3063                        [
3064                            "bar0",
3065                            "bar1",
3066                            "bar2"
3067                        ]
3068                    }
3069                },
3070                {
3071                    "name": "barUse",
3072                    "type": "Bar"
3073                }
3074            ]
3075            }"#;
3076
3077        let writer_schema = Schema::parse_str(writer_schema)?;
3078        let foo = Foo {
3079            bar_init: Bar::Bar0,
3080            bar_use: Bar::Bar1,
3081        };
3082        let avro_value = crate::to_value(foo)?;
3083        avro_value.validate(&writer_schema)?;
3084        let datum = GenericDatumWriter::builder(&writer_schema)
3085            .build()?
3086            .write_value_to_vec(avro_value)?;
3087        let mut x = &datum[..];
3088        let reader_schema = Schema::parse_str(reader_schema)?;
3089        let deser_value = GenericDatumReader::builder(&writer_schema)
3090            .reader_schema(&reader_schema)
3091            .build()?
3092            .read_value(&mut x)?;
3093        match deser_value {
3094            types::Value::Record(fields) => {
3095                assert_eq!(fields.len(), 2);
3096                assert_eq!(fields[0].0, "barInit");
3097                assert_eq!(fields[0].1, types::Value::Enum(0, "bar0".to_string()));
3098                assert_eq!(fields[1].0, "barUse");
3099                assert_eq!(fields[1].1, types::Value::Enum(1, "bar1".to_string()));
3100            }
3101            _ => panic!("Expected Value::Record"),
3102        }
3103
3104        Ok(())
3105    }
3106
3107    #[test]
3108    fn test_avro_3780_decimal_schema_type_with_fixed() -> TestResult {
3109        let schema = json!(
3110        {
3111          "type": "record",
3112          "name": "recordWithDecimal",
3113          "fields": [
3114            {
3115                "name": "decimal",
3116                "type": {
3117                    "type": "fixed",
3118                    "name": "nestedFixed",
3119                    "size": 8,
3120                    "logicalType": "decimal",
3121                    "precision": 4
3122                }
3123            }
3124          ]
3125        });
3126
3127        let parse_result = Schema::parse(schema);
3128        assert!(
3129            parse_result.is_ok(),
3130            "parse result must be ok, got: {parse_result:?}"
3131        );
3132
3133        Ok(())
3134    }
3135
3136    #[test]
3137    fn test_avro_3772_enum_default_wrong_type() -> TestResult {
3138        let schema = r#"
3139        {
3140          "type": "record",
3141          "name": "test",
3142          "fields": [
3143            {"name": "a", "type": "long", "default": 42},
3144            {"name": "b", "type": "string"},
3145            {
3146              "name": "c",
3147              "type": {
3148                "type": "enum",
3149                "name": "suit",
3150                "symbols": ["diamonds", "spades", "clubs", "hearts"],
3151                "default": 123
3152              }
3153            }
3154          ]
3155        }
3156        "#;
3157
3158        match Schema::parse_str(schema) {
3159            Err(err) => {
3160                assert_eq!(
3161                    err.to_string(),
3162                    "Default value for an enum must be a string! Got: 123"
3163                );
3164            }
3165            _ => panic!("Expected an error"),
3166        }
3167        Ok(())
3168    }
3169
3170    #[test]
3171    fn test_avro_3812_handle_null_namespace_properly() -> TestResult {
3172        let schema_str = r#"
3173        {
3174          "namespace": "",
3175          "type": "record",
3176          "name": "my_schema",
3177          "fields": [
3178            {
3179              "name": "a",
3180              "type": {
3181                "type": "enum",
3182                "name": "my_enum",
3183                "namespace": "",
3184                "symbols": ["a", "b"]
3185              }
3186            },  {
3187              "name": "b",
3188              "type": {
3189                "type": "fixed",
3190                "name": "my_fixed",
3191                "namespace": "",
3192                "size": 10
3193              }
3194            }
3195          ]
3196         }
3197         "#;
3198
3199        let expected = r#"{"name":"my_schema","type":"record","fields":[{"name":"a","type":{"name":"my_enum","type":"enum","symbols":["a","b"]}},{"name":"b","type":{"name":"my_fixed","type":"fixed","size":10}}]}"#;
3200        let schema = Schema::parse_str(schema_str)?;
3201        let canonical_form = schema.canonical_form();
3202        assert_eq!(canonical_form, expected);
3203
3204        let name = Name::new("my_name")?;
3205        let fullname = name.fullname(Some(""));
3206        assert_eq!(fullname, "my_name");
3207        let qname = name.fully_qualified_name(Some("")).to_string();
3208        assert_eq!(qname, "my_name");
3209
3210        Ok(())
3211    }
3212
3213    #[test]
3214    fn test_avro_3818_inherit_enclosing_namespace() -> TestResult {
3215        // Enclosing namespace is specified but inner namespaces are not.
3216        let schema_str = r#"
3217        {
3218          "namespace": "my_ns",
3219          "type": "record",
3220          "name": "my_schema",
3221          "fields": [
3222            {
3223              "name": "f1",
3224              "type": {
3225                "name": "enum1",
3226                "type": "enum",
3227                "symbols": ["a"]
3228              }
3229            },  {
3230              "name": "f2",
3231              "type": {
3232                "name": "fixed1",
3233                "type": "fixed",
3234                "size": 1
3235              }
3236            }
3237          ]
3238        }
3239        "#;
3240
3241        let expected = r#"{"name":"my_ns.my_schema","type":"record","fields":[{"name":"f1","type":{"name":"my_ns.enum1","type":"enum","symbols":["a"]}},{"name":"f2","type":{"name":"my_ns.fixed1","type":"fixed","size":1}}]}"#;
3242        let schema = Schema::parse_str(schema_str)?;
3243        let canonical_form = schema.canonical_form();
3244        assert_eq!(canonical_form, expected);
3245
3246        // Enclosing namespace and inner namespaces are specified
3247        // but inner namespaces are ""
3248        let schema_str = r#"
3249        {
3250          "namespace": "my_ns",
3251          "type": "record",
3252          "name": "my_schema",
3253          "fields": [
3254            {
3255              "name": "f1",
3256              "type": {
3257                "name": "enum1",
3258                "type": "enum",
3259                "namespace": "",
3260                "symbols": ["a"]
3261              }
3262            },  {
3263              "name": "f2",
3264              "type": {
3265                "name": "fixed1",
3266                "type": "fixed",
3267                "namespace": "",
3268                "size": 1
3269              }
3270            }
3271          ]
3272        }
3273        "#;
3274
3275        let expected = r#"{"name":"my_ns.my_schema","type":"record","fields":[{"name":"f1","type":{"name":"enum1","type":"enum","symbols":["a"]}},{"name":"f2","type":{"name":"fixed1","type":"fixed","size":1}}]}"#;
3276        let schema = Schema::parse_str(schema_str)?;
3277        let canonical_form = schema.canonical_form();
3278        assert_eq!(canonical_form, expected);
3279
3280        // Enclosing namespace is "" and inner non-empty namespaces are specified.
3281        let schema_str = r#"
3282        {
3283          "namespace": "",
3284          "type": "record",
3285          "name": "my_schema",
3286          "fields": [
3287            {
3288              "name": "f1",
3289              "type": {
3290                "name": "enum1",
3291                "type": "enum",
3292                "namespace": "f1.ns",
3293                "symbols": ["a"]
3294              }
3295            },  {
3296              "name": "f2",
3297              "type": {
3298                "name": "f2.ns.fixed1",
3299                "type": "fixed",
3300                "size": 1
3301              }
3302            }
3303          ]
3304        }
3305        "#;
3306
3307        let expected = r#"{"name":"my_schema","type":"record","fields":[{"name":"f1","type":{"name":"f1.ns.enum1","type":"enum","symbols":["a"]}},{"name":"f2","type":{"name":"f2.ns.fixed1","type":"fixed","size":1}}]}"#;
3308        let schema = Schema::parse_str(schema_str)?;
3309        let canonical_form = schema.canonical_form();
3310        assert_eq!(canonical_form, expected);
3311
3312        // Nested complex types with non-empty enclosing namespace.
3313        let schema_str = r#"
3314        {
3315          "type": "record",
3316          "name": "my_ns.my_schema",
3317          "fields": [
3318            {
3319              "name": "f1",
3320              "type": {
3321                "name": "inner_record1",
3322                "type": "record",
3323                "fields": [
3324                  {
3325                    "name": "f1_1",
3326                    "type": {
3327                      "name": "enum1",
3328                      "type": "enum",
3329                      "symbols": ["a"]
3330                    }
3331                  }
3332                ]
3333              }
3334            },  {
3335              "name": "f2",
3336                "type": {
3337                "name": "inner_record2",
3338                "type": "record",
3339                "namespace": "inner_ns",
3340                "fields": [
3341                  {
3342                    "name": "f2_1",
3343                    "type": {
3344                      "name": "enum2",
3345                      "type": "enum",
3346                      "symbols": ["a"]
3347                    }
3348                  }
3349                ]
3350              }
3351            }
3352          ]
3353        }
3354        "#;
3355
3356        let expected = r#"{"name":"my_ns.my_schema","type":"record","fields":[{"name":"f1","type":{"name":"my_ns.inner_record1","type":"record","fields":[{"name":"f1_1","type":{"name":"my_ns.enum1","type":"enum","symbols":["a"]}}]}},{"name":"f2","type":{"name":"inner_ns.inner_record2","type":"record","fields":[{"name":"f2_1","type":{"name":"inner_ns.enum2","type":"enum","symbols":["a"]}}]}}]}"#;
3357        let schema = Schema::parse_str(schema_str)?;
3358        let canonical_form = schema.canonical_form();
3359        assert_eq!(canonical_form, expected);
3360
3361        Ok(())
3362    }
3363
3364    #[test]
3365    fn test_avro_3779_bigdecimal_schema() -> TestResult {
3366        let schema = json!(
3367            {
3368                "name": "decimal",
3369                "type": "bytes",
3370                "logicalType": "big-decimal"
3371            }
3372        );
3373
3374        let parse_result = Schema::parse(schema);
3375        assert!(
3376            parse_result.is_ok(),
3377            "parse result must be ok, got: {parse_result:?}"
3378        );
3379        match parse_result? {
3380            Schema::BigDecimal => (),
3381            other => panic!("Expected Schema::BigDecimal but got: {other:?}"),
3382        }
3383
3384        Ok(())
3385    }
3386
3387    #[test]
3388    fn test_avro_3820_deny_invalid_field_names() -> TestResult {
3389        let schema_str = r#"
3390        {
3391          "name": "my_record",
3392          "type": "record",
3393          "fields": [
3394            {
3395              "name": "f1.x",
3396              "type": {
3397                "name": "my_enum",
3398                "type": "enum",
3399                "symbols": ["a"]
3400              }
3401            },  {
3402              "name": "f2",
3403              "type": {
3404                "name": "my_fixed",
3405                "type": "fixed",
3406                "size": 1
3407              }
3408            }
3409          ]
3410        }
3411        "#;
3412
3413        match Schema::parse_str(schema_str).map_err(Error::into_details) {
3414            Err(Details::FieldName(x)) if x == "f1.x" => Ok(()),
3415            other => Err(format!("Expected Details::FieldName, got {other:?}").into()),
3416        }
3417    }
3418
3419    #[test]
3420    fn test_avro_3827_disallow_duplicate_field_names() -> TestResult {
3421        let schema_str = r#"
3422        {
3423          "name": "my_schema",
3424          "type": "record",
3425          "fields": [
3426            {
3427              "name": "f1",
3428              "type": {
3429                "name": "a",
3430                "type": "record",
3431                "fields": []
3432              }
3433            },  {
3434              "name": "f1",
3435              "type": {
3436                "name": "b",
3437                "type": "record",
3438                "fields": []
3439              }
3440            }
3441          ]
3442        }
3443        "#;
3444
3445        match Schema::parse_str(schema_str).map_err(Error::into_details) {
3446            Err(Details::FieldNameDuplicate(_)) => (),
3447            other => {
3448                return Err(format!("Expected Details::FieldNameDuplicate, got {other:?}").into());
3449            }
3450        };
3451
3452        let schema_str = r#"
3453        {
3454          "name": "my_schema",
3455          "type": "record",
3456          "fields": [
3457            {
3458              "name": "f1",
3459              "type": {
3460                "name": "a",
3461                "type": "record",
3462                "fields": [
3463                  {
3464                    "name": "f1",
3465                    "type": {
3466                      "name": "b",
3467                      "type": "record",
3468                      "fields": []
3469                    }
3470                  }
3471                ]
3472              }
3473            }
3474          ]
3475        }
3476        "#;
3477
3478        let expected = r#"{"name":"my_schema","type":"record","fields":[{"name":"f1","type":{"name":"a","type":"record","fields":[{"name":"f1","type":{"name":"b","type":"record","fields":[]}}]}}]}"#;
3479        let schema = Schema::parse_str(schema_str)?;
3480        let canonical_form = schema.canonical_form();
3481        assert_eq!(canonical_form, expected);
3482
3483        Ok(())
3484    }
3485
3486    #[test]
3487    fn test_avro_3830_null_namespace_in_fully_qualified_names() -> TestResult {
3488        // Check whether all the named types don't refer to the namespace field
3489        // if their name starts with a dot.
3490        let schema_str = r#"
3491        {
3492          "name": ".record1",
3493          "namespace": "ns1",
3494          "type": "record",
3495          "fields": [
3496            {
3497              "name": "f1",
3498              "type": {
3499                "name": ".enum1",
3500                "namespace": "ns2",
3501                "type": "enum",
3502                "symbols": ["a"]
3503              }
3504            },  {
3505              "name": "f2",
3506              "type": {
3507                "name": ".fxed1",
3508                "namespace": "ns3",
3509                "type": "fixed",
3510                "size": 1
3511              }
3512            }
3513          ]
3514        }
3515        "#;
3516
3517        let expected = r#"{"name":"record1","type":"record","fields":[{"name":"f1","type":{"name":"enum1","type":"enum","symbols":["a"]}},{"name":"f2","type":{"name":"fxed1","type":"fixed","size":1}}]}"#;
3518        let schema = Schema::parse_str(schema_str)?;
3519        let canonical_form = schema.canonical_form();
3520        assert_eq!(canonical_form, expected);
3521
3522        // Check whether inner types don't inherit ns1.
3523        let schema_str = r#"
3524        {
3525          "name": ".record1",
3526          "namespace": "ns1",
3527          "type": "record",
3528          "fields": [
3529            {
3530              "name": "f1",
3531              "type": {
3532                "name": "enum1",
3533                "type": "enum",
3534                "symbols": ["a"]
3535              }
3536            },  {
3537              "name": "f2",
3538              "type": {
3539                "name": "fxed1",
3540                "type": "fixed",
3541                "size": 1
3542              }
3543            }
3544          ]
3545        }
3546        "#;
3547
3548        let expected = r#"{"name":"record1","type":"record","fields":[{"name":"f1","type":{"name":"enum1","type":"enum","symbols":["a"]}},{"name":"f2","type":{"name":"fxed1","type":"fixed","size":1}}]}"#;
3549        let schema = Schema::parse_str(schema_str)?;
3550        let canonical_form = schema.canonical_form();
3551        assert_eq!(canonical_form, expected);
3552
3553        let name = Name::new(".my_name")?;
3554        let fullname = name.fullname(None);
3555        assert_eq!(fullname, "my_name");
3556        let qname = name.fully_qualified_name(None).to_string();
3557        assert_eq!(qname, "my_name");
3558
3559        Ok(())
3560    }
3561
3562    #[test]
3563    fn test_avro_3814_schema_resolution_failure() -> TestResult {
3564        // Define a reader schema: a nested record with an optional field.
3565        let reader_schema = json!(
3566            {
3567                "type": "record",
3568                "name": "MyOuterRecord",
3569                "fields": [
3570                    {
3571                        "name": "inner_record",
3572                        "type": [
3573                            "null",
3574                            {
3575                                "type": "record",
3576                                "name": "MyRecord",
3577                                "fields": [
3578                                    {"name": "a", "type": "string"}
3579                                ]
3580                            }
3581                        ],
3582                        "default": null
3583                    }
3584                ]
3585            }
3586        );
3587
3588        // Define a writer schema: a nested record with an optional field, which
3589        // may optionally contain an enum.
3590        let writer_schema = json!(
3591            {
3592                "type": "record",
3593                "name": "MyOuterRecord",
3594                "fields": [
3595                    {
3596                        "name": "inner_record",
3597                        "type": [
3598                            "null",
3599                            {
3600                                "type": "record",
3601                                "name": "MyRecord",
3602                                "fields": [
3603                                    {"name": "a", "type": "string"},
3604                                    {
3605                                        "name": "b",
3606                                        "type": [
3607                                            "null",
3608                                            {
3609                                                "type": "enum",
3610                                                "name": "MyEnum",
3611                                                "symbols": ["A", "B", "C"],
3612                                                "default": "C"
3613                                            }
3614                                        ],
3615                                        "default": null
3616                                    },
3617                                ]
3618                            }
3619                        ]
3620                    }
3621                ],
3622                "default": null
3623            }
3624        );
3625
3626        // Use different structs to represent the "Reader" and the "Writer"
3627        // to mimic two different versions of a producer & consumer application.
3628        #[derive(Serialize, Deserialize, Debug)]
3629        struct MyInnerRecordReader {
3630            a: String,
3631        }
3632
3633        #[derive(Serialize, Deserialize, Debug)]
3634        struct MyRecordReader {
3635            inner_record: Option<MyInnerRecordReader>,
3636        }
3637
3638        #[derive(Serialize, Deserialize, Debug)]
3639        enum MyEnum {
3640            A,
3641            B,
3642            C,
3643        }
3644
3645        #[derive(Serialize, Deserialize, Debug)]
3646        struct MyInnerRecordWriter {
3647            a: String,
3648            b: Option<MyEnum>,
3649        }
3650
3651        #[derive(Serialize, Deserialize, Debug)]
3652        struct MyRecordWriter {
3653            inner_record: Option<MyInnerRecordWriter>,
3654        }
3655
3656        let s = MyRecordWriter {
3657            inner_record: Some(MyInnerRecordWriter {
3658                a: "foo".to_string(),
3659                b: None,
3660            }),
3661        };
3662
3663        // Serialize using the writer schema.
3664        let writer_schema = Schema::parse(writer_schema)?;
3665        let avro_value = crate::to_value(s)?;
3666        avro_value.validate(&writer_schema)?;
3667        let datum = GenericDatumWriter::builder(&writer_schema)
3668            .build()?
3669            .write_value_to_vec(avro_value)?;
3670
3671        // Now, attempt to deserialize using the reader schema.
3672        let reader_schema = Schema::parse(reader_schema)?;
3673        let mut x = &datum[..];
3674
3675        // Deserialization should succeed and we should be able to resolve the schema.
3676        let deser_value = GenericDatumReader::builder(&writer_schema)
3677            .reader_schema(&reader_schema)
3678            .build()?
3679            .read_value(&mut x)?;
3680        deser_value.validate(&reader_schema)?;
3681
3682        // Verify that we can read a field from the record.
3683        let d: MyRecordReader = crate::from_value(&deser_value)?;
3684        assert_eq!(d.inner_record.unwrap().a, "foo".to_string());
3685        Ok(())
3686    }
3687
3688    #[test]
3689    fn test_avro_3837_disallow_invalid_namespace() -> TestResult {
3690        // Valid namespace #1 (Single name portion)
3691        let schema_str = r#"
3692        {
3693          "name": "record1",
3694          "namespace": "ns1",
3695          "type": "record",
3696          "fields": []
3697        }
3698        "#;
3699
3700        let expected = r#"{"name":"ns1.record1","type":"record","fields":[]}"#;
3701        let schema = Schema::parse_str(schema_str)?;
3702        let canonical_form = schema.canonical_form();
3703        assert_eq!(canonical_form, expected);
3704
3705        // Valid namespace #2 (multiple name portions).
3706        let schema_str = r#"
3707        {
3708          "name": "enum1",
3709          "namespace": "ns1.foo.bar",
3710          "type": "enum",
3711          "symbols": ["a"]
3712        }
3713        "#;
3714
3715        let expected = r#"{"name":"ns1.foo.bar.enum1","type":"enum","symbols":["a"]}"#;
3716        let schema = Schema::parse_str(schema_str)?;
3717        let canonical_form = schema.canonical_form();
3718        assert_eq!(canonical_form, expected);
3719
3720        // Invalid namespace #1 (a name portion starts with dot)
3721        let schema_str = r#"
3722        {
3723          "name": "fixed1",
3724          "namespace": ".ns1.a.b",
3725          "type": "fixed",
3726          "size": 1
3727        }
3728        "#;
3729
3730        match Schema::parse_str(schema_str).map_err(Error::into_details) {
3731            Err(Details::InvalidNamespace(_, _)) => (),
3732            other => {
3733                return Err(format!("Expected Details::InvalidNamespace, got {other:?}").into());
3734            }
3735        };
3736
3737        // Invalid namespace #2 (invalid character in a name portion)
3738        let schema_str = r#"
3739        {
3740          "name": "record1",
3741          "namespace": "ns1.a*b.c",
3742          "type": "record",
3743          "fields": []
3744        }
3745        "#;
3746
3747        match Schema::parse_str(schema_str).map_err(Error::into_details) {
3748            Err(Details::InvalidNamespace(_, _)) => (),
3749            other => {
3750                return Err(format!("Expected Details::InvalidNamespace, got {other:?}").into());
3751            }
3752        };
3753
3754        // Invalid namespace #3 (a name portion starts with a digit)
3755        let schema_str = r#"
3756        {
3757          "name": "fixed1",
3758          "namespace": "ns1.1a.b",
3759          "type": "fixed",
3760          "size": 1
3761        }
3762        "#;
3763
3764        match Schema::parse_str(schema_str).map_err(Error::into_details) {
3765            Err(Details::InvalidNamespace(_, _)) => (),
3766            other => {
3767                return Err(format!("Expected Details::InvalidNamespace, got {other:?}").into());
3768            }
3769        };
3770
3771        // Invalid namespace #4 (a name portion is missing - two dots in a row)
3772        let schema_str = r#"
3773        {
3774          "name": "fixed1",
3775          "namespace": "ns1..a",
3776          "type": "fixed",
3777          "size": 1
3778        }
3779        "#;
3780
3781        match Schema::parse_str(schema_str).map_err(Error::into_details) {
3782            Err(Details::InvalidNamespace(_, _)) => (),
3783            other => {
3784                return Err(format!("Expected Details::InvalidNamespace, got {other:?}").into());
3785            }
3786        };
3787
3788        // Invalid namespace #5 (a name portion is missing - ends with a dot)
3789        let schema_str = r#"
3790        {
3791          "name": "fixed1",
3792          "namespace": "ns1.a.",
3793          "type": "fixed",
3794          "size": 1
3795        }
3796        "#;
3797
3798        match Schema::parse_str(schema_str).map_err(Error::into_details) {
3799            Err(Details::InvalidNamespace(_, _)) => (),
3800            other => {
3801                return Err(format!("Expected Details::InvalidNamespace, got {other:?}").into());
3802            }
3803        };
3804
3805        Ok(())
3806    }
3807
3808    #[test]
3809    fn test_avro_3851_validate_default_value_of_simple_record_field() -> TestResult {
3810        let schema_str = r#"
3811        {
3812            "name": "record1",
3813            "namespace": "ns",
3814            "type": "record",
3815            "fields": [
3816                {
3817                    "name": "f1",
3818                    "type": "int",
3819                    "default": "invalid"
3820                }
3821            ]
3822        }
3823        "#;
3824        assert_eq!(
3825            Schema::parse_str(schema_str).unwrap_err().to_string(),
3826            r#"`default`'s value type of field `f1` in `ns.record1` must be a `"int"`. Got: String("invalid")"#
3827        );
3828
3829        Ok(())
3830    }
3831
3832    #[test]
3833    fn test_avro_3851_validate_default_value_of_nested_record_field() -> TestResult {
3834        let schema_str = r#"
3835        {
3836            "name": "record1",
3837            "namespace": "ns",
3838            "type": "record",
3839            "fields": [
3840                {
3841                    "name": "f1",
3842                    "type": {
3843                        "name": "record2",
3844                        "type": "record",
3845                        "fields": [
3846                            {
3847                                "name": "f1_1",
3848                                "type": "int"
3849                            }
3850                        ]
3851                    },
3852                    "default": "invalid"
3853                }
3854            ]
3855        }
3856        "#;
3857        assert_eq!(
3858            Schema::parse_str(schema_str).unwrap_err().to_string(),
3859            r#"`default`'s value type of field `f1` in `ns.record1` must be a `{"name":"ns.record2","type":"record","fields":[{"name":"f1_1","type":"int"}]}`. Got: String("invalid")"#
3860        );
3861
3862        Ok(())
3863    }
3864
3865    #[test]
3866    fn test_avro_3851_validate_default_value_of_enum_record_field() -> TestResult {
3867        let schema_str = r#"
3868        {
3869            "name": "record1",
3870            "namespace": "ns",
3871            "type": "record",
3872            "fields": [
3873                {
3874                    "name": "f1",
3875                    "type": {
3876                        "name": "enum1",
3877                        "type": "enum",
3878                        "symbols": ["a", "b", "c"]
3879                    },
3880                    "default": "invalid"
3881                }
3882            ]
3883        }
3884        "#;
3885        assert_eq!(
3886            Schema::parse_str(schema_str).unwrap_err().to_string(),
3887            r#"`default`'s value type of field `f1` in `ns.record1` must be a `{"name":"ns.enum1","type":"enum","symbols":["a","b","c"]}`. Got: String("invalid")"#
3888        );
3889
3890        Ok(())
3891    }
3892
3893    #[test]
3894    fn test_avro_3851_validate_default_value_of_fixed_record_field() -> TestResult {
3895        let schema_str = r#"
3896        {
3897            "name": "record1",
3898            "namespace": "ns",
3899            "type": "record",
3900            "fields": [
3901                {
3902                    "name": "f1",
3903                    "type": {
3904                        "name": "fixed1",
3905                        "type": "fixed",
3906                        "size": 3
3907                    },
3908                    "default": 100
3909                }
3910            ]
3911        }
3912        "#;
3913        assert_eq!(
3914            Schema::parse_str(schema_str).unwrap_err().to_string(),
3915            r#"`default`'s value type of field `f1` in `ns.record1` must be a `{"name":"ns.fixed1","type":"fixed","size":3}`. Got: Number(100)"#
3916        );
3917
3918        Ok(())
3919    }
3920
3921    #[test]
3922    fn test_avro_3851_validate_default_value_of_array_record_field() -> TestResult {
3923        let schema_str = r#"
3924        {
3925            "name": "record1",
3926            "namespace": "ns",
3927            "type": "record",
3928            "fields": [
3929                {
3930                    "name": "f1",
3931                    "type": {
3932                        "type": "array",
3933                        "items": "int"
3934                    },
3935                    "default": "invalid"
3936                }
3937            ]
3938        }
3939        "#;
3940
3941        let result = Schema::parse_str(schema_str);
3942        assert!(result.is_err());
3943        let err = result
3944            .map_err(|e| e.to_string())
3945            .err()
3946            .unwrap_or_else(|| "unexpected".to_string());
3947        assert_eq!(
3948            r#"`default`'s value type of field `f1` in `ns.record1` must be a `{"type":"array","items":"int"}`. Got: String("invalid")"#,
3949            err
3950        );
3951
3952        Ok(())
3953    }
3954
3955    #[test]
3956    fn test_avro_3851_validate_default_value_of_map_record_field() -> TestResult {
3957        let schema_str = r#"
3958        {
3959            "name": "record1",
3960            "namespace": "ns",
3961            "type": "record",
3962            "fields": [
3963                {
3964                    "name": "f1",
3965                    "type": {
3966                        "type": "map",
3967                        "values": "string"
3968                    },
3969                    "default": "invalid"
3970                }
3971            ]
3972        }
3973        "#;
3974
3975        let result = Schema::parse_str(schema_str);
3976        assert!(result.is_err());
3977        let err = result
3978            .map_err(|e| e.to_string())
3979            .err()
3980            .unwrap_or_else(|| "unexpected".to_string());
3981        assert_eq!(
3982            r#"`default`'s value type of field `f1` in `ns.record1` must be a `{"type":"map","values":"string"}`. Got: String("invalid")"#,
3983            err
3984        );
3985
3986        Ok(())
3987    }
3988
3989    #[test]
3990    fn test_avro_3851_validate_default_value_of_ref_record_field() -> TestResult {
3991        let schema_str = r#"
3992        {
3993            "name": "record1",
3994            "namespace": "ns",
3995            "type": "record",
3996            "fields": [
3997                {
3998                    "name": "f1",
3999                    "type": {
4000                        "name": "record2",
4001                        "type": "record",
4002                        "fields": [
4003                            {
4004                                "name": "f1_1",
4005                                "type": "int"
4006                            }
4007                        ]
4008                    }
4009                },  {
4010                    "name": "f2",
4011                    "type": "ns.record2",
4012                    "default": { "f1_1": true }
4013                }
4014            ]
4015        }
4016        "#;
4017        assert_eq!(
4018            Schema::parse_str(schema_str).unwrap_err().to_string(),
4019            r#"`default`'s value type of field `f2` in `ns.record1` must be a `{"name":"ns.record2","type":"record","fields":[{"name":"f1_1","type":"int"}]}`. Got: Object {"f1_1": Bool(true)}"#
4020        );
4021
4022        Ok(())
4023    }
4024
4025    #[test]
4026    fn test_avro_3851_validate_default_value_of_enum() -> TestResult {
4027        let schema_str = r#"
4028        {
4029            "name": "enum1",
4030            "namespace": "ns",
4031            "type": "enum",
4032            "symbols": ["a", "b", "c"],
4033            "default": 100
4034        }
4035        "#;
4036        let expected = Details::EnumDefaultWrongType(100.into()).to_string();
4037        let result = Schema::parse_str(schema_str);
4038        assert!(result.is_err());
4039        let err = result
4040            .map_err(|e| e.to_string())
4041            .err()
4042            .unwrap_or_else(|| "unexpected".to_string());
4043        assert_eq!(expected, err);
4044
4045        let schema_str = r#"
4046        {
4047            "name": "enum1",
4048            "namespace": "ns",
4049            "type": "enum",
4050            "symbols": ["a", "b", "c"],
4051            "default": "d"
4052        }
4053        "#;
4054        let expected = Details::GetEnumDefault {
4055            symbol: "d".to_string(),
4056            symbols: vec!["a".to_string(), "b".to_string(), "c".to_string()],
4057        }
4058        .to_string();
4059        let result = Schema::parse_str(schema_str);
4060        assert!(result.is_err());
4061        let err = result
4062            .map_err(|e| e.to_string())
4063            .err()
4064            .unwrap_or_else(|| "unexpected".to_string());
4065        assert_eq!(expected, err);
4066
4067        Ok(())
4068    }
4069
4070    #[test]
4071    fn test_avro_3862_get_aliases() -> TestResult {
4072        // Test for Record
4073        let schema_str = r#"
4074        {
4075            "name": "record1",
4076            "namespace": "ns1",
4077            "type": "record",
4078            "aliases": ["r1", "ns2.r2"],
4079            "fields": [
4080                { "name": "f1", "type": "int" },
4081                { "name": "f2", "type": "string" }
4082            ]
4083        }
4084        "#;
4085        let schema = Schema::parse_str(schema_str)?;
4086        let expected = vec![Alias::new("ns1.r1")?, Alias::new("ns2.r2")?];
4087        match schema.aliases() {
4088            Some(aliases) => assert_eq!(aliases, &expected),
4089            None => panic!("Expected Some({expected:?}), got None"),
4090        }
4091
4092        let schema_str = r#"
4093        {
4094            "name": "record1",
4095            "namespace": "ns1",
4096            "type": "record",
4097            "fields": [
4098                { "name": "f1", "type": "int" },
4099                { "name": "f2", "type": "string" }
4100            ]
4101        }
4102        "#;
4103        let schema = Schema::parse_str(schema_str)?;
4104        match schema.aliases() {
4105            None => (),
4106            some => panic!("Expected None, got {some:?}"),
4107        }
4108
4109        // Test for Enum
4110        let schema_str = r#"
4111        {
4112            "name": "enum1",
4113            "namespace": "ns1",
4114            "type": "enum",
4115            "aliases": ["en1", "ns2.en2"],
4116            "symbols": ["a", "b", "c"]
4117        }
4118        "#;
4119        let schema = Schema::parse_str(schema_str)?;
4120        let expected = vec![Alias::new("ns1.en1")?, Alias::new("ns2.en2")?];
4121        match schema.aliases() {
4122            Some(aliases) => assert_eq!(aliases, &expected),
4123            None => panic!("Expected Some({expected:?}), got None"),
4124        }
4125
4126        let schema_str = r#"
4127        {
4128            "name": "enum1",
4129            "namespace": "ns1",
4130            "type": "enum",
4131            "symbols": ["a", "b", "c"]
4132        }
4133        "#;
4134        let schema = Schema::parse_str(schema_str)?;
4135        match schema.aliases() {
4136            None => (),
4137            some => panic!("Expected None, got {some:?}"),
4138        }
4139
4140        // Test for Fixed
4141        let schema_str = r#"
4142        {
4143            "name": "fixed1",
4144            "namespace": "ns1",
4145            "type": "fixed",
4146            "aliases": ["fx1", "ns2.fx2"],
4147            "size": 10
4148        }
4149        "#;
4150        let schema = Schema::parse_str(schema_str)?;
4151        let expected = vec![Alias::new("ns1.fx1")?, Alias::new("ns2.fx2")?];
4152        match schema.aliases() {
4153            Some(aliases) => assert_eq!(aliases, &expected),
4154            None => panic!("Expected Some({expected:?}), got None"),
4155        }
4156
4157        let schema_str = r#"
4158        {
4159            "name": "fixed1",
4160            "namespace": "ns1",
4161            "type": "fixed",
4162            "size": 10
4163        }
4164        "#;
4165        let schema = Schema::parse_str(schema_str)?;
4166        match schema.aliases() {
4167            None => (),
4168            some => panic!("Expected None, got {some:?}"),
4169        }
4170
4171        // Test for non-named type
4172        let schema = Schema::Int;
4173        match schema.aliases() {
4174            None => (),
4175            some => panic!("Expected None, got {some:?}"),
4176        }
4177
4178        Ok(())
4179    }
4180
4181    #[test]
4182    fn test_avro_3862_get_doc() -> TestResult {
4183        // Test for Record
4184        let schema_str = r#"
4185        {
4186            "name": "record1",
4187            "type": "record",
4188            "doc": "Record Document",
4189            "fields": [
4190                { "name": "f1", "type": "int" },
4191                { "name": "f2", "type": "string" }
4192            ]
4193        }
4194        "#;
4195        let schema = Schema::parse_str(schema_str)?;
4196        let expected = "Record Document";
4197        match schema.doc() {
4198            Some(doc) => assert_eq!(doc, expected),
4199            None => panic!("Expected Some({expected:?}), got None"),
4200        }
4201
4202        let schema_str = r#"
4203        {
4204            "name": "record1",
4205            "type": "record",
4206            "fields": [
4207                { "name": "f1", "type": "int" },
4208                { "name": "f2", "type": "string" }
4209            ]
4210        }
4211        "#;
4212        let schema = Schema::parse_str(schema_str)?;
4213        match schema.doc() {
4214            None => (),
4215            some => panic!("Expected None, got {some:?}"),
4216        }
4217
4218        // Test for Enum
4219        let schema_str = r#"
4220        {
4221            "name": "enum1",
4222            "type": "enum",
4223            "doc": "Enum Document",
4224            "symbols": ["a", "b", "c"]
4225        }
4226        "#;
4227        let schema = Schema::parse_str(schema_str)?;
4228        let expected = "Enum Document";
4229        match schema.doc() {
4230            Some(doc) => assert_eq!(doc, expected),
4231            None => panic!("Expected Some({expected:?}), got None"),
4232        }
4233
4234        let schema_str = r#"
4235        {
4236            "name": "enum1",
4237            "type": "enum",
4238            "symbols": ["a", "b", "c"]
4239        }
4240        "#;
4241        let schema = Schema::parse_str(schema_str)?;
4242        match schema.doc() {
4243            None => (),
4244            some => panic!("Expected None, got {some:?}"),
4245        }
4246
4247        // Test for Fixed
4248        let schema_str = r#"
4249        {
4250            "name": "fixed1",
4251            "type": "fixed",
4252            "doc": "Fixed Document",
4253            "size": 10
4254        }
4255        "#;
4256        let schema = Schema::parse_str(schema_str)?;
4257        let expected = "Fixed Document";
4258        match schema.doc() {
4259            Some(doc) => assert_eq!(doc, expected),
4260            None => panic!("Expected Some({expected:?}), got None"),
4261        }
4262
4263        let schema_str = r#"
4264        {
4265            "name": "fixed1",
4266            "type": "fixed",
4267            "size": 10
4268        }
4269        "#;
4270        let schema = Schema::parse_str(schema_str)?;
4271        match schema.doc() {
4272            None => (),
4273            some => panic!("Expected None, got {some:?}"),
4274        }
4275
4276        // Test for non-named type
4277        let schema = Schema::Int;
4278        match schema.doc() {
4279            None => (),
4280            some => panic!("Expected None, got {some:?}"),
4281        }
4282
4283        Ok(())
4284    }
4285
4286    #[test]
4287    fn avro_3886_serialize_attributes() -> TestResult {
4288        let attributes = BTreeMap::from([
4289            ("string_key".into(), "value".into()),
4290            ("number_key".into(), 1.23.into()),
4291            ("null_key".into(), JsonValue::Null),
4292            (
4293                "array_key".into(),
4294                JsonValue::Array(vec![1.into(), 2.into(), 3.into()]),
4295            ),
4296            ("object_key".into(), JsonValue::Object(Map::default())),
4297        ]);
4298
4299        // Test serialize enum attributes
4300        let schema = Schema::Enum(EnumSchema {
4301            name: Name::new("a")?,
4302            aliases: None,
4303            doc: None,
4304            symbols: vec![],
4305            default: None,
4306            attributes: attributes.clone(),
4307        });
4308        let serialized = serde_json::to_string(&schema)?;
4309        assert_eq!(
4310            r#"{"type":"enum","name":"a","symbols":[],"array_key":[1,2,3],"null_key":null,"number_key":1.23,"object_key":{},"string_key":"value"}"#,
4311            &serialized
4312        );
4313
4314        // Test serialize fixed custom_attributes
4315        let schema = Schema::Fixed(FixedSchema {
4316            name: Name::new("a")?,
4317            aliases: None,
4318            doc: None,
4319            size: 1,
4320            attributes: attributes.clone(),
4321        });
4322        let serialized = serde_json::to_string(&schema)?;
4323        assert_eq!(
4324            r#"{"type":"fixed","name":"a","size":1,"array_key":[1,2,3],"null_key":null,"number_key":1.23,"object_key":{},"string_key":"value"}"#,
4325            &serialized
4326        );
4327
4328        // Test serialize record custom_attributes
4329        let schema = Schema::Record(RecordSchema {
4330            name: Name::new("a")?,
4331            aliases: None,
4332            doc: None,
4333            fields: vec![],
4334            lookup: BTreeMap::new(),
4335            attributes,
4336        });
4337        let serialized = serde_json::to_string(&schema)?;
4338        assert_eq!(
4339            r#"{"type":"record","name":"a","fields":[],"array_key":[1,2,3],"null_key":null,"number_key":1.23,"object_key":{},"string_key":"value"}"#,
4340            &serialized
4341        );
4342
4343        Ok(())
4344    }
4345
4346    #[test]
4347    fn test_avro_3896_decimal_schema() -> TestResult {
4348        // bytes decimal, represented as native logical type.
4349        let schema = json!(
4350        {
4351          "type": "bytes",
4352          "name": "BytesDecimal",
4353          "logicalType": "decimal",
4354          "size": 38,
4355          "precision": 9,
4356          "scale": 2
4357        });
4358        let parse_result = Schema::parse(schema)?;
4359        assert_eq!(
4360            parse_result,
4361            Schema::Decimal(DecimalSchema {
4362                precision: NonZero::new(9).unwrap(),
4363                scale: 2,
4364                inner: InnerDecimalSchema::Bytes
4365            })
4366        );
4367
4368        // long decimal, represents as native complex type.
4369        let schema = json!(
4370        {
4371          "type": "long",
4372          "name": "LongDecimal",
4373          "logicalType": "decimal"
4374        });
4375        let parse_result = Schema::parse(schema)?;
4376        // assert!(matches!(parse_result, Schema::Long));
4377        assert_eq!(parse_result, Schema::Long);
4378
4379        Ok(())
4380    }
4381
4382    #[test]
4383    fn avro_3896_uuid_schema_for_string() -> TestResult {
4384        // string uuid, represents as native logical type.
4385        let schema = json!(
4386        {
4387          "type": "string",
4388          "name": "StringUUID",
4389          "logicalType": "uuid"
4390        });
4391        let parse_result = Schema::parse(schema)?;
4392        assert_eq!(parse_result, Schema::Uuid(UuidSchema::String));
4393
4394        Ok(())
4395    }
4396
4397    #[test]
4398    fn avro_3926_uuid_schema_for_fixed_with_size_16() -> TestResult {
4399        let schema = json!(
4400        {
4401            "type": "fixed",
4402            "name": "FixedUUID",
4403            "size": 16,
4404            "logicalType": "uuid"
4405        });
4406        let parse_result = Schema::parse(schema)?;
4407        assert_eq!(
4408            parse_result,
4409            Schema::Uuid(UuidSchema::Fixed(FixedSchema {
4410                name: Name::new("FixedUUID")?,
4411                aliases: None,
4412                doc: None,
4413                size: 16,
4414                attributes: Default::default(),
4415            }))
4416        );
4417        assert_not_logged(
4418            r#"Ignoring uuid logical type for a Fixed schema because its size (6) is not 16! Schema: Fixed(FixedSchema { name: Name { name: "FixedUUID", namespace: None }, aliases: None, doc: None, size: 6, attributes: {"logicalType": String("uuid")} })"#,
4419        );
4420
4421        Ok(())
4422    }
4423
4424    #[test]
4425    fn uuid_schema_bytes() -> TestResult {
4426        let schema = json!(
4427        {
4428          "type": "bytes",
4429          "name": "BytesUUID",
4430          "logicalType": "uuid"
4431        });
4432        let parse_result = Schema::parse(schema)?;
4433        assert_eq!(parse_result, Schema::Uuid(UuidSchema::Bytes));
4434
4435        Ok(())
4436    }
4437
4438    #[test]
4439    fn avro_3926_uuid_schema_for_fixed_with_size_different_than_16() -> TestResult {
4440        let schema = json!(
4441        {
4442            "type": "fixed",
4443            "name": "FixedUUID",
4444            "size": 6,
4445            "logicalType": "uuid"
4446        });
4447        let parse_result = Schema::parse(schema)?;
4448
4449        assert_eq!(
4450            parse_result,
4451            Schema::Fixed(FixedSchema {
4452                name: Name::new("FixedUUID")?,
4453                aliases: None,
4454                doc: None,
4455                size: 6,
4456                attributes: BTreeMap::new(),
4457            })
4458        );
4459        assert_logged(
4460            r#"Ignoring uuid logical type for a Fixed schema because its size (6) is not 16! Schema: Fixed(FixedSchema { name: Name { name: "FixedUUID", .. }, size: 6, .. })"#,
4461        );
4462
4463        Ok(())
4464    }
4465
4466    #[test]
4467    fn test_avro_3896_timestamp_millis_schema() -> TestResult {
4468        // long timestamp-millis, represents as native logical type.
4469        let schema = json!(
4470        {
4471          "type": "long",
4472          "name": "LongTimestampMillis",
4473          "logicalType": "timestamp-millis"
4474        });
4475        let parse_result = Schema::parse(schema)?;
4476        assert_eq!(parse_result, Schema::TimestampMillis);
4477
4478        // int timestamp-millis, represents as native complex type.
4479        let schema = json!(
4480        {
4481            "type": "int",
4482            "name": "IntTimestampMillis",
4483            "logicalType": "timestamp-millis"
4484        });
4485        let parse_result = Schema::parse(schema)?;
4486        assert_eq!(parse_result, Schema::Int);
4487
4488        Ok(())
4489    }
4490
4491    #[test]
4492    fn test_avro_3896_custom_bytes_schema() -> TestResult {
4493        // log type, represents as complex type.
4494        let schema = json!(
4495        {
4496            "type": "bytes",
4497            "name": "BytesLog",
4498            "logicalType": "custom"
4499        });
4500        let parse_result = Schema::parse(schema)?;
4501        assert_eq!(parse_result, Schema::Bytes);
4502        assert_eq!(parse_result.custom_attributes(), None);
4503
4504        Ok(())
4505    }
4506
4507    #[test]
4508    fn test_avro_3899_parse_decimal_type() -> TestResult {
4509        let schema = Schema::parse_str(
4510            r#"{
4511             "name": "InvalidDecimal",
4512             "type": "fixed",
4513             "size": 16,
4514             "logicalType": "decimal",
4515             "precision": 2,
4516             "scale": 3
4517         }"#,
4518        )?;
4519        match schema {
4520            Schema::Fixed(fixed_schema) => {
4521                let attrs = fixed_schema.attributes;
4522                let precision = attrs
4523                    .get("precision")
4524                    .expect("The 'precision' attribute is missing");
4525                let scale = attrs
4526                    .get("scale")
4527                    .expect("The 'scale' attribute is missing");
4528                assert_logged(&format!(
4529                    "Ignoring invalid decimal logical type: The decimal precision ({precision}) must be bigger or equal to the scale ({scale})"
4530                ));
4531            }
4532            _ => unreachable!("Expected Schema::Fixed, got {:?}", schema),
4533        }
4534
4535        let schema = Schema::parse_str(
4536            r#"{
4537            "name": "ValidDecimal",
4538             "type": "bytes",
4539             "logicalType": "decimal",
4540             "precision": 3,
4541             "scale": 2
4542         }"#,
4543        )?;
4544        match schema {
4545            Schema::Decimal(_) => {
4546                assert_not_logged(
4547                    "Ignoring invalid decimal logical type: The decimal precision (2) must be bigger or equal to the scale (3)",
4548                );
4549            }
4550            _ => unreachable!("Expected Schema::Decimal, got {:?}", schema),
4551        }
4552
4553        Ok(())
4554    }
4555
4556    #[test]
4557    fn avro_3920_serialize_record_with_custom_attributes() -> TestResult {
4558        let expected = {
4559            let mut lookup = BTreeMap::new();
4560            lookup.insert("value".to_owned(), 0);
4561            Schema::Record(RecordSchema {
4562                name: Name::new("LongList")?,
4563                aliases: Some(vec![Alias::new("LinkedLongs").unwrap()]),
4564                doc: None,
4565                fields: vec![
4566                    RecordField::builder()
4567                        .name("value".to_string())
4568                        .schema(Schema::Long)
4569                        .custom_attributes(BTreeMap::from([("field-id".to_string(), 1.into())]))
4570                        .build(),
4571                ],
4572                lookup,
4573                attributes: BTreeMap::from([("custom-attribute".to_string(), "value".into())]),
4574            })
4575        };
4576
4577        let value = serde_json::to_value(&expected)?;
4578        let serialized = serde_json::to_string(&value)?;
4579        assert_eq!(
4580            r#"{"aliases":["LinkedLongs"],"custom-attribute":"value","fields":[{"field-id":1,"name":"value","type":"long"}],"name":"LongList","type":"record"}"#,
4581            &serialized
4582        );
4583        assert_eq!(expected, Schema::parse_str(&serialized)?);
4584
4585        Ok(())
4586    }
4587
4588    #[test]
4589    fn test_avro_3925_serialize_decimal_inner_fixed() -> TestResult {
4590        let schema = Schema::Decimal(DecimalSchema {
4591            precision: NonZero::new(36).unwrap(),
4592            scale: 10,
4593            inner: InnerDecimalSchema::Fixed(FixedSchema {
4594                name: Name::new("decimal_36_10").unwrap(),
4595                aliases: None,
4596                doc: None,
4597                size: 16,
4598                attributes: Default::default(),
4599            }),
4600        });
4601
4602        let serialized_json = serde_json::to_string_pretty(&schema)?;
4603
4604        let expected_json = r#"{
4605  "type": "fixed",
4606  "name": "decimal_36_10",
4607  "size": 16,
4608  "logicalType": "decimal",
4609  "scale": 10,
4610  "precision": 36
4611}"#;
4612
4613        assert_eq!(serialized_json, expected_json);
4614
4615        Ok(())
4616    }
4617
4618    #[test]
4619    fn test_avro_3925_serialize_decimal_inner_bytes() -> TestResult {
4620        let schema = Schema::Decimal(DecimalSchema {
4621            precision: NonZero::new(36).unwrap(),
4622            scale: 10,
4623            inner: InnerDecimalSchema::Bytes,
4624        });
4625
4626        let serialized_json = serde_json::to_string_pretty(&schema)?;
4627
4628        let expected_json = r#"{
4629  "type": "bytes",
4630  "logicalType": "decimal",
4631  "scale": 10,
4632  "precision": 36
4633}"#;
4634
4635        assert_eq!(serialized_json, expected_json);
4636
4637        Ok(())
4638    }
4639
4640    #[test]
4641    fn test_avro_3927_serialize_array_with_custom_attributes() -> TestResult {
4642        let expected = Schema::array(Schema::Long)
4643            .attributes(BTreeMap::from([("field-id".to_string(), "1".into())]))
4644            .build();
4645
4646        let value = serde_json::to_value(&expected)?;
4647        let serialized = serde_json::to_string(&value)?;
4648        assert_eq!(
4649            r#"{"field-id":"1","items":"long","type":"array"}"#,
4650            &serialized
4651        );
4652        let actual_schema = Schema::parse_str(&serialized)?;
4653        assert_eq!(expected, actual_schema);
4654        assert_eq!(
4655            expected.custom_attributes(),
4656            actual_schema.custom_attributes()
4657        );
4658
4659        Ok(())
4660    }
4661
4662    #[test]
4663    fn test_avro_3927_serialize_map_with_custom_attributes() -> TestResult {
4664        let expected = Schema::map(Schema::Long)
4665            .attributes(BTreeMap::from([("field-id".to_string(), "1".into())]))
4666            .build();
4667
4668        let value = serde_json::to_value(&expected)?;
4669        let serialized = serde_json::to_string(&value)?;
4670        assert_eq!(
4671            r#"{"field-id":"1","type":"map","values":"long"}"#,
4672            &serialized
4673        );
4674        let actual_schema = Schema::parse_str(&serialized)?;
4675        assert_eq!(expected, actual_schema);
4676        assert_eq!(
4677            expected.custom_attributes(),
4678            actual_schema.custom_attributes()
4679        );
4680
4681        Ok(())
4682    }
4683
4684    #[test]
4685    fn avro_3928_parse_int_based_schema_with_default() -> TestResult {
4686        let schema = r#"
4687        {
4688          "type": "record",
4689          "name": "DateLogicalType",
4690          "fields": [ {
4691            "name": "birthday",
4692            "type": {"type": "int", "logicalType": "date"},
4693            "default": 1681601653
4694          } ]
4695        }"#;
4696
4697        match Schema::parse_str(schema)? {
4698            Schema::Record(record_schema) => {
4699                assert_eq!(record_schema.fields.len(), 1);
4700                let field = record_schema.fields.first().unwrap();
4701                assert_eq!(field.name, "birthday");
4702                assert_eq!(field.schema, Schema::Date);
4703                assert_eq!(
4704                    types::Value::try_from(field.default.clone().unwrap())?,
4705                    types::Value::Int(1681601653)
4706                );
4707            }
4708            _ => unreachable!("Expected Schema::Record"),
4709        }
4710
4711        Ok(())
4712    }
4713
4714    #[test]
4715    fn avro_3946_union_with_single_type() -> TestResult {
4716        let schema = r#"
4717        {
4718          "type": "record",
4719          "name": "Issue",
4720          "namespace": "invalid.example",
4721          "fields": [
4722            {
4723              "name": "myField",
4724              "type": ["long"]
4725            }
4726          ]
4727        }"#;
4728
4729        let _ = Schema::parse_str(schema)?;
4730
4731        assert_logged(
4732            "Union schema with just one member! Consider dropping the union! \
4733                    Please enable debug logging to find out which Record schema \
4734                    declares the union with 'RUST_LOG=apache_avro::schema=debug'.",
4735        );
4736
4737        Ok(())
4738    }
4739
4740    #[test]
4741    fn avro_3946_union_without_any_types() -> TestResult {
4742        let schema = r#"
4743        {
4744          "type": "record",
4745          "name": "Issue",
4746          "namespace": "invalid.example",
4747          "fields": [
4748            {
4749              "name": "myField",
4750              "type": []
4751            }
4752          ]
4753        }"#;
4754
4755        let _ = Schema::parse_str(schema)?;
4756
4757        assert_logged(
4758            "Union schemas should have at least two members! \
4759                    Please enable debug logging to find out which Record schema \
4760                    declares the union with 'RUST_LOG=apache_avro::schema=debug'.",
4761        );
4762
4763        Ok(())
4764    }
4765
4766    #[test]
4767    fn avro_4004_canonical_form_strip_logical_types() -> TestResult {
4768        let schema_str = r#"
4769      {
4770        "type": "record",
4771        "name": "test",
4772        "fields": [
4773            {"name": "a", "type": "long", "default": 42, "doc": "The field a"},
4774            {"name": "b", "type": "string", "namespace": "test.a"},
4775            {"name": "c", "type": {"type": "long", "logicalType": "timestamp-micros"}}
4776        ]
4777    }"#;
4778
4779        let schema = Schema::parse_str(schema_str)?;
4780        let canonical_form = schema.canonical_form();
4781        let fp_rabin = schema.fingerprint::<Rabin>();
4782        assert_eq!(
4783            r#"{"name":"test","type":"record","fields":[{"name":"a","type":"long"},{"name":"b","type":"string"},{"name":"c","type":{"type":"long"}}]}"#,
4784            canonical_form
4785        );
4786        assert_eq!("92f2ccef718c6754", fp_rabin.to_string());
4787        Ok(())
4788    }
4789
4790    #[test]
4791    fn avro_4055_should_fail_to_parse_invalid_schema() -> TestResult {
4792        // This is invalid because the record type should be inside the type field.
4793        let invalid_schema_str = r#"
4794        {
4795        "type": "record",
4796        "name": "SampleSchema",
4797        "fields": [
4798            {
4799            "name": "order",
4800            "type": "record",
4801            "fields": [
4802                {
4803                "name": "order_number",
4804                "type": ["null", "string"],
4805                "default": null
4806                },
4807                { "name": "order_date", "type": "string" }
4808            ]
4809            }
4810        ]
4811        }"#;
4812
4813        let schema = Schema::parse_str(invalid_schema_str);
4814        assert!(schema.is_err());
4815        assert_eq!(
4816            schema.unwrap_err().to_string(),
4817            "Invalid schema: There is no type called 'record', if you meant to define a non-primitive schema, it should be defined inside `type` attribute."
4818        );
4819
4820        let valid_schema = r#"
4821        {
4822            "type": "record",
4823            "name": "SampleSchema",
4824            "fields": [
4825                {
4826                "name": "order",
4827                "type": {
4828                    "type": "record",
4829                    "name": "Order",
4830                    "fields": [
4831                    {
4832                        "name": "order_number",
4833                        "type": ["null", "string"],
4834                        "default": null
4835                    },
4836                    { "name": "order_date", "type": "string" }
4837                    ]
4838                }
4839                }
4840            ]
4841        }"#;
4842        let schema = Schema::parse_str(valid_schema);
4843        assert!(schema.is_ok());
4844
4845        Ok(())
4846    }
4847
4848    #[test]
4849    fn avro_rs_292_array_items_should_be_ignored_in_custom_attributes() -> TestResult {
4850        let raw_schema = r#"{
4851                    "type": "array",
4852                    "items": {
4853                        "name": "foo",
4854                        "type": "record",
4855                        "fields": [
4856                            {
4857                                "name": "bar",
4858                                "type": {
4859                                    "type": "array",
4860                                    "items": {
4861                                        "type": "record",
4862                                        "name": "baz",
4863                                        "fields": [
4864                                            {
4865                                                "name": "quux",
4866                                                "type": "int"
4867                                            }
4868                                        ]
4869                                    }
4870                                }
4871                            }
4872                        ]
4873                    }
4874                }"#;
4875
4876        let schema1 = Schema::parse_str(raw_schema)?;
4877        match &schema1 {
4878            Schema::Array(ArraySchema { items, attributes }) => {
4879                assert!(attributes.is_empty());
4880
4881                match **items {
4882                    Schema::Record(RecordSchema {
4883                        ref name,
4884                        aliases: _,
4885                        doc: _,
4886                        ref fields,
4887                        lookup: _,
4888                        ref attributes,
4889                    }) => {
4890                        assert_eq!(name.to_string(), "foo");
4891                        assert_eq!(fields.len(), 1);
4892                        assert!(attributes.is_empty());
4893
4894                        match &fields[0].schema {
4895                            Schema::Array(ArraySchema {
4896                                items: _,
4897                                attributes,
4898                            }) => {
4899                                assert!(attributes.is_empty());
4900                            }
4901                            _ => panic!("Expected ArraySchema. got: {}", fields[0].schema),
4902                        }
4903                    }
4904                    _ => panic!("Expected RecordSchema. got: {items}"),
4905                }
4906            }
4907            _ => panic!("Expected ArraySchema. got: {schema1}"),
4908        }
4909        let canonical_form1 = schema1.canonical_form();
4910        let schema2 = Schema::parse_str(&canonical_form1)?;
4911        let canonical_form2 = schema2.canonical_form();
4912
4913        assert_eq!(canonical_form1, canonical_form2);
4914
4915        Ok(())
4916    }
4917
4918    #[test]
4919    fn avro_rs_292_map_values_should_be_ignored_in_custom_attributes() -> TestResult {
4920        let raw_schema = r#"{
4921                    "type": "array",
4922                    "items": {
4923                        "name": "foo",
4924                        "type": "record",
4925                        "fields": [
4926                            {
4927                                "name": "bar",
4928                                "type": {
4929                                    "type": "map",
4930                                    "values": {
4931                                        "type": "record",
4932                                        "name": "baz",
4933                                        "fields": [
4934                                            {
4935                                                "name": "quux",
4936                                                "type": "int"
4937                                            }
4938                                        ]
4939                                    }
4940                                }
4941                            }
4942                        ]
4943                    }
4944                }"#;
4945
4946        let schema1 = Schema::parse_str(raw_schema)?;
4947        match &schema1 {
4948            Schema::Array(ArraySchema { items, attributes }) => {
4949                assert!(attributes.is_empty());
4950
4951                match **items {
4952                    Schema::Record(RecordSchema {
4953                        ref name,
4954                        aliases: _,
4955                        doc: _,
4956                        ref fields,
4957                        lookup: _,
4958                        ref attributes,
4959                    }) => {
4960                        assert_eq!(name.to_string(), "foo");
4961                        assert_eq!(fields.len(), 1);
4962                        assert!(attributes.is_empty());
4963
4964                        match &fields[0].schema {
4965                            Schema::Map(MapSchema {
4966                                types: _,
4967                                attributes,
4968                            }) => {
4969                                assert!(attributes.is_empty());
4970                            }
4971                            _ => panic!("Expected MapSchema. got: {}", fields[0].schema),
4972                        }
4973                    }
4974                    _ => panic!("Expected RecordSchema. got: {items}"),
4975                }
4976            }
4977            _ => panic!("Expected ArraySchema. got: {schema1}"),
4978        }
4979        let canonical_form1 = schema1.canonical_form();
4980        let schema2 = Schema::parse_str(&canonical_form1)?;
4981        let canonical_form2 = schema2.canonical_form();
4982
4983        assert_eq!(canonical_form1, canonical_form2);
4984
4985        Ok(())
4986    }
4987
4988    #[test]
4989    fn avro_rs_382_serialize_duration_schema() -> TestResult {
4990        let schema = Schema::Duration(FixedSchema {
4991            name: Name::try_from("Duration")?,
4992            aliases: None,
4993            doc: None,
4994            size: 12,
4995            attributes: BTreeMap::new(),
4996        });
4997
4998        let expected_schema_json = json!({
4999            "type": "fixed",
5000            "logicalType": "duration",
5001            "name": "Duration",
5002            "size": 12
5003        });
5004
5005        let schema_json = serde_json::to_value(&schema)?;
5006
5007        assert_eq!(&schema_json, &expected_schema_json);
5008
5009        Ok(())
5010    }
5011
5012    #[test]
5013    fn avro_rs_395_logical_type_written_once_for_duration() -> TestResult {
5014        let schema = Schema::parse_str(
5015            r#"{
5016            "type": "fixed",
5017            "logicalType": "duration",
5018            "name": "Duration",
5019            "size": 12
5020        }"#,
5021        )?;
5022
5023        let schema_json_str = serde_json::to_string(&schema)?;
5024
5025        assert_eq!(
5026            schema_json_str.matches("logicalType").count(),
5027            1,
5028            "Expected serialized schema to contain only one logicalType key: {schema_json_str}"
5029        );
5030
5031        Ok(())
5032    }
5033
5034    #[test]
5035    fn avro_rs_395_logical_type_written_once_for_uuid_fixed() -> TestResult {
5036        let schema = Schema::parse_str(
5037            r#"{
5038            "type": "fixed",
5039            "logicalType": "uuid",
5040            "name": "UUID",
5041            "size": 16
5042        }"#,
5043        )?;
5044
5045        let schema_json_str = serde_json::to_string(&schema)?;
5046
5047        assert_eq!(
5048            schema_json_str.matches("logicalType").count(),
5049            1,
5050            "Expected serialized schema to contain only one logicalType key: {schema_json_str}"
5051        );
5052
5053        Ok(())
5054    }
5055
5056    #[test]
5057    fn avro_rs_395_logical_type_written_once_for_decimal_fixed() -> TestResult {
5058        let schema = Schema::parse_str(
5059            r#"{
5060            "type": "fixed",
5061            "logicalType": "decimal",
5062            "scale": 4,
5063            "precision": 8,
5064            "name": "FixedDecimal16",
5065            "size": 16
5066        }"#,
5067        )?;
5068
5069        let schema_json_str = serde_json::to_string(&schema)?;
5070
5071        assert_eq!(
5072            schema_json_str.matches("logicalType").count(),
5073            1,
5074            "Expected serialized schema to contain only one logicalType key: {schema_json_str}"
5075        );
5076
5077        Ok(())
5078    }
5079
5080    #[test]
5081    fn avro_rs_420_independent_canonical_form() -> TestResult {
5082        let (record, schemata) = Schema::parse_str_with_list(
5083            r#"{
5084            "name": "root",
5085            "type": "record",
5086            "fields": [{
5087                "name": "node",
5088                "type": "node"
5089            }]
5090        }"#,
5091            [r#"{
5092            "name": "node",
5093            "type": "record",
5094            "fields": [{
5095                "name": "children",
5096                "type": ["null", "node"]
5097            }]
5098        }"#],
5099        )?;
5100        let icf = record.independent_canonical_form(&schemata)?;
5101        assert_eq!(
5102            icf,
5103            r#"{"name":"root","type":"record","fields":[{"name":"node","type":{"name":"node","type":"record","fields":[{"name":"children","type":["null","node"]}]}}]}"#
5104        );
5105        Ok(())
5106    }
5107
5108    #[test]
5109    fn avro_rs_456_bool_instead_of_boolean() -> TestResult {
5110        let error = Schema::parse_str(
5111            r#"{
5112            "type": "record",
5113            "name": "defaults",
5114            "fields": [
5115                {"name": "boolean", "type": "bool", "default": true}
5116            ]
5117        }"#,
5118        )
5119        .unwrap_err()
5120        .into_details()
5121        .to_string();
5122        assert_eq!(
5123            error,
5124            Details::ParsePrimitiveSimilar("bool".to_string(), "boolean").to_string()
5125        );
5126
5127        Ok(())
5128    }
5129
5130    #[test]
5131    fn avro_rs_460_fixed_default_in_custom_attributes() -> TestResult {
5132        let schema = Schema::parse_str(
5133            r#"{
5134            "name": "fixed_with_default",
5135            "type": "fixed",
5136            "size": 1,
5137            "default": "\u0000",
5138            "doc": "a docstring"
5139        }"#,
5140        )?;
5141
5142        assert_eq!(schema.custom_attributes().unwrap().len(), 1);
5143
5144        let json = serde_json::to_string(&schema)?;
5145        let schema2 = Schema::parse_str(&json)?;
5146
5147        assert_eq!(schema2.custom_attributes().unwrap().len(), 1);
5148
5149        Ok(())
5150    }
5151
5152    #[test]
5153    fn avro_rs_460_enum_default_not_in_custom_attributes() -> TestResult {
5154        let schema = Schema::parse_str(
5155            r#"{
5156            "name": "enum_with_default",
5157            "type": "enum",
5158            "symbols": ["A", "B", "C"],
5159            "default": "A",
5160            "doc": "a docstring"
5161        }"#,
5162        )?;
5163
5164        assert_eq!(schema.custom_attributes().unwrap(), &BTreeMap::new());
5165
5166        let json = serde_json::to_string(&schema)?;
5167        let schema2 = Schema::parse_str(&json)?;
5168
5169        let Schema::Enum(enum_schema) = schema2 else {
5170            panic!("Expected Schema::Enum, got {schema2:?}");
5171        };
5172        assert!(enum_schema.default.is_some());
5173        assert!(enum_schema.doc.is_some());
5174
5175        Ok(())
5176    }
5177
5178    #[test]
5179    fn avro_rs_476_enum_cannot_be_directly_in_field() -> TestResult {
5180        let schema_str = r#"{
5181            "type": "record",
5182            "name": "ExampleEnum",
5183            "namespace": "com.schema",
5184            "fields": [
5185                {
5186                "name": "wrong_enum",
5187                "type": "enum",
5188                "symbols": ["INSERT", "UPDATE"]
5189                }
5190            ]
5191        }"#;
5192        let result = Schema::parse_str(schema_str).unwrap_err();
5193        assert_eq!(
5194            result.to_string(),
5195            "Invalid schema: There is no type called 'enum', if you meant to define a non-primitive schema, it should be defined inside `type` attribute."
5196        );
5197        Ok(())
5198    }
5199
5200    #[test]
5201    fn avro_rs_509_default_must_be_in_custom_attributes_for_map_and_enum() -> TestResult {
5202        let schema = Schema::parse_str(
5203            r#"{
5204            "name": "ignore_defaults",
5205            "type": "record",
5206            "fields": [
5207                {"name": "a", "type": { "type": "map", "values": "string", "default": null }},
5208                {"name": "b", "type": { "type": "array", "items": "string", "default": null }}
5209            ]
5210        }"#,
5211        )?;
5212
5213        let Schema::Record(record) = schema else {
5214            panic!("Expected Schema::Record, got {schema:?}");
5215        };
5216        let Schema::Map(map) = &record.fields[0].schema else {
5217            panic!("Expected Schema::Map for first field of {record:?}");
5218        };
5219        assert_eq!(map.attributes.len(), 1);
5220        assert_eq!(
5221            map.attributes.get("default"),
5222            Some(&serde_json::Value::Null)
5223        );
5224
5225        let Schema::Array(array) = &record.fields[1].schema else {
5226            panic!("Expected Schema::Array for second field of {record:?}");
5227        };
5228        assert_eq!(array.attributes.len(), 1);
5229        assert_eq!(
5230            array.attributes.get("default"),
5231            Some(&serde_json::Value::Null)
5232        );
5233
5234        Ok(())
5235    }
5236
5237    #[test]
5238    fn avro_rs_512_unique_normalized_name_must_be_unique() -> TestResult {
5239        let one = Schema::Ref {
5240            name: "a.b.c".parse()?,
5241        };
5242        let two = Schema::Ref {
5243            name: "a_b_c".parse()?,
5244        };
5245        let three = Schema::Ref {
5246            name: "a__b__c".parse()?,
5247        };
5248        let four = Schema::Ref {
5249            name: "a._b._c".parse()?,
5250        };
5251
5252        assert_ne!(one.unique_normalized_name(), two.unique_normalized_name());
5253        assert_ne!(one.unique_normalized_name(), three.unique_normalized_name());
5254        assert_ne!(one.unique_normalized_name(), four.unique_normalized_name());
5255        assert_ne!(two.unique_normalized_name(), three.unique_normalized_name());
5256        assert_ne!(two.unique_normalized_name(), four.unique_normalized_name());
5257        assert_ne!(
5258            three.unique_normalized_name(),
5259            four.unique_normalized_name()
5260        );
5261
5262        Ok(())
5263    }
5264
5265    #[test]
5266    fn avro_rs_654_unknown_logical_type_must_survive_roundtrip() -> TestResult {
5267        let schema_str = r#"{
5268          "type": "array",
5269          "logicalType": "map",
5270          "items": {
5271            "type": "record",
5272            "name": "k12_v13",
5273            "fields": [
5274              {
5275                "name": "key",
5276                "type": "int",
5277                "field-id": 12
5278              },
5279              {
5280                "name": "value",
5281                "type": "string",
5282                "field-id": 13
5283              }
5284            ]
5285          }
5286        }"#;
5287        let Schema::Array(schema) = Schema::parse_str(schema_str)? else {
5288            panic!("This must be an array schema")
5289        };
5290        assert_eq!(
5291            schema.attributes.get("logicalType").unwrap(),
5292            &serde_json::Value::String("map".into())
5293        );
5294
5295        let schema_str_2 = serde_json::to_string(&Schema::Array(schema))?;
5296
5297        let Schema::Array(schema) = Schema::parse_str(&schema_str_2)? else {
5298            panic!("This must be an array schema")
5299        };
5300        assert_eq!(
5301            schema.attributes.get("logicalType").unwrap(),
5302            &serde_json::Value::String("map".into())
5303        );
5304
5305        Ok(())
5306    }
5307
5308    #[test]
5309    fn avro_rs_654_preserve_unknown_logical_type_on_outer_item() -> TestResult {
5310        let raw_schema = r#"{
5311        "type": "array",
5312        "logicalType": "blub",
5313        "items": {
5314            "type": "record",
5315            "name": "k12_v13",
5316            "fields": [
5317                {
5318                    "name": "key",
5319                    "type": "int",
5320                    "field-id": 12
5321                },
5322                {
5323                    "name": "value",
5324                    "type": "string",
5325                    "field-id": 13
5326                }
5327            ]
5328        }
5329    }"#;
5330
5331        let schema = Schema::parse_str(raw_schema)?;
5332
5333        let output = serde_json::to_string_pretty(&schema).unwrap();
5334        pretty_assertions::assert_eq!(
5335            r#"{
5336  "type": "array",
5337  "items": {
5338    "type": "record",
5339    "name": "k12_v13",
5340    "fields": [
5341      {
5342        "name": "key",
5343        "type": "int",
5344        "field-id": 12
5345      },
5346      {
5347        "name": "value",
5348        "type": "string",
5349        "field-id": 13
5350      }
5351    ]
5352  },
5353  "logicalType": "blub"
5354}"#,
5355            output
5356        );
5357
5358        let logical_type = schema.custom_attributes().unwrap().get("logicalType");
5359        assert_eq!(
5360            logical_type,
5361            Some(&serde_json::Value::String("blub".to_string()))
5362        );
5363
5364        Ok(())
5365    }
5366
5367    #[test]
5368    fn avro_rs_654_preserve_unknown_logical_type_on_inner_item() -> TestResult {
5369        let raw_schema = r#"{
5370        "type": "record",
5371        "name": "test_record",
5372        "fields": [
5373            {
5374            "name": "example_map",
5375            "type": {
5376                "type": "array",
5377                "logicalType": "fish",
5378                "items": {
5379                    "type": "record",
5380                    "name": "k12_v13",
5381                    "fields": [
5382                        {
5383                            "name": "key",
5384                            "type": "int",
5385                            "field-id": 12
5386                        },
5387                        {
5388                            "name": "value",
5389                            "type": "string",
5390                            "field-id": 13
5391                        }
5392                    ]
5393                }
5394            }
5395            }
5396        ]
5397    }"#;
5398
5399        let schema = Schema::parse_str(raw_schema)?;
5400        let Schema::Record(record) = &schema else {
5401            panic!("Expected a record schema");
5402        };
5403        let example_map_schema = &record.fields[0].schema;
5404        let logical_type = example_map_schema
5405            .custom_attributes()
5406            .unwrap()
5407            .get("logicalType");
5408        assert_eq!(
5409            logical_type,
5410            Some(&serde_json::Value::String("fish".to_string()))
5411        );
5412
5413        Ok(())
5414    }
5415}