You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@avro.apache.org by "github-code-scanning[bot] (via GitHub)" <gi...@apache.org> on 2023/06/16 14:52:02 UTC

[GitHub] [avro] github-code-scanning[bot] commented on a diff in pull request #2292: Cross-file type resolution for C#

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


##########
lang/csharp/src/apache/codegen/AvroGen.cs:
##########
@@ -201,5 +220,102 @@
 
             return 0;
         }
+
+        static int GenSchema(List<string> infiles, string outdir, IEnumerable<KeyValuePair<string, string>> namespaceMapping)
+        {
+            try
+            {
+                var sn = new SchemaNames();
+                CodeGen codegen = new CodeGen();
+                var targetNs = new List<string>();
+
+                if (infiles.Count == 1)
+                {
+                    FileAttributes attr = System.IO.File.GetAttributes(infiles.First());
+                    if (attr.HasFlag(FileAttributes.Directory))
+                    {
+                        var dirInfo = new DirectoryInfo(infiles.First());
+                        infiles = dirInfo.GetFiles("*.*", SearchOption.TopDirectoryOnly)
+                                         .OrderBy(f => f.Name)
+                                         .Select(f => f.FullName)
+                                         .ToList();
+                    }
+                }
+
+                var toRetry = new List<string>();

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



##########
lang/csharp/src/apache/codegen/AvroGen.cs:
##########
@@ -201,5 +220,102 @@
 
             return 0;
         }
+
+        static int GenSchema(List<string> infiles, string outdir, IEnumerable<KeyValuePair<string, string>> namespaceMapping)
+        {
+            try
+            {
+                var sn = new SchemaNames();
+                CodeGen codegen = new CodeGen();
+                var targetNs = new List<string>();
+
+                if (infiles.Count == 1)
+                {
+                    FileAttributes attr = System.IO.File.GetAttributes(infiles.First());
+                    if (attr.HasFlag(FileAttributes.Directory))
+                    {
+                        var dirInfo = new DirectoryInfo(infiles.First());
+                        infiles = dirInfo.GetFiles("*.*", SearchOption.TopDirectoryOnly)
+                                         .OrderBy(f => f.Name)
+                                         .Select(f => f.FullName)
+                                         .ToList();
+                    }
+                }
+
+                var toRetry = new List<string>();

Review Comment:
   ## Container contents are never accessed
   
   The contents of this container are never accessed.
   
   [Show more details](https://github.com/apache/avro/security/code-scanning/2994)



##########
lang/csharp/src/apache/test/AvroGen/AvroGenToolTests.cs:
##########
@@ -99,5 +99,16 @@
             Assert.That(result.ExitCode, Is.EqualTo(0));
             Assert.IsTrue(result.StdOut.Any(s => s.Contains("--skip-directories")));
         }
+
+        [TestCase("-ms", "data/", "generated/")]
+        public void CommandLineMsArgs(params string[] args)
+        {
+            AvroGenToolResult result = AvroGenHelper.RunAvroGenTool(args);

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



##########
lang/csharp/src/apache/codegen/AvroGen.cs:
##########
@@ -201,5 +220,102 @@
 
             return 0;
         }
+
+        static int GenSchema(List<string> infiles, string outdir, IEnumerable<KeyValuePair<string, string>> namespaceMapping)
+        {
+            try
+            {
+                var sn = new SchemaNames();
+                CodeGen codegen = new CodeGen();
+                var targetNs = new List<string>();
+
+                if (infiles.Count == 1)
+                {
+                    FileAttributes attr = System.IO.File.GetAttributes(infiles.First());
+                    if (attr.HasFlag(FileAttributes.Directory))
+                    {
+                        var dirInfo = new DirectoryInfo(infiles.First());
+                        infiles = dirInfo.GetFiles("*.*", SearchOption.TopDirectoryOnly)
+                                         .OrderBy(f => f.Name)
+                                         .Select(f => f.FullName)
+                                         .ToList();
+                    }
+                }
+
+                var toRetry = new List<string>();
+                foreach (var infile in infiles)
+                {
+                    FileAttributes attr = System.IO.File.GetAttributes(infile);
+                    if (attr.HasFlag(FileAttributes.Directory))
+                        continue;
+
+                    Console.WriteLine($"Loading Schema from: [{infile}]");
+                    string text = System.IO.File.ReadAllText(infile);
+
+                    //try
+                    {
+                        Schema schema = Schema.Parse(text, sn);
+                        var namespaces = GetNamespacesFromSchema(schema);
+
+                        foreach (var n in namespaces)
+                        {
+                            if (!targetNs.Contains(n))
+                            {
+                                targetNs.Add(n);
+                            }
+                        }
+
+                        codegen.AddSchema(schema);
+                    }
+                    //catch(Avro.SchemaParseException e)
+                    //{
+                    //    if (toRetry.Contains(infile))
+                    //        toRetry.Remove(infile);
+                    //    else
+                    //        toRetry.Add(infile);
+                    //}
+                }
+
+                foreach (var entry in namespaceMapping)
+                    codegen.NamespaceMapping[entry.Key] = entry.Value;
+
+                Console.WriteLine("Generating code ...");
+                codegen.GenerateCode();
+
+                Console.WriteLine($"Output code to [{Path.GetFullPath(outdir)}]");
+
+                codegen.WriteTypes(outdir);
+
+                return 0;
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine("Exception occurred. " + ex.Message);
+                return 1;
+            }

Review Comment:
   ## Generic catch clause
   
   Generic catch clause.
   
   [Show more details](https://github.com/apache/avro/security/code-scanning/2997)



##########
lang/csharp/src/apache/codegen/AvroGen.cs:
##########
@@ -201,5 +220,102 @@
 
             return 0;
         }
+
+        static int GenSchema(List<string> infiles, string outdir, IEnumerable<KeyValuePair<string, string>> namespaceMapping)
+        {
+            try
+            {
+                var sn = new SchemaNames();
+                CodeGen codegen = new CodeGen();
+                var targetNs = new List<string>();
+
+                if (infiles.Count == 1)
+                {
+                    FileAttributes attr = System.IO.File.GetAttributes(infiles.First());
+                    if (attr.HasFlag(FileAttributes.Directory))
+                    {
+                        var dirInfo = new DirectoryInfo(infiles.First());
+                        infiles = dirInfo.GetFiles("*.*", SearchOption.TopDirectoryOnly)
+                                         .OrderBy(f => f.Name)
+                                         .Select(f => f.FullName)
+                                         .ToList();
+                    }
+                }
+
+                var toRetry = new List<string>();
+                foreach (var infile in infiles)
+                {
+                    FileAttributes attr = System.IO.File.GetAttributes(infile);
+                    if (attr.HasFlag(FileAttributes.Directory))
+                        continue;
+
+                    Console.WriteLine($"Loading Schema from: [{infile}]");
+                    string text = System.IO.File.ReadAllText(infile);
+
+                    //try
+                    {
+                        Schema schema = Schema.Parse(text, sn);
+                        var namespaces = GetNamespacesFromSchema(schema);
+
+                        foreach (var n in namespaces)
+                        {
+                            if (!targetNs.Contains(n))
+                            {
+                                targetNs.Add(n);
+                            }
+                        }

Review Comment:
   ## Missed opportunity to use Where
   
   This foreach loop [implicitly filters its target sequence](1) - consider filtering the sequence explicitly using '.Where(...)'.
   
   [Show more details](https://github.com/apache/avro/security/code-scanning/2998)



##########
lang/csharp/src/apache/codegen/AvroGen.cs:
##########
@@ -201,5 +220,102 @@
 
             return 0;
         }
+
+        static int GenSchema(List<string> infiles, string outdir, IEnumerable<KeyValuePair<string, string>> namespaceMapping)
+        {
+            try
+            {
+                var sn = new SchemaNames();
+                CodeGen codegen = new CodeGen();
+                var targetNs = new List<string>();
+
+                if (infiles.Count == 1)
+                {
+                    FileAttributes attr = System.IO.File.GetAttributes(infiles.First());
+                    if (attr.HasFlag(FileAttributes.Directory))
+                    {
+                        var dirInfo = new DirectoryInfo(infiles.First());
+                        infiles = dirInfo.GetFiles("*.*", SearchOption.TopDirectoryOnly)
+                                         .OrderBy(f => f.Name)
+                                         .Select(f => f.FullName)
+                                         .ToList();
+                    }
+                }
+
+                var toRetry = new List<string>();
+                foreach (var infile in infiles)
+                {
+                    FileAttributes attr = System.IO.File.GetAttributes(infile);
+                    if (attr.HasFlag(FileAttributes.Directory))
+                        continue;
+
+                    Console.WriteLine($"Loading Schema from: [{infile}]");
+                    string text = System.IO.File.ReadAllText(infile);
+
+                    //try
+                    {
+                        Schema schema = Schema.Parse(text, sn);
+                        var namespaces = GetNamespacesFromSchema(schema);
+
+                        foreach (var n in namespaces)
+                        {
+                            if (!targetNs.Contains(n))
+                            {
+                                targetNs.Add(n);
+                            }
+                        }
+
+                        codegen.AddSchema(schema);
+                    }
+                    //catch(Avro.SchemaParseException e)
+                    //{
+                    //    if (toRetry.Contains(infile))
+                    //        toRetry.Remove(infile);
+                    //    else
+                    //        toRetry.Add(infile);
+                    //}
+                }
+
+                foreach (var entry in namespaceMapping)
+                    codegen.NamespaceMapping[entry.Key] = entry.Value;
+
+                Console.WriteLine("Generating code ...");
+                codegen.GenerateCode();
+
+                Console.WriteLine($"Output code to [{Path.GetFullPath(outdir)}]");
+
+                codegen.WriteTypes(outdir);
+
+                return 0;
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine("Exception occurred. " + ex.Message);
+                return 1;
+            }
+        }
+
+        private static List<string> GetNamespacesFromSchema(Schema schema)
+        {
+            var result = new List<string>();
+
+            if (schema is NamedSchema namedSchema && !result.Contains(namedSchema.Namespace))
+            {
+                result.Add(namedSchema.Namespace);
+            }
+
+            if (schema is UnionSchema unionSchema)
+            {
+                foreach (var s in unionSchema.Schemas)
+                {
+                    if (s is RecordSchema recordSchema && !result.Contains(recordSchema.Namespace))
+                    {
+                        result.Add(recordSchema.Namespace);
+                    }
+                }

Review Comment:
   ## Missed opportunity to use Where
   
   This foreach loop [implicitly filters its target sequence](1) - consider filtering the sequence explicitly using '.Where(...)'.
   
   [Show more details](https://github.com/apache/avro/security/code-scanning/2999)



##########
lang/csharp/src/apache/main/CodeGen/CodeGen.cs:
##########
@@ -1273,5 +1273,47 @@
                 return $@"""namespace""{m.Groups[1].Value}:{m.Groups[2].Value}""{ns}""";
             });
         }
+
+        public virtual void WriteTypes(string outputdir, List<string> targetNamespaces)
+        {
+            var cscp = new CSharpCodeProvider();

Review Comment:
   ## Missing Dispose call on local IDisposable
   
   Disposable 'CSharpCodeProvider' is created but not disposed.
   
   [Show more details](https://github.com/apache/avro/security/code-scanning/3000)



-- 
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