You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2022/02/10 14:37:01 UTC

[avro] branch master updated: AVRO-3259: Throw exception if unknown codec (#1531)

This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/master by this push:
     new 9f37f91  AVRO-3259: Throw exception if unknown codec (#1531)
9f37f91 is described below

commit 9f37f912e9bde481a14a27e5553fb61a4df1277b
Author: Zoltan Csizmadia <zc...@gmail.com>
AuthorDate: Thu Feb 10 08:36:52 2022 -0600

    AVRO-3259: Throw exception if unknown codec (#1531)
    
    * AVRO-3259: Throw exception if unknown codec
    
    * AVRO-3259: If codec is absent, it is assumed to be "null"
    
    * AVRO-3259: Sync  null and exception handling with Java code
    
    Co-authored-by: Zoltan Csizmadia <Cs...@valassis.com>
---
 lang/csharp/src/apache/main/File/Codec.cs          | 19 +++++++++++----
 lang/csharp/src/apache/main/File/DataFileReader.cs |  8 ++++++-
 lang/csharp/src/apache/test/File/FileTests.cs      | 28 ++++++++++++++++++++++
 3 files changed, 50 insertions(+), 5 deletions(-)

diff --git a/lang/csharp/src/apache/main/File/Codec.cs b/lang/csharp/src/apache/main/File/Codec.cs
index 3aab184..3a5b27b 100644
--- a/lang/csharp/src/apache/main/File/Codec.cs
+++ b/lang/csharp/src/apache/main/File/Codec.cs
@@ -138,9 +138,11 @@ namespace Avro.File
             {
                 case Type.Deflate:
                     return new DeflateCodec();
-                default:
+                case Type.Null:
                     return new NullCodec();
             }
+
+            throw new AvroRuntimeException($"Unrecognized codec: {codecType}");
         }
 
         /// <summary>
@@ -150,6 +152,13 @@ namespace Avro.File
         /// <returns>Codec based on type.</returns>
         public static Codec CreateCodecFromString(string codecType)
         {
+            if (codecType == null)
+            {
+                // If codec is absent, it is assumed to be "null"
+                // https://avro.apache.org/docs/current/spec.html
+                return CreateCodec(Type.Null);
+            }
+
             foreach (var resolver in _codecResolvers)
             {
                 var candidateCodec = resolver(codecType);
@@ -162,10 +171,12 @@ namespace Avro.File
             switch (codecType)
             {
                 case DataFileConstants.DeflateCodec:
-                    return new DeflateCodec();
-                default:
-                    return new NullCodec();
+                    return CreateCodec(Type.Deflate);
+                case DataFileConstants.NullCodec:
+                    return CreateCodec(Type.Null);
             }
+
+            throw new AvroRuntimeException($"Unrecognized codec: {codecType}");
         }
 
         /// <summary>
diff --git a/lang/csharp/src/apache/main/File/DataFileReader.cs b/lang/csharp/src/apache/main/File/DataFileReader.cs
index 77bfd8a..dff13e0 100644
--- a/lang/csharp/src/apache/main/File/DataFileReader.cs
+++ b/lang/csharp/src/apache/main/File/DataFileReader.cs
@@ -462,7 +462,13 @@ namespace Avro.File
         /// </returns>
         private Codec ResolveCodec()
         {
-            return Codec.CreateCodecFromString(GetMetaString(DataFileConstants.MetaDataCodec));
+            string codec = GetMetaString(DataFileConstants.MetaDataCodec);
+
+            // If codec is absent, it is assumed to be "null"
+            if (codec == null)
+                return Codec.CreateCodec(Codec.Type.Null);
+
+            return Codec.CreateCodecFromString(codec);
         }
 
         /// <inheritdoc/>
diff --git a/lang/csharp/src/apache/test/File/FileTests.cs b/lang/csharp/src/apache/test/File/FileTests.cs
index 5cde994..7047043 100644
--- a/lang/csharp/src/apache/test/File/FileTests.cs
+++ b/lang/csharp/src/apache/test/File/FileTests.cs
@@ -870,6 +870,34 @@ namespace Avro.Test.File
             Assert.AreEqual(expectResolverProvidedCodec, resolverProvidedCodec);
         }
 
+        [TestCase("")]
+        [TestCase("blahblahblah")]
+        public void UnknownCodecFromStringException(string codec)
+        {
+            Assert.Throws(typeof(AvroRuntimeException), () => Codec.CreateCodecFromString(codec));
+        }
+
+        [TestCase((Codec.Type)(-1))] // "Invalid" Codec.Type
+        public void UnknownCodecFromType(Codec.Type codec)
+        {
+            Assert.Throws(typeof(AvroRuntimeException), () => Codec.CreateCodec(codec));
+        }
+
+        [TestCase("deflate")]
+        [TestCase("null")]
+        [TestCase(null)] // If codec is absent, it is assumed to be "null"
+        public void KnownCodecFromString(string codec)
+        {
+            Assert.NotNull(Codec.CreateCodecFromString(codec));
+        }
+
+        [TestCase(Codec.Type.Deflate)]
+        [TestCase(Codec.Type.Null)]
+        public void KnownCodecFromType(Codec.Type codec)
+        {
+            Assert.NotNull(Codec.CreateCodec(codec));
+        }
+
         private bool CheckPrimitive<T>(Stream input, T value, ReaderWriterSet<T>.ReaderFactory createReader)
         {
             IFileReader<T> reader = createReader(input, null);