Skip to content
This repository was archived by the owner on Jul 6, 2020. It is now read-only.

Commit

Permalink
Fixed stack-overflow exception on self reference entities.
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Nov 19, 2016
1 parent 0487229 commit c1355d8
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,5 @@ FakesAssemblies/
# LightSwitch generated files
GeneratedArtifacts/
_Pvt_Extensions/
ModelManifest.xml
ModelManifest.xml
/.vs
94 changes: 94 additions & 0 deletions JqGridHelper.Tests/JqGridHelper.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C23CFFD9-DB0E-44B4-A48B-0CF109095572}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>JqGridHelper.Tests</RootNamespace>
<AssemblyName>JqGridHelper.Tests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
</ItemGroup>
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="Models\Role.cs" />
<Compile Include="Models\User.cs" />
<Compile Include="UnitTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\JqGridHelper\JqGridHelper\JqGridHelper.csproj">
<Project>{066b6179-f8ad-44b4-8f6e-4e75c95d6c3a}</Project>
<Name>JqGridHelper</Name>
</ProjectReference>
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
</ItemGroup>
</When>
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
9 changes: 9 additions & 0 deletions JqGridHelper.Tests/Models/Role.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace JqGridHelper.Tests.Models
{
public class Role
{
public Role ParentRole { get; set; }

public string Name { get; set; }
}
}
9 changes: 9 additions & 0 deletions JqGridHelper.Tests/Models/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace JqGridHelper.Tests.Models
{
public class User
{
public string UserName { get; set; }

public Role Role { get; set; }
}
}
36 changes: 36 additions & 0 deletions JqGridHelper.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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")]
38 changes: 38 additions & 0 deletions JqGridHelper.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
5 changes: 3 additions & 2 deletions JqGridHelper/JqGridHelper/Utils/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 8 additions & 2 deletions Samples.sln
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c1355d8

Please sign in to comment.