You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by ni...@apache.org on 2022/01/18 12:50:57 UTC

[ignite] branch master updated: IGNITE-16294 .NET: Fix System.Enum serialization (#9734)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 7c73053  IGNITE-16294 .NET: Fix System.Enum serialization (#9734)
7c73053 is described below

commit 7c730535c252f39c2c731f4e0df5bd84bc386115
Author: Vladimir Steshin <vl...@gmail.com>
AuthorDate: Tue Jan 18 15:49:49 2022 +0300

    IGNITE-16294 .NET: Fix System.Enum serialization (#9734)
---
 .../ignite/platform/PlatformDeployServiceTask.java |   5 +-
 .../platform/model/{Role.java => AccessLevel.java} |  20 +--
 .../org/apache/ignite/platform/model/Role.java     |  16 ++-
 .../Apache.Ignite.Core.Tests/Binary/EnumsTest.cs   | 144 +++++++++++++++++++++
 .../Apache.Ignite.Core.Tests/Services/Model.cs     |  11 ++
 .../Services/PlatformTestService.cs                |   4 +-
 .../Services/ServicesTypeAutoResolveTest.cs        |   4 +-
 .../Apache.Ignite.Core/Impl/Common/TypeCaster.cs   |  34 ++++-
 8 files changed, 215 insertions(+), 23 deletions(-)

diff --git a/modules/core/src/test/java/org/apache/ignite/platform/PlatformDeployServiceTask.java b/modules/core/src/test/java/org/apache/ignite/platform/PlatformDeployServiceTask.java
index a101620..0436d4c 100644
--- a/modules/core/src/test/java/org/apache/ignite/platform/PlatformDeployServiceTask.java
+++ b/modules/core/src/test/java/org/apache/ignite/platform/PlatformDeployServiceTask.java
@@ -43,6 +43,7 @@ import org.apache.ignite.internal.binary.BinaryArray;
 import org.apache.ignite.internal.util.typedef.F;
 import org.apache.ignite.internal.util.typedef.internal.U;
 import org.apache.ignite.platform.model.ACL;
+import org.apache.ignite.platform.model.AccessLevel;
 import org.apache.ignite.platform.model.Account;
 import org.apache.ignite.platform.model.Address;
 import org.apache.ignite.platform.model.Department;
@@ -591,8 +592,8 @@ public class PlatformDeployServiceTask extends ComputeTaskAdapter<String, Object
         /** */
         public User[] testUsers() {
             return new User[] {
-                new User(1, ACL.ALLOW, new Role("admin")),
-                new User(2, ACL.DENY, new Role("user"))
+                new User(1, ACL.ALLOW, new Role("admin", AccessLevel.SUPER)),
+                new User(2, ACL.DENY, new Role("user", AccessLevel.USER))
             };
         }
 
diff --git a/modules/core/src/test/java/org/apache/ignite/platform/model/Role.java b/modules/core/src/test/java/org/apache/ignite/platform/model/AccessLevel.java
similarity index 75%
copy from modules/core/src/test/java/org/apache/ignite/platform/model/Role.java
copy to modules/core/src/test/java/org/apache/ignite/platform/model/AccessLevel.java
index f47eddb..f14e3a2 100644
--- a/modules/core/src/test/java/org/apache/ignite/platform/model/Role.java
+++ b/modules/core/src/test/java/org/apache/ignite/platform/model/AccessLevel.java
@@ -17,23 +17,11 @@
 
 package org.apache.ignite.platform.model;
 
-/** Test value object. */
-public class Role {
+/** Test enum. */
+public enum AccessLevel {
     /** */
-    String name;
+    USER,
 
     /** */
-    public Role(String name) {
-        this.name = name;
-    }
-
-    /** */
-    public String getName() {
-        return name;
-    }
-
-    /** */
-    public void setName(String name) {
-        this.name = name;
-    }
+    SUPER
 }
diff --git a/modules/core/src/test/java/org/apache/ignite/platform/model/Role.java b/modules/core/src/test/java/org/apache/ignite/platform/model/Role.java
index f47eddb..ad8b3d3 100644
--- a/modules/core/src/test/java/org/apache/ignite/platform/model/Role.java
+++ b/modules/core/src/test/java/org/apache/ignite/platform/model/Role.java
@@ -23,8 +23,12 @@ public class Role {
     String name;
 
     /** */
-    public Role(String name) {
+    AccessLevel accessLevel;
+
+    /** */
+    public Role(String name, AccessLevel accessLevel) {
         this.name = name;
+        this.accessLevel = accessLevel;
     }
 
     /** */
@@ -36,4 +40,14 @@ public class Role {
     public void setName(String name) {
         this.name = name;
     }
+
+    /** */
+    public AccessLevel getAccessLevel() {
+        return accessLevel;
+    }
+
+    /** */
+    public void setAccessLevel(AccessLevel accessLevel) {
+        this.accessLevel = accessLevel;
+    }
 }
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/EnumsTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/EnumsTest.cs
index 4064151..e2de729 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/EnumsTest.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/EnumsTest.cs
@@ -307,7 +307,82 @@ namespace Apache.Ignite.Core.Tests.Binary
 
             CheckSerializeDeserialize(val);
         }
+        
+        /// <summary>
+        /// Tests enums serialization by single value.
+        /// </summary>
+        [Test]
+        public void TestEnumValueSerialization()
+        {
+            CheckSerializeDeserialize(ByteEnum.Foo);
+            CheckSerializeDeserialize(ByteEnum.Bar);
+            CheckSerializeDeserialize(SByteEnum.Foo);
+            CheckSerializeDeserialize(SByteEnum.Bar);
+
+            CheckSerializeDeserialize(ShortEnum.Foo);
+            CheckSerializeDeserialize(ShortEnum.Bar);
+            CheckSerializeDeserialize(UShortEnum.Foo);
+            CheckSerializeDeserialize(UShortEnum.Bar);
+
+            CheckSerializeDeserialize(IntEnum.Foo);
+            CheckSerializeDeserialize(IntEnum.Bar);
+            CheckSerializeDeserialize(UIntEnum.Foo);
+            CheckSerializeDeserialize(UIntEnum.Bar);
+
+            CheckSerializeDeserialize(LongEnum.Foo);
+            CheckSerializeDeserialize(LongEnum.Bar);
+            CheckSerializeDeserialize(ULongEnum.Foo);
+            CheckSerializeDeserialize(ULongEnum.Bar);
+        }
+
+        /// <summary>
+        /// Tests enums arrays serialization.
+        /// </summary>
+        [Test]
+        public void TestEnumArraysSerialization()
+        {
+            CheckSerializeDeserialize(new[] {ByteEnum.Foo, ByteEnum.Bar});
+            CheckSerializeDeserialize(new[] {SByteEnum.Foo, SByteEnum.Bar});
+            CheckSerializeDeserialize(new Enum[] {ByteEnum.Foo, SByteEnum.Bar});
+
+            CheckSerializeDeserialize(new[] {ShortEnum.Foo, ShortEnum.Bar});
+            CheckSerializeDeserialize(new[] {UShortEnum.Foo, UShortEnum.Bar});
+            CheckSerializeDeserialize(new Enum[] {ShortEnum.Foo, UShortEnum.Bar});
 
+            CheckSerializeDeserialize(new[] {IntEnum.Foo, IntEnum.Bar});
+            CheckSerializeDeserialize(new[] {UIntEnum.Foo, UIntEnum.Bar});
+            CheckSerializeDeserialize(new Enum[] {IntEnum.Foo, UIntEnum.Bar});
+
+            CheckSerializeDeserialize(new[] {LongEnum.Foo, LongEnum.Bar});
+            CheckSerializeDeserialize(new[] {ULongEnum.Foo, ULongEnum.Bar});
+            CheckSerializeDeserialize(new Enum[] {LongEnum.Foo, ULongEnum.Bar});
+
+            CheckSerializeDeserialize(new Enum[]
+            {
+                ByteEnum.Foo, ByteEnum.Bar,
+                SByteEnum.Foo, SByteEnum.Bar,
+                ShortEnum.Foo, ShortEnum.Bar,
+                UShortEnum.Foo, ShortEnum.Bar,
+                IntEnum.Foo, IntEnum.Bar,
+                UIntEnum.Foo, UIntEnum.Bar,
+                LongEnum.Foo, LongEnum.Bar,
+                ULongEnum.Foo, LongEnum.Bar,
+            });
+        }
+
+        /// <summary>
+        /// Tests enums serialization when declared as System.Enum.
+        /// </summary>
+        [Test]
+        public void TestSystemEnumHolders()
+        {
+            CheckSerializeDeserialize(new RawEnumsHolder());
+            CheckSerializeDeserialize(new [] {new RawEnumsHolder(), new RawEnumsHolder()});
+            CheckSerializeDeserialize(new RawEnumsHolder2());
+            CheckSerializeDeserialize(new [] {new RawEnumsHolder2(), new RawEnumsHolder2()});
+            CheckSerializeDeserialize(new [] {new RawEnumsHolder(), new RawEnumsHolder2()});
+        }
+        
         private enum ByteEnum : byte
         {
             Foo = byte.MinValue,
@@ -550,5 +625,74 @@ namespace Apache.Ignite.Core.Tests.Binary
                 info.AddValue("ulong", ULong);
             }
         }
+
+        /// <summary>
+        /// Holds enums declared as System.Enum.
+        /// </summary>
+        #pragma warning disable 659
+        private class RawEnumsHolder
+        {
+            private Enum EnmByteRaw { get; set; } = ByteEnum.Bar;
+            private Enum EnmUByteRaw { get; set; } = SByteEnum.Foo;
+            private Enum EnmShortRaw { get; set; } = ShortEnum.Bar;
+            private Enum EnmUShortRaw { get; set; } = UShortEnum.Foo;
+            private Enum EnmIntRaw { get; set; } = IntEnum.Bar;
+            private Enum EnmUIntRaw { get; set; } = UIntEnum.Foo;
+            private Enum EnmLongRaw { get; set; } = LongEnum.Bar;
+            private Enum EnmULongRaw { get; set; } = ULongEnum.Bar;
+
+            private Enum[] EnmRawArr { get; set; } = new Enum[]
+            {
+                ByteEnum.Bar, SByteEnum.Foo, ShortEnum.Bar,
+                UShortEnum.Foo, IntEnum.Bar, UIntEnum.Foo, LongEnum.Bar, ULongEnum.Foo
+            };
+
+            public override bool Equals(object obj)
+            {
+                if (ReferenceEquals(null, obj)) return false;
+                if (ReferenceEquals(this, obj)) return true;
+                if (obj.GetType() != this.GetType()) return false;
+
+                var other = (RawEnumsHolder) obj;
+
+                return Equals(EnmByteRaw, other.EnmByteRaw) && Equals(EnmUByteRaw, other.EnmUByteRaw) &&
+                       Equals(EnmShortRaw, other.EnmShortRaw) && Equals(EnmUShortRaw, other.EnmUShortRaw) &&
+                       Equals(EnmIntRaw, other.EnmIntRaw) && Equals(EnmUIntRaw, other.EnmUIntRaw) &&
+                       Equals(EnmLongRaw, other.EnmLongRaw) && Equals(EnmULongRaw, other.EnmULongRaw) &&
+                       Enumerable.SequenceEqual(EnmRawArr, other.EnmRawArr);
+            }
+        }
+        
+        /// <summary>
+        /// Additionally holds enums holder as field mixed with other simple fields and enums.
+        /// </summary>
+        #pragma warning disable 659
+        private class RawEnumsHolder2 : RawEnumsHolder
+        {
+            private RawEnumsHolder EnmHolder { get; set; } = new RawEnumsHolder();
+
+            private object Holder { get; set; } = new RawEnumsHolder();
+
+            private int IntVal { get; set; } = 1;
+
+            private string StringVal { get; set; } = "Foo";
+
+            private Enum EnmVal1 { get; set; } = LongEnum.Bar;
+
+            private IntEnum EnmVal2 { get; set; } = IntEnum.Foo;
+
+            public override bool Equals(object obj)
+            {
+                if (ReferenceEquals(null, obj)) return false;
+                if (ReferenceEquals(this, obj)) return true;
+                if (obj.GetType() != this.GetType()) return false;
+
+                var other = (RawEnumsHolder2) obj;
+
+                return base.Equals(other) && Equals(EnmHolder, other.EnmHolder) && Equals(Holder, other.Holder) &&
+                       IntVal == other.IntVal && StringVal == other.StringVal && Equals(EnmVal1, other.EnmVal1) &&
+                       EnmVal2 == other.EnmVal2;
+            }
+        }
     }
 }
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/Model.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/Model.cs
index 065eb90c..6e84726 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/Model.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/Model.cs
@@ -125,6 +125,14 @@ namespace Apache.Ignite.Platform.Model
     {
         ALLOW, DENY
     }
+    
+    /// <summary>
+    /// A enum is a clone of Java class AccessLevel with the same namespace.
+    /// </summary>
+    public enum AccessLevel
+    {
+        USER, SUPER
+    }
 
     /// <summary>
     /// A class is a clone of Java class Role with the same namespace.
@@ -132,6 +140,9 @@ namespace Apache.Ignite.Platform.Model
     public class Role
     {
         public String Name { get; set; }
+
+        /** Tests declaration as System.Enum. */
+        public Enum AccessLevel { get; set; }
     }
 
     /// <summary>
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/PlatformTestService.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/PlatformTestService.cs
index daa1a87..53e95443 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/PlatformTestService.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/PlatformTestService.cs
@@ -527,8 +527,8 @@ namespace Apache.Ignite.Core.Tests.Services
         public User[] testUsers()
         {
             return new[] {
-                new User {Id = 1, Acl = ACL.ALLOW, Role = new Role {Name = "admin"}},
-                new User {Id = 2, Acl = ACL.DENY, Role = new Role {Name = "user"}}
+                new User {Id = 1, Acl = ACL.ALLOW, Role = new Role {Name = "admin", AccessLevel = AccessLevel.SUPER}},
+                new User {Id = 2, Acl = ACL.DENY, Role = new Role {Name = "user", AccessLevel = AccessLevel.USER}}
             };
         }
 
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/ServicesTypeAutoResolveTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/ServicesTypeAutoResolveTest.cs
index 7a36423..dd1c7ae 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/ServicesTypeAutoResolveTest.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/ServicesTypeAutoResolveTest.cs
@@ -200,7 +200,7 @@ namespace Apache.Ignite.Core.Tests.Services
         /// <summary>
         /// Tests service invocation.
         /// </summary>
-        public void DoTestService(IJavaService svc, bool isPlatform = false)
+        private void DoTestService(IJavaService svc, bool isPlatform = false)
         {
             Assert.IsNull(svc.testDepartments(null));
 
@@ -275,9 +275,11 @@ namespace Apache.Ignite.Core.Tests.Services
             Assert.AreEqual(1, users[0].Id);
             Assert.AreEqual(ACL.ALLOW, users[0].Acl);
             Assert.AreEqual("admin", users[0].Role.Name);
+            Assert.AreEqual(AccessLevel.SUPER, users[0].Role.AccessLevel);
             Assert.AreEqual(2, users[1].Id);
             Assert.AreEqual(ACL.DENY, users[1].Acl);
             Assert.AreEqual("user", users[1].Role.Name);
+            Assert.AreEqual(AccessLevel.USER, users[1].Role.AccessLevel);
 
             var users2 = svc.testRoundtrip(users);
 
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Common/TypeCaster.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Common/TypeCaster.cs
index 4f41110..c957d93 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Common/TypeCaster.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Common/TypeCaster.cs
@@ -18,8 +18,10 @@
 namespace Apache.Ignite.Core.Impl.Common
 {
     using System;
+    using System.Diagnostics;
     using System.Diagnostics.CodeAnalysis;
     using System.Linq.Expressions;
+    using System.Reflection;
 
     /// <summary>
     /// Does type casts without extra boxing. 
@@ -87,10 +89,40 @@ namespace Apache.Ignite.Core.Impl.Common
                 }
 
                 var paramExpr = Expression.Parameter(typeof(TFrom));
-                var convertExpr = Expression.Convert(paramExpr, typeof(T));
+
+                var convertExpr = typeof(TFrom) == typeof(Enum)
+                    ? TryConvertRawEnum(typeof(T), paramExpr)
+                    : Expression.Convert(paramExpr, typeof(T));
 
                 return Expression.Lambda<Func<TFrom, T>>(convertExpr, paramExpr).Compile();
             }
+
+            private static Expression TryConvertRawEnum(Type toType, Expression fromParamExpr)
+            {
+                var mtdName = "";
+
+                if (toType == typeof(byte))
+                    mtdName = "ToByte";
+                else if (toType == typeof(sbyte))
+                    mtdName = "ToSByte";
+                else if (toType == typeof(short))
+                    mtdName = "ToInt16";
+                else if (toType == typeof(ushort))
+                    mtdName = "ToUInt16";
+                else if (toType == typeof(int))
+                    mtdName = "ToInt32";
+                else if (toType == typeof(uint))
+                    mtdName = "ToUInt32";
+
+                var toIntMtd = typeof(Convert).GetMethod(mtdName, new[] {typeof(object)});
+
+                if (toIntMtd == null)
+                {
+                    throw new InvalidCastException($"Unable to convert 'System.Enum' to '{toType}': no converter found.");
+                }
+
+                return Expression.Call(null, toIntMtd, fromParamExpr);
+            }
         }
     }
 }
\ No newline at end of file