From c1355d89454b017c20bb0745e9504000fc6978ae Mon Sep 17 00:00:00 2001 From: VahidN Date: Sat, 19 Nov 2016 12:23:13 +0330 Subject: [PATCH] Fixed stack-overflow exception on self reference entities. --- .gitignore | 3 +- JqGridHelper.Tests/JqGridHelper.Tests.csproj | 94 +++++++++++++++++++ JqGridHelper.Tests/Models/Role.cs | 9 ++ JqGridHelper.Tests/Models/User.cs | 9 ++ JqGridHelper.Tests/Properties/AssemblyInfo.cs | 36 +++++++ JqGridHelper.Tests/UnitTests.cs | 38 ++++++++ .../JqGridHelper/Utils/ReflectionHelper.cs | 5 +- Samples.sln | 10 +- 8 files changed, 199 insertions(+), 5 deletions(-) create mode 100644 JqGridHelper.Tests/JqGridHelper.Tests.csproj create mode 100644 JqGridHelper.Tests/Models/Role.cs create mode 100644 JqGridHelper.Tests/Models/User.cs create mode 100644 JqGridHelper.Tests/Properties/AssemblyInfo.cs create mode 100644 JqGridHelper.Tests/UnitTests.cs diff --git a/.gitignore b/.gitignore index 7964536..0603585 100644 --- a/.gitignore +++ b/.gitignore @@ -186,4 +186,5 @@ FakesAssemblies/ # LightSwitch generated files GeneratedArtifacts/ _Pvt_Extensions/ -ModelManifest.xml \ No newline at end of file +ModelManifest.xml +/.vs diff --git a/JqGridHelper.Tests/JqGridHelper.Tests.csproj b/JqGridHelper.Tests/JqGridHelper.Tests.csproj new file mode 100644 index 0000000..e66e012 --- /dev/null +++ b/JqGridHelper.Tests/JqGridHelper.Tests.csproj @@ -0,0 +1,94 @@ + + + + Debug + AnyCPU + {C23CFFD9-DB0E-44B4-A48B-0CF109095572} + Library + Properties + JqGridHelper.Tests + JqGridHelper.Tests + v4.0 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + 3.5 + + + + + + + + + + + + + + + + + + + + + + + {066b6179-f8ad-44b4-8f6e-4e75c95d6c3a} + JqGridHelper + + + + + + + False + + + False + + + False + + + False + + + + + + + + \ No newline at end of file diff --git a/JqGridHelper.Tests/Models/Role.cs b/JqGridHelper.Tests/Models/Role.cs new file mode 100644 index 0000000..59b754b --- /dev/null +++ b/JqGridHelper.Tests/Models/Role.cs @@ -0,0 +1,9 @@ +namespace JqGridHelper.Tests.Models +{ + public class Role + { + public Role ParentRole { get; set; } + + public string Name { get; set; } + } +} \ No newline at end of file diff --git a/JqGridHelper.Tests/Models/User.cs b/JqGridHelper.Tests/Models/User.cs new file mode 100644 index 0000000..4b00339 --- /dev/null +++ b/JqGridHelper.Tests/Models/User.cs @@ -0,0 +1,9 @@ +namespace JqGridHelper.Tests.Models +{ + public class User + { + public string UserName { get; set; } + + public Role Role { get; set; } + } +} \ No newline at end of file diff --git a/JqGridHelper.Tests/Properties/AssemblyInfo.cs b/JqGridHelper.Tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..d421c52 --- /dev/null +++ b/JqGridHelper.Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("JqGridHelper.Tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("JqGridHelper.Tests")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("c23cffd9-db0e-44b4-a48b-0cf109095572")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/JqGridHelper.Tests/UnitTests.cs b/JqGridHelper.Tests/UnitTests.cs new file mode 100644 index 0000000..5bfced5 --- /dev/null +++ b/JqGridHelper.Tests/UnitTests.cs @@ -0,0 +1,38 @@ +using JqGridHelper.Tests.Models; +using JqGridHelper.Utils; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace JqGridHelper.Tests +{ + [TestClass] + public class UnitTests + { + [TestMethod] + public void Test_Self_Referencing_Entities() + { + var role1 = new Role + { + Name = "Role 1", + ParentRole = null + }; + + var role2 = new Role + { + Name = "Role 2", + ParentRole = role1 + }; + + var obj = new User + { + UserName = "User 1", + Role = role2 + }; + + var type = obj.GetType().FindFieldType("Role.Name", dumpLevel: 3); + Assert.AreEqual(expected: typeof(string), actual: type); + + type = obj.GetType().FindFieldType("UserName", dumpLevel: 3); + Assert.AreEqual(expected: typeof(string), actual: type); + } + } +} \ No newline at end of file diff --git a/JqGridHelper/JqGridHelper/Utils/ReflectionHelper.cs b/JqGridHelper/JqGridHelper/Utils/ReflectionHelper.cs index cf238a2..317351b 100644 --- a/JqGridHelper/JqGridHelper/Utils/ReflectionHelper.cs +++ b/JqGridHelper/JqGridHelper/Utils/ReflectionHelper.cs @@ -11,12 +11,13 @@ public static Type FindFieldType(this Type type, string fieldName, string parent if (parent + property.Name == fieldName) return property.PropertyType; - if (parent.Split('.').Length > dumpLevel) + if (parent.Split('.').Length > dumpLevel || + property.PropertyType.Name.Equals(parent.TrimEnd('.'))) continue; if (isNestedProperty(property.PropertyType)) { - var result = FindFieldType(property.PropertyType, fieldName, property.Name + "."); + var result = FindFieldType(property.PropertyType, fieldName, property.Name + ".", dumpLevel); if (result != null) return result; } diff --git a/Samples.sln b/Samples.sln index e0b5fcd..3cd33a6 100644 --- a/Samples.sln +++ b/Samples.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.31101.0 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "jqGrid01", "jqGrid01\jqGrid01\jqGrid01.csproj", "{B3A275D1-CC40-464C-8189-62F1C03906D7}" EndProject @@ -44,6 +44,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JqGridHelper.Tests", "JqGridHelper.Tests\JqGridHelper.Tests.csproj", "{C23CFFD9-DB0E-44B4-A48B-0CF109095572}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -106,6 +108,10 @@ Global {62A1B06A-7D18-4304-9389-A680655C5003}.Debug|Any CPU.Build.0 = Debug|Any CPU {62A1B06A-7D18-4304-9389-A680655C5003}.Release|Any CPU.ActiveCfg = Release|Any CPU {62A1B06A-7D18-4304-9389-A680655C5003}.Release|Any CPU.Build.0 = Release|Any CPU + {C23CFFD9-DB0E-44B4-A48B-0CF109095572}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C23CFFD9-DB0E-44B4-A48B-0CF109095572}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C23CFFD9-DB0E-44B4-A48B-0CF109095572}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C23CFFD9-DB0E-44B4-A48B-0CF109095572}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE