Skip to content

Commit 4779fff

Browse files
committed
fix: AddValueObjectConverters must check inside a complex value objects not just for scalar but for complex properties as well
1 parent 41fe85e commit 4779fff

File tree

9 files changed

+104
-1
lines changed

9 files changed

+104
-1
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Copyright>(c) $([System.DateTime]::Now.Year), Pawel Gerr. All rights reserved.</Copyright>
5-
<VersionPrefix>8.5.1</VersionPrefix>
5+
<VersionPrefix>8.5.2</VersionPrefix>
66
<Authors>Pawel Gerr</Authors>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<PackageProjectUrl>https://github.com/PawelGerr/Thinktecture.Runtime.Extensions</PackageProjectUrl>

src/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Sources/EntityFrameworkCore/Conventions/ValueObjectConventionPlugin.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ private static void AddNonKeyedValueObjectMembers(IConventionEntityTypeBuilder e
7575

7676
foreach (var memberName in members)
7777
{
78+
#if COMPLEX_TYPES
79+
var complexProperty = entity.FindComplexProperty(memberName);
80+
81+
if (complexProperty is not null)
82+
continue;
83+
#endif
84+
7885
var property = entity.FindProperty(memberName);
7986

8087
if (property is null)

src/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Sources/Extensions/EntityTypeBuilderExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,13 @@ private static void AddNonKeyedValueObjectMembers(
285285

286286
foreach (var memberName in members)
287287
{
288+
#if COMPLEX_TYPES
289+
var complexProperty = entity.FindComplexProperty(memberName);
290+
291+
if (complexProperty is not null)
292+
continue;
293+
#endif
294+
288295
var property = entity.FindProperty(memberName);
289296

290297
if (property is null)

test/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Tests.Sources/EntityFrameworkCore/ValueConversion/ValueObjectValueConverterFactoryTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,25 @@ await _ctx.TestEntities_with_Enum_and_ValueObjects
122122
.ToListAsync();
123123
}
124124

125+
#if COMPLEX_TYPES
126+
[Fact]
127+
public async Task Should_roundtrip_complex_value_object_with_complex_property()
128+
{
129+
var entity = ComplexValueObjectWithComplexType.Create(
130+
new Guid("00B7B411-A95E-41F7-91C8-29384431E21A"),
131+
new TestComplexType { TestEnum = TestEnum.Item1 });
132+
133+
_ctx.Add(entity);
134+
await _ctx.SaveChangesAsync();
135+
136+
_ctx.ChangeTracker.Clear();
137+
138+
var loadedEntity = await _ctx.ComplexValueObject_with_ComplexType.SingleAsync();
139+
loadedEntity.Id.Should().Be(entity.Id);
140+
loadedEntity.TestComplexType.Should().BeEquivalentTo(entity.TestComplexType);
141+
}
142+
#endif
143+
125144
public void Dispose()
126145
{
127146
_ctx.Dispose();

test/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Tests.Sources/Extensions/EntityTypeBuilderExtensionsTests/AddValueObjectConverters.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ public void Should_add_converters_for_complex_types_inside_complex_type_configur
5959

6060
ValidateConverter(complexProperty.ComplexType, nameof(TestComplexType.TestEnum));
6161
}
62+
63+
[Fact]
64+
public void Should_add_converters_for_complex_types_inside_complex_value_object()
65+
{
66+
var entityType = _ctx.Model.FindEntityType(typeof(ComplexValueObjectWithComplexType)) ?? throw new Exception("Entity not found");
67+
var complexProperty = entityType.FindComplexProperty(nameof(ComplexValueObjectWithComplexType.TestComplexType)) ?? throw new Exception("Complex type property not found");
68+
69+
ValidateConverter(complexProperty.ComplexType, nameof(TestComplexType.TestEnum));
70+
}
6271
#endif
6372

6473
[Fact]

test/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Tests.Sources/Extensions/ModelBuilderExtensionsTests/AddValueObjectConverters.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ public void Should_add_converters_for_complex_types()
4646

4747
ValidateConverter(complexProperty.ComplexType, nameof(TestComplexType.TestEnum));
4848
}
49+
50+
[Fact]
51+
public void Should_add_converters_for_complex_types_inside_complex_value_object()
52+
{
53+
var entityType = _ctx.Model.FindEntityType(typeof(ComplexValueObjectWithComplexType)) ?? throw new Exception("Entity not found");
54+
var complexProperty = entityType.FindComplexProperty(nameof(ComplexValueObjectWithComplexType.TestComplexType)) ?? throw new Exception("Complex type property not found");
55+
56+
ValidateConverter(complexProperty.ComplexType, nameof(TestComplexType.TestEnum));
57+
}
4958
#endif
5059

5160
[Fact]

test/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Tests.Sources/Extensions/ValueObjectDbContextOptionsBuilderExtensionsTests/UseValueObjectValueConverter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ public void Should_add_converters_for_complex_types()
5555

5656
ValidateConverter(complexProperty.ComplexType, nameof(TestComplexType.TestEnum));
5757
}
58+
59+
[Fact]
60+
public void Should_add_converters_for_complex_types_inside_complex_value_object()
61+
{
62+
var entityType = _ctx.Model.FindEntityType(typeof(ComplexValueObjectWithComplexType)) ?? throw new Exception("Entity not found");
63+
var complexProperty = entityType.FindComplexProperty(nameof(ComplexValueObjectWithComplexType.TestComplexType)) ?? throw new Exception("Complex type property not found");
64+
65+
ValidateConverter(complexProperty.ComplexType, nameof(TestComplexType.TestEnum));
66+
}
5867
#endif
5968

6069
[Fact]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#if COMPLEX_TYPES
2+
using System;
3+
using Microsoft.EntityFrameworkCore;
4+
5+
namespace Thinktecture.Runtime.Tests.TestEntities;
6+
7+
[ComplexValueObject]
8+
public partial class ComplexValueObjectWithComplexType
9+
{
10+
public Guid Id { get; }
11+
public TestComplexType TestComplexType { get; }
12+
13+
private ComplexValueObjectWithComplexType(Guid id)
14+
{
15+
Id = id;
16+
}
17+
18+
public static void Configure(
19+
ModelBuilder modelBuilder,
20+
ValueConverterRegistration valueConverterRegistration)
21+
{
22+
modelBuilder.Entity<ComplexValueObjectWithComplexType>(builder =>
23+
{
24+
builder.Property(p => p.Id);
25+
builder.ComplexProperty(e => e.TestComplexType,
26+
b =>
27+
{
28+
b.IsRequired();
29+
b.Property(t => t.TestEnum);
30+
31+
if (valueConverterRegistration == ValueConverterRegistration.ComplexTypeConfiguration)
32+
b.AddValueObjectConverters(true);
33+
});
34+
35+
if (valueConverterRegistration == ValueConverterRegistration.EntityConfiguration)
36+
builder.AddValueObjectConverters(true);
37+
});
38+
}
39+
}
40+
41+
#endif

test/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Tests.Sources/TestEntities/TestDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class TestDbContext : DbContext
1212

1313
#if COMPLEX_TYPES
1414
public DbSet<TestEntityWithComplexType> TestEntities_with_ComplexType { get; set; }
15+
public DbSet<ComplexValueObjectWithComplexType> ComplexValueObject_with_ComplexType { get; set; }
1516
#endif
1617

1718
public TestDbContext(
@@ -40,6 +41,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
4041

4142
#if COMPLEX_TYPES
4243
TestEntityWithComplexType.Configure(modelBuilder, _valueConverterRegistration);
44+
ComplexValueObjectWithComplexType.Configure(modelBuilder, _valueConverterRegistration);
4345
#endif
4446

4547
if (_valueConverterRegistration == ValueConverterRegistration.OnModelCreating)

0 commit comments

Comments
 (0)