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

Commit 8739f93

Browse files
author
shigaofei
committed
BugFixed for Issue 2265
1 parent 84faf67 commit 8739f93

File tree

11 files changed

+238
-129
lines changed

11 files changed

+238
-129
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,8 @@ FakesAssemblies/
222222
# except build/, which is used as an MSBuild target.
223223
!**/packages/build/
224224
# Uncomment if necessary however generally it will be regenerated when needed
225-
#!**/packages/repositories.config
225+
#!**/packages/repositories.config
226+
227+
228+
*.vshistory
229+

ConsoleTestApp/ConsoleTestApp.csproj

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\LiteDB\LiteDB.csproj" />
12+
</ItemGroup>
13+
14+
</Project>

ConsoleTestApp/Program.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// See https://aka.ms/new-console-template for more information
2+
using LiteDB;
3+
using System.Diagnostics;
4+
5+
Console.WriteLine("Hello, World!");
6+
7+
try
8+
{
9+
var db = new LiteDatabase(":memory:");
10+
//var col = db.GetCollection<PackageA>(nameof(PackageA));
11+
var col = db.GetCollection<PackageB>(nameof(PackageB));
12+
col.EnsureIndex(x => x.Parent);
13+
}
14+
catch (Exception ex)
15+
{
16+
Debug.WriteLine(ex.Message);
17+
Debug.WriteLine(ex.StackTrace);
18+
}
19+
20+
public class PackageB
21+
{
22+
23+
[BsonRef()]
24+
public PackageB Parent { get; set; }
25+
26+
public List<PackageB> Childs { get; set; }
27+
28+
}
29+
public class PackageA
30+
{
31+
32+
[BsonRef()]
33+
public PackageC Parent { get; set; }
34+
35+
public List<PackageC> Childs { get; set; }
36+
}
37+
public class PackageC
38+
{
39+
public string Field { get; set; } = "";
40+
41+
}

LiteDB.Benchmarks/LiteDB.Benchmarks.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net472;net5</TargetFrameworks>
6-
<LangVersion>8</LangVersion>
5+
<TargetFrameworks>net7</TargetFrameworks>
6+
<LangVersion>latest</LangVersion>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
10+
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
1111
</ItemGroup>
1212

1313
<ItemGroup>
14-
<ProjectReference Include="..\LiteDB\LiteDB.csproj" />
14+
<ProjectReference Include="..\LiteDB\LiteDB.csproj" />
1515
</ItemGroup>
1616

1717
</Project>

LiteDB.Shell/LiteDB.Shell.csproj

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<TargetFramework>net5</TargetFramework>
5-
<AssemblyName>LiteDB.Shell</AssemblyName>
6-
<PackageId>LiteDB.Shell</PackageId>
7-
<OutputType>Exe</OutputType>
8-
<RootNamespace>LiteDB.Shell</RootNamespace>
9-
<AssemblyVersion>5.0.6.0</AssemblyVersion>
10-
<FileVersion>5.0.6</FileVersion>
11-
<VersionPrefix>5.0.6</VersionPrefix>
12-
<Authors>Maurício David</Authors>
13-
<Copyright>MIT</Copyright>
14-
<NeutralLanguage>en-US</NeutralLanguage>
15-
<GenerateDocumentationFile>false</GenerateDocumentationFile>
16-
<NoWarn>1701;1702;1705;1591</NoWarn>
17-
</PropertyGroup>
18-
19-
<ItemGroup>
20-
<ProjectReference Include="..\LiteDB\LiteDB.csproj" />
21-
</ItemGroup>
22-
3+
<PropertyGroup>
4+
<TargetFramework>net7</TargetFramework>
5+
<AssemblyName>LiteDB.Shell</AssemblyName>
6+
<PackageId>LiteDB.Shell</PackageId>
7+
<OutputType>Exe</OutputType>
8+
<RootNamespace>LiteDB.Shell</RootNamespace>
9+
<AssemblyVersion>5.0.6.0</AssemblyVersion>
10+
<FileVersion>5.0.6</FileVersion>
11+
<VersionPrefix>5.0.6</VersionPrefix>
12+
<Authors>Maurício David</Authors>
13+
<Copyright>MIT</Copyright>
14+
<NeutralLanguage>en-US</NeutralLanguage>
15+
<GenerateDocumentationFile>false</GenerateDocumentationFile>
16+
<NoWarn>1701;1702;1705;1591</NoWarn>
17+
</PropertyGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\LiteDB\LiteDB.csproj" />
21+
</ItemGroup>
22+
2323
</Project>

