This is an automated email from the ASF dual-hosted git repository. mgrigorov pushed a commit to branch branch-1.11 in repository https://gitbox.apache.org/repos/asf/avro.git
commit f666c568ccd11493292acdafcc2ebf2a5818eed0 Author: Martin Tzvetanov Grigorov <[email protected]> AuthorDate: Thu Jan 20 09:22:57 2022 +0200 AVRO-3302: Add support for parsing recursive schemas for Enum Signed-off-by: Martin Tzvetanov Grigorov <[email protected]> (cherry picked from commit 064cc6b4bd1c6dcf59da989e712a27f0a955f70d) --- lang/rust/src/schema.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/lang/rust/src/schema.rs b/lang/rust/src/schema.rs index 73b64c7..5415d35 100644 --- a/lang/rust/src/schema.rs +++ b/lang/rust/src/schema.rs @@ -786,7 +786,7 @@ impl Parser { match complex.get("type") { Some(&Value::String(ref t)) => match t.as_str() { "record" => self.parse_record(complex), - "enum" => Self::parse_enum(complex), + "enum" => self.parse_enum(complex), "array" => self.parse_array(complex), "map" => self.parse_map(complex), "fixed" => Self::parse_fixed(complex), @@ -842,7 +842,7 @@ impl Parser { /// Parse a `serde_json::Value` representing a Avro enum type into a /// `Schema`. - fn parse_enum(complex: &Map<String, Value>) -> AvroResult<Schema> { + fn parse_enum(&mut self, complex: &Map<String, Value>) -> AvroResult<Schema> { let name = Name::parse(complex)?; let symbols: Vec<String> = complex @@ -872,11 +872,14 @@ impl Parser { existing_symbols.insert(symbol); } - Ok(Schema::Enum { - name, + let schema = Schema::Enum { + name: name.clone(), doc: complex.doc(), symbols, - }) + }; + self.parsed_schemas + .insert(name.fullname(None), schema.clone()); + Ok(schema) } /// Parse a `serde_json::Value` representing a Avro array type into a @@ -1514,6 +1517,58 @@ mod tests { assert_eq!(canonical_form, &expected); } + // https://github.com/flavray/avro-rs/pull/99#issuecomment-1016948451 + #[test] + fn test_parsing_of_recursive_type_enum() { + let schema = r#" + { + "type": "record", + "name": "User", + "namespace": "office", + "fields": [ + { + "name": "details", + "type": [ + { + "type": "record", + "name": "Employee", + "fields": [ + { + "name": "gender", + "type": { + "type": "enum", + "name": "Gender", + "symbols": [ + "male", + "female" + ] + }, + "default": "female" + } + ] + }, + { + "type": "record", + "name": "Manager", + "fields": [ + { + "name": "gender", + "type": "Gender" + } + ] + } + ] + } + ] +} +"#; + + let schema = Schema::parse_str(schema).unwrap(); + let schema_str = schema.canonical_form(); + let expected = r#"{"name":"office.User","type":"record","fields":[{"name":"details","type":[{"name":"Employee","type":"record","fields":[{"name":"gender","type":{"name":"Gender","type":"enum","symbols":["male","female"]}}]},{"name":"Manager","type":"record","fields":[{"name":"gender","type":{"name":"Gender","type":"enum","symbols":["male","female"]}}]}]}]}"#; + assert_eq!(schema_str, expected); + } + #[test] fn test_enum_schema() { let schema = Schema::parse_str(
