martin-g commented on code in PR #509:
URL: https://github.com/apache/avro-rs/pull/509#discussion_r2904736953


##########
avro/src/schema/mod.rs:
##########
@@ -5102,289 +5061,62 @@ mod tests {
     }
 
     #[test]
-    fn avro_rs_467_array_default() -> TestResult {
-        let schema = Schema::parse_str(
-            r#"{
-            "type": "array",
-            "items": "string",
-            "default": []
-        }"#,
-        )?;
-
-        let Schema::Array(array) = schema else {
-            panic!("Expected Schema::Array, got {schema:?}");
-        };
-
-        assert_eq!(array.attributes, BTreeMap::new());
-        assert_eq!(array.default, Some(Vec::new()));
-
-        let json = serde_json::to_string(&Schema::Array(array))?;
-        assert!(json.contains(r#""default":[]"#));
-
-        Ok(())
-    }
-
-    #[test]
-    fn avro_rs_467_array_default_with_actual_values() -> TestResult {
-        let schema = Schema::parse_str(
-            r#"{
-            "type": "array",
-            "items": "string",
-            "default": ["foo", "bar"]
-        }"#,
-        )?;
-
-        let Schema::Array(array) = schema else {
-            panic!("Expected Schema::Array, got {schema:?}");
-        };
-
-        assert_eq!(array.attributes, BTreeMap::new());
-        assert_eq!(
-            array.default,
-            Some(vec![
-                Value::String("foo".into()),
-                Value::String("bar".into())
-            ])
-        );
-
-        let json = serde_json::to_string(&Schema::Array(array))?;
-        assert!(json.contains(r#""default":["foo","bar"]"#));
-
-        Ok(())
-    }
-
-    #[test]
-    fn avro_rs_467_array_default_with_invalid_values() -> TestResult {
-        let err = Schema::parse_str(
-            r#"{
-            "type": "array",
-            "items": "string",
-            "default": [false, true]
-        }"#,
-        )
-        .unwrap_err();
-
-        assert_eq!(
-            err.to_string(),
-            "Default value for an array must be an array of String! Found: 
Boolean(false)"
-        );
-
-        Ok(())
-    }
-
-    #[test]
-    fn avro_rs_467_array_default_with_mixed_values() -> TestResult {
-        let err = Schema::parse_str(
-            r#"{
-            "type": "array",
-            "items": "string",
-            "default": ["foo", true]
-        }"#,
-        )
-        .unwrap_err();
-
-        assert_eq!(
-            err.to_string(),
-            "Default value for an array must be an array of String! Found: 
Boolean(true)"
-        );
-
-        Ok(())
-    }
-
-    #[test]
-    fn avro_rs_467_array_default_with_reference() -> TestResult {
-        let schema = Schema::parse_str(
-            r#"{
+    fn avro_rs_476_enum_cannot_be_directly_in_field() -> TestResult {
+        let schema_str = r#"{
             "type": "record",
-            "name": "Something",
+            "name": "ExampleEnum",
+            "namespace": "com.schema",
             "fields": [
                 {
-                    "name": "one",
-                    "type": {
-                        "type": "enum",
-                        "name": "ABC",
-                        "symbols": ["A", "B", "C"]
-                    }
-                },
-                {
-                    "name": "two",
-                    "type": {
-                        "type": "array",
-                        "items": "ABC",
-                        "default": ["A", "B", "C"]
-                    }
+                "name": "wrong_enum",
+                "type": "enum",
+                "symbols": ["INSERT", "UPDATE"]
                 }
             ]
-        }"#,
-        )?;
-
-        let Schema::Record(record) = schema else {
-            panic!("Expected Schema::Record, got {schema:?}");
-        };
-        let Schema::Array(array) = &record.fields[1].schema else {
-            panic!("Expected Schema::Array, got {:?}", 
record.fields[1].schema);
-        };
-
-        assert_eq!(array.attributes, BTreeMap::new());
-        assert_eq!(
-            array.default,
-            Some(vec![
-                Value::String("A".into()),
-                Value::String("B".into()),
-                Value::String("C".into())
-            ])
-        );
-
-        Ok(())
-    }
-
-    #[test]
-    fn avro_rs_467_map_default() -> TestResult {
-        let schema = Schema::parse_str(
-            r#"{
-            "type": "map",
-            "values": "string",
-            "default": {}
-        }"#,
-        )?;
-
-        let Schema::Map(map) = schema else {
-            panic!("Expected Schema::Map, got {schema:?}");
-        };
-
-        assert_eq!(map.attributes, BTreeMap::new());
-        assert_eq!(map.default, Some(HashMap::new()));
-
-        let json = serde_json::to_string(&Schema::Map(map))?;
-        assert!(json.contains(r#""default":{}"#));
-
-        Ok(())
-    }
-
-    #[test]
-    fn avro_rs_467_map_default_with_actual_values() -> TestResult {
-        let schema = Schema::parse_str(
-            r#"{
-            "type": "map",
-            "values": "string",
-            "default": {"foo": "bar"}
-        }"#,
-        )?;
-
-        let Schema::Map(map) = schema else {
-            panic!("Expected Schema::Map, got {schema:?}");
-        };
-
-        let mut hashmap = HashMap::new();
-        hashmap.insert("foo".to_string(), Value::String("bar".into()));
-        assert_eq!(map.attributes, BTreeMap::new());
-        assert_eq!(map.default, Some(hashmap));
-
-        let json = serde_json::to_string(&Schema::Map(map))?;
-        assert!(json.contains(r#""default":{"foo":"bar"}"#));
-
-        Ok(())
-    }
-
-    #[test]
-    fn avro_rs_467_map_default_with_invalid_values() -> TestResult {
-        let err = Schema::parse_str(
-            r#"{
-            "type": "map",
-            "values": "string",
-            "default": {"foo": true}
-        }"#,
-        )
-        .unwrap_err();
-
-        assert_eq!(
-            err.to_string(),
-            "Default value for a map must be an object with (String, String)! 
Found: (String, Boolean(true))"
-        );
-
-        Ok(())
-    }
-
-    #[test]
-    fn avro_rs_467_map_default_with_mixed_values() -> TestResult {
-        let err = Schema::parse_str(
-            r#"{
-            "type": "map",
-            "values": "string",
-            "default": {"foo": "bar", "spam": true}
-        }"#,
-        )
-        .unwrap_err();
-
+        }"#;
+        let result = Schema::parse_str(schema_str).unwrap_err();
         assert_eq!(
-            err.to_string(),
-            "Default value for a map must be an object with (String, String)! 
Found: (String, Boolean(true))"
+            result.to_string(),
+            "Invalid schema: There is no type called 'enum', if you meant to 
define a non-primitive schema, it should be defined inside `type` attribute."
         );