LiteDB.Stress/LiteDB.Stress.csproj

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFramework>net5</TargetFramework>
6-
</PropertyGroup>
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7</TargetFramework>
6+
</PropertyGroup>
77

8-
<ItemGroup>
9-
<ProjectReference Include="..\LiteDB\LiteDB.csproj" />
10-
</ItemGroup>
8+
<ItemGroup>
9+
<ProjectReference Include="..\LiteDB\LiteDB.csproj" />
10+
</ItemGroup>
1111

1212
</Project>
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using Xunit;
5+
6+
namespace LiteDB.Tests.Issues;
7+
8+
public class Issue2265_Tests
9+
{
10+
11+
[Fact]
12+
public void TestRefSelf()
13+
{
14+
try
15+
{
16+
var db = new LiteDatabase(":memory:");
17+
var col = db.GetCollection<Package>(nameof(Package));
18+
col.EnsureIndex(x => x.Parent);
19+
}
20+
catch (Exception ex)
21+
{
22+
Debug.WriteLine(ex.Message);
23+
Debug.WriteLine(ex.StackTrace);
24+
}
25+
//col.EnsureIndex(x => x.Accounts2to1);
26+
//col.InsertBulk(this.GenerateItems());
27+
//col.DeleteAll();
28+
//col.InsertBulk(this.GenerateItems());
29+
}
30+
31+
}
32+
33+
34+
35+
36+
public class Package
37+
{
38+
39+
[BsonRef()]
40+
public Package Parent { get; set; }
41+
42+
public List<Package> Childs { get; set; }
43+
44+
}

LiteDB.Tests/LiteDB.Tests.csproj

+38-38
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<TargetFramework>net5</TargetFramework>
5-
<AssemblyName>LiteDB.Tests</AssemblyName>
6-
<RootNamespace>LiteDB.Tests</RootNamespace>
7-
<Authors>Maurício David</Authors>
8-
<Copyright>MIT</Copyright>
9-
<NeutralLanguage>en-US</NeutralLanguage>
10-
<GenerateDocumentationFile>false</GenerateDocumentationFile>
11-
<NoWarn>1701;1702;1705;1591;0618</NoWarn>
12-
</PropertyGroup>
3+
<PropertyGroup>
4+
<TargetFramework>net7</TargetFramework>
5+
<AssemblyName>LiteDB.Tests</AssemblyName>
6+
<RootNamespace>LiteDB.Tests</RootNamespace>
7+
<Authors>Maurício David</Authors>
8+
<Copyright>MIT</Copyright>
9+
<NeutralLanguage>en-US</NeutralLanguage>
10+
<GenerateDocumentationFile>false</GenerateDocumentationFile>
11+
<NoWarn>1701;1702;1705;1591;0618</NoWarn>
12+
</PropertyGroup>
1313

14-
<ItemGroup Condition="'$(Configuration)' == 'Release'">
15-
<Compile Remove="Internals\**" />
16-
</ItemGroup>
14+
<ItemGroup Condition="'$(Configuration)' == 'Release'">
15+
<Compile Remove="Internals\**" />
16+
</ItemGroup>
1717

18-
<ItemGroup>
19-
<None Remove="Utils\Json\person.json" />
20-
<None Remove="Utils\Json\zip.json" />
21-
</ItemGroup>
18+
<ItemGroup>
19+
<None Remove="Utils\Json\person.json" />
20+
<None Remove="Utils\Json\zip.json" />
21+
</ItemGroup>
2222

23-
<ItemGroup>
24-
<EmbeddedResource Include="Utils\Json\person.json" />
25-
<EmbeddedResource Include="Utils\Json\zip.json" />
26-
</ItemGroup>
23+
<ItemGroup>
24+
<EmbeddedResource Include="Utils\Json\person.json" />
25+
<EmbeddedResource Include="Utils\Json\zip.json" />
26+
</ItemGroup>
2727

