You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@avro.apache.org by GitBox <gi...@apache.org> on 2022/06/21 17:05:29 UTC

[GitHub] [avro] github-code-scanning[bot] commented on a diff in pull request #1728: AVRO-3540: Support dictionaries keyed by types other than string

github-code-scanning[bot] commented on code in PR #1728:
URL: https://github.com/apache/avro/pull/1728#discussion_r902864008


##########
lang/csharp/src/apache/test/Reflect/TestReflect.cs:
##########
@@ -21,12 +21,75 @@
 using NUnit.Framework;
 using Avro.IO;
 using Avro.Reflect;
+using System.Collections.Generic;
+using System;
+using Avro.Specific;
+using System.Linq;
+
+
 
 namespace Avro.Test
 {
     [TestFixture]
     class TestReflect
     {
+        public class DictionaryTestClass
+        {
+            public Dictionary<int, int> p { get; set; }
+            public NestedTestClass ntc { get; set; }
+
+
+            public class NestedTestClass
+            {
+                public int NestedTestClassInt { get; set; }
+            }
+        }
+
+        public class DictionaryTestClass2
+        {
+            public Dictionary<int, string> p { get; set; }
+
+        }
+        class TestMapConverter : IAvroFieldConverter
+        {
+            public object FromAvroType(object o, Schema s) =>
+                ((Dictionary<string, int>)o).ToDictionary(x => int.Parse(x.Key), y=>y.Value);
+                
+            
+            public Type GetAvroType() => typeof(IDictionary<string, int>);
+            public Type GetPropertyType() => typeof(Dictionary<int, int>);
+            public object ToAvroType(object o, Schema s) =>
+                ((Dictionary<int, int>)o).ToDictionary(x => x.Key.ToString(), y => y.Value);
+        }
+
+        [TestCase]
+        public void TestMapWithConverterSucceeds()
+        {
+            ClassCache.AddDefaultConverter(new TestMapConverter());
+            
+            var schemaJson = "{\"fields\":[{\"name\":\"ntc\",\"type\":{\"type\":\"record\",\"name\":\"NestedTestClass\",\"fields\":[{\"name\":\"NestedTestClassInt\",\"type\":\"int\"}]}},{\"type\":{\"values\":\"int\",\"type\":\"map\"},\"name\":\"p\"}],\"type\":\"record\",\"name\":\"DictionaryTestClass\",\"namespace\":\"Avro.Test.TestReflect\\u002B\"}";
+            var schema = Schema.Parse(schemaJson);
+            DictionaryTestClass expected = new DictionaryTestClass() { p = new Dictionary<int, int>() { { 1, 1 }, { 2, 4 }, { 3, 5 } } , ntc = new DictionaryTestClass.NestedTestClass() { NestedTestClassInt = 1 } };
+
+            using Stream stream =  serialize(schema, expected);
+            var avroReader = new ReflectReader<DictionaryTestClass>(schema, schema);    
+            stream.Position = 0;
+            var actual = avroReader.Read(null, new BinaryDecoder(stream));
+           
+            CollectionAssert.AreEquivalent(expected.p, actual.p);
+        }
+
+        [TestCase]
+        public void TestMapWithoutConverterFails()
+        {
+            var schemaJson = "{\"fields\":[{\"type\":{\"values\":\"string\",\"type\":\"map\"},\"name\":\"p\"}],\"type\":\"record\",\"name\":\"DictionaryTestClass\",\"namespace\":\"Avro.Test.TestReflect\\u002B\"}";
+            var schema = Schema.Parse(schemaJson);
+            DictionaryTestClass2 expected = new DictionaryTestClass2() { p = new Dictionary<int, string>() { { 1, "1" }, { 2, "4" }, { 3, "5" } } };

Review Comment:
   ## Useless assignment to local variable
   
   This assignment to [expected](1) is useless, since its value is never read.
   
   [Show more details](https://github.com/apache/avro/security/code-scanning/2723)



##########
lang/csharp/src/apache/main/Reflect/DotnetProperty.cs:
##########
@@ -19,6 +19,7 @@
 using System;
 using System.Reflection;
 using System.Collections;
+using System.Collections.Generic;

Review Comment:
   ## Compilation message
   
   Hidden CS8019 Unnecessary using directive.
   
   [Show more details](https://github.com/apache/avro/security/code-scanning/2724)



-- 
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: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org