-
         Ok(())
     }
 
     #[test]
-    fn avro_rs_467_map_default_with_reference() -> TestResult {
+    fn avro_rs_509_default_must_be_in_custom_attributes_for_map_and_enum() -> 
TestResult {
         let schema = Schema::parse_str(
             r#"{
+            "name": "ignore_defaults",
             "type": "record",
-            "name": "Something",
             "fields": [
-                {
-                    "name": "one",
-                    "type": {
-                        "type": "enum",
-                        "name": "ABC",
-                        "symbols": ["A", "B", "C"]
-                    }
-                },
-                {
-                    "name": "two",
-                    "type": {
-                        "type": "map",
-                        "values": "ABC",
-                        "default": {"foo": "A"}
-                    }
-                }
+                {"name": "a", "type": { "type": "map", "values": "string", 
"default": null }},
+                {"name": "b", "type": { "type": "array", "items": "string", 
"default": null }}
             ]
         }"#,
         )?;
 
         let Schema::Record(record) = schema else {
-            panic!("Expected Schema::Record, got {schema:?}");
+            panic!("Expect Schema::Record for {schema:?}");
         };
-        let Schema::Map(map) = &record.fields[1].schema else {
-            panic!("Expected Schema::Map, got {:?}", record.fields[1].schema);
+        println!("{:?}", record);

Review Comment:
   debug leftover
   ```suggestion
   ```



##########
avro/src/schema/mod.rs:
##########
@@ -5102,289 +5061,62 @@ mod tests {
     }
 
     #[test]
-    fn avro_rs_467_array_default() -> TestResult {
-        let schema = Schema::parse_str(
-            r#"{
-            "type": "array",
-            "items": "string",
-            "default": []
-        }"#,
-        )?;
-
-        let Schema::Array(array) = schema else {
-            panic!("Expected Schema::Array, got {schema:?}");
-        };
-
-        assert_eq!(array.attributes, BTreeMap::new());
-        assert_eq!(array.default, Some(Vec::new()));
-
-        let json = serde_json::to_string(&Schema::Array(array))?;
-        assert!(json.contains(r#""default":[]"#));
-
-        Ok(())
-    }
-
-    #[test]
-    fn avro_rs_467_array_default_with_actual_values() -> TestResult {
-        let schema = Schema::parse_str(
-            r#"{
-            "type": "array",
-            "items": "string",
-            "default": ["foo", "bar"]
-        }"#,
-        )?;
-
-        let Schema::Array(array) = schema else {
-            panic!("Expected Schema::Array, got {schema:?}");
-        };
-
-        assert_eq!(array.attributes, BTreeMap::new());
-        assert_eq!(
-            array.default,
-            Some(vec![
-                Value::String("foo".into()),
-                Value::String("bar".into())
-            ])
-        );
-
-        let json = serde_json::to_string(&Schema::Array(array))?;
-        assert!(json.contains(r#""default":["foo","bar"]"#));
-
-        Ok(())
-    }
-
-    #[test]
-    fn avro_rs_467_array_default_with_invalid_values() -> TestResult {
-        let err = Schema::parse_str(
-            r#"{
-            "type": "array",
-            "items": "string",
-            "default": [false, true]
-        }"#,
-        )
-        .unwrap_err();
-
-        assert_eq!(
-            err.to_string(),
-            "Default value for an array must be an array of String! Found: 
Boolean(false)"
-        );
-
-        Ok(())
-    }
-
-    #[test]
-    fn avro_rs_467_array_default_with_mixed_values() -> TestResult {
-        let err = Schema::parse_str(
-            r#"{
-            "type": "array",
-            "items": "string",
-            "default": ["foo", true]
-        }"#,
-        )
-        .unwrap_err();
-
-        assert_eq!(
-            err.to_string(),
-            "Default value for an array must be an array of String! Found: 
Boolean(true)"
-        );
-
-        Ok(())
-    }
-
-    #[test]
-    fn avro_rs_467_array_default_with_reference() -> TestResult {
-        let schema = Schema::parse_str(
-            r#"{
+    fn avro_rs_476_enum_cannot_be_directly_in_field() -> TestResult {
+        let schema_str = r#"{
             "type": "record",
-            "name": "Something",
+            "name": "ExampleEnum",
+            "namespace": "com.schema",
             "fields": [
                 {
-                    "name": "one",
-                    "type": {
-                        "type": "enum",
-                        "name": "ABC",
-                        "symbols": ["A", "B", "C"]
-                    }
-                },
-                {
-                    "name": "two",
-                    "type": {
-                        "type": "array",
-                        "items": "ABC",
-                        "default": ["A", "B", "C"]
-                    }
+                "name": "wrong_enum",
+                "type": "enum",
+                "symbols": ["INSERT", "UPDATE"]
                 }
             ]
-        }"#,
-        )?;
-
-        let Schema::Record(record) = schema else {
-            panic!("Expected Schema::Record, got {schema:?}");
-        };
-        let Schema::Array(array) = &record.fields[1].schema else {
-            panic!("Expected Schema::Array, got {:?}", 
record.fields[1].schema);
-        };
-
-        assert_eq!(array.attributes, BTreeMap::new());
-        assert_eq!(
-            array.default,
-            Some(vec![
-                Value::String("A".into()),
-                Value::String("B".into()),
-                Value::String("C".into())
-            ])
-        );
-
-        Ok(())
-    }
-
-    #[test]
-    fn avro_rs_467_map_default() -> TestResult {
-        let schema = Schema::parse_str(
-            r#"{
-            "type": "map",
-            "values": "string",
-            "default": {}
-        }"#,
-        )?;
-
-        let Schema::Map(map) = schema else {
-            panic!("Expected Schema::Map, got {schema:?}");
-        };
-
-        assert_eq!(map.attributes, BTreeMap::new());
-        assert_eq!(map.default, Some(HashMap::new()));
-
-        let json = serde_json::to_string(&Schema::Map(map))?;
-        assert!(json.contains(r#""default":{}"#));
-
-        Ok(())
-    }
-
-    #[test]
-    fn avro_rs_467_map_default_with_actual_values() -> TestResult {
-        let schema = Schema::parse_str(
-            r#"{
-            "type": "map",
-            "values": "string",
-            "default": {"foo": "bar"}
-        }"#,
-        )?;
-
-        let Schema::Map(map) = schema else {
-            panic!("Expected Schema::Map, got {schema:?}");
-        };
-
-        let mut hashmap = HashMap::new();
-        hashmap.insert("foo".to_string(), Value::String("bar".into()));
-        assert_eq!(map.attributes, BTreeMap::new());
-        assert_eq!(map.default, Some(hashmap));
-
-        let json = serde_json::to_string(&Schema::Map(map))?;
-        assert!(json.contains(r#""default":{"foo":"bar"}"#));
-
-        Ok(())
-    }
-
-    #[test]
-    fn avro_rs_467_map_default_with_invalid_values() -> TestResult {
-        let err = Schema::parse_str(
-            r#"{
-            "type": "map",
-            "values": "string",
-            "default": {"foo": true}
-        }"#,
-        )
-        .unwrap_err();
-
-        assert_eq!(
-            err.to_string(),
-            "Default value for a map must be an object with (String, String)! 
Found: (String, Boolean(true))"
-        );
-
-        Ok(())
-    }
-
-    #[test]
-    fn avro_rs_467_map_default_with_mixed_values() -> TestResult {
-        let err = Schema::parse_str(
-            r#"{
-            "type": "map",
-            "values": "string",
-            "default": {"foo": "bar", "spam": true}
-        }"#,
-        )
-        .unwrap_err();
-
+        }"#;
+        let result = Schema::parse_str(schema_str).unwrap_err();
         assert_eq!(
-            err.to_string(),
-            "Default value for a map must be an object with (String, String)! 
Found: (String, Boolean(true))"
+            result.to_string(),
+            "Invalid schema: There is no type called 'enum', if you meant to 
define a non-primitive schema, it should be defined inside `type` attribute."
         );
-
         Ok(())
     }
 
     #[test]
-    fn avro_rs_467_map_default_with_reference() -> TestResult {
+    fn avro_rs_509_default_must_be_in_custom_attributes_for_map_and_enum() -> 
TestResult {
         let schema = Schema::parse_str(
             r#"{
+            "name": "ignore_defaults",
             "type": "record",
-            "name": "Something",
             "fields": [
-                {
-                    "name": "one",
-                    "type": {
-                        "type": "enum",
-                        "name": "ABC",
-                        "symbols": ["A", "B", "C"]
-                    }
-                },
-                {
-                    "name": "two",
-                    "type": {
-                        "type": "map",
-                        "values": "ABC",
-                        "default": {"foo": "A"}
-                    }
-                }
+                {"name": "a", "type": { "type": "map", "values": "string", 
"default": null }},
+                {"name": "b", "type": { "type": "array", "items": "string", 
"default": null }}
             ]
         }"#,
         )?;
 
         let Schema::Record(record) = schema else {
-            panic!("Expected Schema::Record, got {schema:?}");
+            panic!("Expect Schema::Record for {schema:?}");

Review Comment:
   The old message look better to me.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to