28-
<ItemGroup>
29-
<PackageReference Include="FluentAssertions" Version="5.10.2" />
30-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
31-
<PackageReference Include="xunit" Version="2.4.2" />
32-
<PackageReference Include="xunit.runner.console" Version="2.4.2">
33-
<PrivateAssets>all</PrivateAssets>
34-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
35-
</PackageReference>
36-
<PackageReference Include="xunit.runner.reporters" Version="2.4.2" />
37-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
38-
<PrivateAssets>all</PrivateAssets>
39-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
40-
</PackageReference>
41-
</ItemGroup>
28+
<ItemGroup>
29+
<PackageReference Include="FluentAssertions" Version="5.10.2" />
30+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
31+
<PackageReference Include="xunit" Version="2.4.2" />
32+
<PackageReference Include="xunit.runner.console" Version="2.4.2">
33+
<PrivateAssets>all</PrivateAssets>
34+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
35+
</PackageReference>
36+
<PackageReference Include="xunit.runner.reporters" Version="2.4.2" />
37+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
38+
<PrivateAssets>all</PrivateAssets>
39+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
40+
</PackageReference>
41+
</ItemGroup>
4242

43-
<ItemGroup>
44-
<ProjectReference Include="..\LiteDB\LiteDB.csproj" />
45-
</ItemGroup>
43+
<ItemGroup>
44+
<ProjectReference Include="..\LiteDB\LiteDB.csproj" />
45+
</ItemGroup>
4646

4747
</Project>

LiteDB.sln

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LiteDB.Benchmarks", "LiteDB
1313
EndProject
1414
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LiteDB.Stress", "LiteDB.Stress\LiteDB.Stress.csproj", "{FFBC5669-DA32-4907-8793-7B414279DA3B}"
1515
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTestApp", "ConsoleTestApp\ConsoleTestApp.csproj", "{5CB20D6E-FB98-4680-A568-765F9351CD84}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -39,6 +41,10 @@ Global
3941
{FFBC5669-DA32-4907-8793-7B414279DA3B}.Debug|Any CPU.Build.0 = Debug|Any CPU
4042
{FFBC5669-DA32-4907-8793-7B414279DA3B}.Release|Any CPU.ActiveCfg = Release|Any CPU
4143
{FFBC5669-DA32-4907-8793-7B414279DA3B}.Release|Any CPU.Build.0 = Release|Any CPU
44+
{5CB20D6E-FB98-4680-A568-765F9351CD84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45+
{5CB20D6E-FB98-4680-A568-765F9351CD84}.Debug|Any CPU.Build.0 = Debug|Any CPU
46+
{5CB20D6E-FB98-4680-A568-765F9351CD84}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{5CB20D6E-FB98-4680-A568-765F9351CD84}.Release|Any CPU.Build.0 = Release|Any CPU
4248
EndGlobalSection
4349
GlobalSection(SolutionProperties) = preSolution
4450
HideSolutionNode = FALSE

LiteDB/Client/Mapper/BsonMapper.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,7 @@ internal EntityMapper GetEntityMapper(Type type)
240240
lock (_entities)
241241
{
242242
if (!_entities.TryGetValue(type, out mapper))
243-
{
244-
return _entities[type] = this.BuildEntityMapper(type);
245-
}
243+
return this.BuildAddEntityMapper(type);
246244
}
247245
}
248246

@@ -253,9 +251,10 @@ internal EntityMapper GetEntityMapper(Type type)
253251
/// Use this method to override how your class can be, by default, mapped from entity to Bson document.
254252
/// Returns an EntityMapper from each requested Type
255253
/// </summary>
256-
protected virtual EntityMapper BuildEntityMapper(Type type)
254+
protected virtual EntityMapper BuildAddEntityMapper(Type type)
257255
{
258256
var mapper = new EntityMapper(type);
257+
_entities[type] = mapper;//direct add into entities, to solove the DBRef [ GetEntityMapper > BuildAddEntityMapper > RegisterDbRef > RegisterDbRefItem > GetEntityMapper ] Loop call recursion,we stoped at here and GetEntityMapper's _entities.TryGetValue
259258

260259
var idAttr = typeof(BsonIdAttribute);
261260
var ignoreAttr = typeof(BsonIgnoreAttribute);
@@ -478,6 +477,7 @@ internal static void RegisterDbRef(BsonMapper mapper, MemberMapper member, IType
478477
/// </summary>
479478
private static void RegisterDbRefItem(BsonMapper mapper, MemberMapper member, ITypeNameBinder typeNameBinder, string collection)
480479
{
480+
481481
// get entity
482482
var entity = mapper.GetEntityMapper(member.DataType);
483483

0 commit comments

Comments
 (0)