Skip to content

Commit 86375e6

Browse files
committed
.Net 10
1 parent 81c4e5d commit 86375e6

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

src/EntityFrameworkCore.SqlServer.SimpleBulks.Tests/DbContextExtensions/GetPropertiesTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,28 +288,37 @@ public void GetProperties_JsonComplexType_ReturnsCorrectColumnInformation()
288288

289289
// Verify that the JsonPropertyWriters have correct structure
290290
var writers = property.JsonPropertyWriters;
291+
var flattenedWriters = property.GetFlattenedJsonPropertyWriters();
291292
Assert.Equal(2, writers.Count); // Street and Location
292293

293294
var streetWriter = writers.FirstOrDefault(w => w.ClrPropertyName == "Street");
294295
Assert.NotNull(streetWriter);
295296
Assert.Equal("Street", streetWriter.JsonPropertyName);
297+
Assert.Equal("Street", streetWriter.FullJsonPath);
298+
Assert.Equal("Street", streetWriter.FullClrPropertyName);
296299
Assert.Equal(typeof(string), streetWriter.PropertyType);
297300
Assert.False(streetWriter.IsNestedComplexType);
298301

299302
var locationWriter = writers.FirstOrDefault(w => w.ClrPropertyName == "Location");
300303
Assert.NotNull(locationWriter);
301304
Assert.Equal("xxx", locationWriter.JsonPropertyName); // Mapped to "xxx" in TestDbContext
305+
Assert.Equal("xxx", locationWriter.FullJsonPath);
306+
Assert.Equal("Location", locationWriter.FullClrPropertyName);
302307
Assert.Equal(typeof(ComplexTypeLocation), locationWriter.PropertyType);
303308
Assert.True(locationWriter.IsNestedComplexType);
304309
Assert.NotNull(locationWriter.NestedProperties);
305310
Assert.Equal(2, locationWriter.NestedProperties.Count); // Lat and Lng
306311

307312
var latWriter = locationWriter.NestedProperties.FirstOrDefault(w => w.ClrPropertyName == "Lat");
308313
Assert.NotNull(latWriter);
314+
Assert.Equal("xxx.Lat", latWriter.FullJsonPath);
315+
Assert.Equal("Location.Lat", latWriter.FullClrPropertyName);
309316
Assert.Equal(typeof(double), latWriter.PropertyType);
310317

311318
var lngWriter = locationWriter.NestedProperties.FirstOrDefault(w => w.ClrPropertyName == "Lng");
312319
Assert.NotNull(lngWriter);
320+
Assert.Equal("xxx.Lng", lngWriter.FullJsonPath);
321+
Assert.Equal("Location.Lng", lngWriter.FullClrPropertyName);
313322
Assert.Equal(typeof(double), lngWriter.PropertyType);
314323

315324
// Verify serialization using JsonPropertyWriters

src/EntityFrameworkCore.SqlServer.SimpleBulks/ColumnInfor.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,31 @@ public class ColumnInfor
2929
public bool IsJson { get; set; }
3030

3131
public IReadOnlyList<JsonPropertyWriter>? JsonPropertyWriters { get; init; }
32+
33+
public IReadOnlyDictionary<string, JsonPropertyWriter> GetFlattenedJsonPropertyWriters()
34+
{
35+
if (JsonPropertyWriters == null || JsonPropertyWriters.Count == 0)
36+
{
37+
return new Dictionary<string, JsonPropertyWriter>();
38+
}
39+
40+
var result = new Dictionary<string, JsonPropertyWriter>();
41+
FlattenJsonPropertyWriters(JsonPropertyWriters, result);
42+
return result;
43+
}
44+
45+
private static void FlattenJsonPropertyWriters(IReadOnlyList<JsonPropertyWriter> writers, Dictionary<string, JsonPropertyWriter> result)
46+
{
47+
foreach (var writer in writers)
48+
{
49+
result[writer.FullClrPropertyName] = writer;
50+
51+
if (writer.IsNestedComplexType)
52+
{
53+
FlattenJsonPropertyWriters(writer.NestedProperties, result);
54+
}
55+
}
56+
}
3257
}
3358

3459
public class JsonPropertyWriter
@@ -37,6 +62,10 @@ public class JsonPropertyWriter
3762

3863
public string ClrPropertyName { get; init; }
3964

65+
public string FullJsonPath { get; init; }
66+
67+
public string FullClrPropertyName { get; init; }
68+
4069
public Type PropertyType { get; init; }
4170

4271
public JsonValueReaderWriter? ReaderWriter { get; init; }

src/EntityFrameworkCore.SqlServer.SimpleBulks/Extensions/DbContextExtensions.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private static void AddJsonComplexProperty(List<ColumnInfor> columnInfors, IComp
211211
});
212212
}
213213

214-
private static List<JsonPropertyWriter> BuildJsonPropertyWriters(IComplexType complexType)
214+
private static List<JsonPropertyWriter> BuildJsonPropertyWriters(IComplexType complexType, string clrPrefix = "", string jsonPrefix = "")
215215
{
216216
var writers = new List<JsonPropertyWriter>();
217217

@@ -225,6 +225,8 @@ private static List<JsonPropertyWriter> BuildJsonPropertyWriters(IComplexType co
225225
{
226226
JsonPropertyName = jsonPropertyName,
227227
ClrPropertyName = property.Name,
228+
FullJsonPath = string.IsNullOrEmpty(jsonPrefix) ? jsonPropertyName : $"{jsonPrefix}.{jsonPropertyName}",
229+
FullClrPropertyName = string.IsNullOrEmpty(clrPrefix) ? property.Name : $"{clrPrefix}.{property.Name}",
228230
PropertyType = property.ClrType,
229231
ReaderWriter = readerWriter,
230232
NestedProperties = null
@@ -235,12 +237,16 @@ private static List<JsonPropertyWriter> BuildJsonPropertyWriters(IComplexType co
235237
foreach (var nestedComplexProperty in complexType.GetComplexProperties())
236238
{
237239
var jsonPropertyName = nestedComplexProperty.GetJsonPropertyName() ?? nestedComplexProperty.Name;
238-
var nestedWriters = BuildJsonPropertyWriters(nestedComplexProperty.ComplexType);
240+
var fullJsonPath = string.IsNullOrEmpty(jsonPrefix) ? jsonPropertyName : $"{jsonPrefix}.{jsonPropertyName}";
241+
var fullClrPropertyName = string.IsNullOrEmpty(clrPrefix) ? nestedComplexProperty.Name : $"{clrPrefix}.{nestedComplexProperty.Name}";
242+
var nestedWriters = BuildJsonPropertyWriters(nestedComplexProperty.ComplexType, fullClrPropertyName, fullJsonPath);
239243

240244
writers.Add(new JsonPropertyWriter
241245
{
242246
JsonPropertyName = jsonPropertyName,
243247
ClrPropertyName = nestedComplexProperty.Name,
248+
FullJsonPath = fullJsonPath,
249+
FullClrPropertyName = fullClrPropertyName,
244250
PropertyType = nestedComplexProperty.ClrType,
245251
ReaderWriter = null,
246252
NestedProperties = nestedWriters

0 commit comments

Comments
 (0)