Skip to content

Commit cc7bd17

Browse files
committed
.Net 10
1 parent 815567d commit cc7bd17

8 files changed

Lines changed: 332 additions & 185 deletions

File tree

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

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -293,15 +293,15 @@ public void GetProperties_JsonComplexType_ReturnsCorrectColumnInformation()
293293
Assert.NotNull(streetWriter);
294294
Assert.Equal("Street", streetWriter.JsonPropertyName);
295295
Assert.Equal("Street", streetWriter.FullJsonPropertyName);
296-
Assert.Equal("Street", streetWriter.FullClrPropertyName);
296+
Assert.Equal("ShippingAddress.Street", streetWriter.FullClrPropertyName);
297297
Assert.Equal(typeof(string), streetWriter.PropertyType);
298298
Assert.False(streetWriter.IsNestedComplexType);
299299

300300
var locationWriter = jsonProperties.FirstOrDefault(w => w.ClrPropertyName == "Location");
301301
Assert.NotNull(locationWriter);
302302
Assert.Equal("xxx", locationWriter.JsonPropertyName); // Mapped to "xxx" in TestDbContext
303303
Assert.Equal("xxx", locationWriter.FullJsonPropertyName);
304-
Assert.Equal("Location", locationWriter.FullClrPropertyName);
304+
Assert.Equal("ShippingAddress.Location", locationWriter.FullClrPropertyName);
305305
Assert.Equal(typeof(ComplexTypeLocation), locationWriter.PropertyType);
306306
Assert.True(locationWriter.IsNestedComplexType);
307307
Assert.NotNull(locationWriter.NestedProperties);
@@ -310,27 +310,32 @@ public void GetProperties_JsonComplexType_ReturnsCorrectColumnInformation()
310310
var latWriter = locationWriter.NestedProperties.FirstOrDefault(w => w.ClrPropertyName == "Lat");
311311
Assert.NotNull(latWriter);
312312
Assert.Equal("xxx.Lat", latWriter.FullJsonPropertyName);
313-
Assert.Equal("Location.Lat", latWriter.FullClrPropertyName);
313+
Assert.Equal("ShippingAddress.Location.Lat", latWriter.FullClrPropertyName);
314314
Assert.Equal(typeof(double), latWriter.PropertyType);
315315

316316
var lngWriter = locationWriter.NestedProperties.FirstOrDefault(w => w.ClrPropertyName == "Lng");
317317
Assert.NotNull(lngWriter);
318318
Assert.Equal("xxx.Lng", lngWriter.FullJsonPropertyName);
319-
Assert.Equal("Location.Lng", lngWriter.FullClrPropertyName);
319+
Assert.Equal("ShippingAddress.Location.Lng", lngWriter.FullClrPropertyName);
320320
Assert.Equal(typeof(double), lngWriter.PropertyType);
321321

322322
// Verify serialization using JsonProperties
323-
var testAddress = new ComplexTypeAddress
323+
324+
var testOrder = new JsonComplexTypeOrder
324325
{
325-
Street = "123 Main St",
326-
Location = new ComplexTypeLocation
326+
Id = 1,
327+
ShippingAddress = new ComplexTypeAddress
327328
{
328-
Lat = 40.7128,
329-
Lng = -74.0060
329+
Street = "123 Main St",
330+
Location = new ComplexTypeLocation
331+
{
332+
Lat = 40.7128,
333+
Lng = -74.0060
334+
}
330335
}
331336
};
332337

333-
var json = JsonProperty.Serialize(testAddress, jsonProperties);
338+
var json = JsonPropertyWriter.Write(testOrder, flattenedJsonProperties);
334339
Assert.NotNull(json);
335340
Assert.Contains("Street", json);
336341
Assert.Contains("123 Main St", json);
@@ -382,15 +387,15 @@ public void GetProperties_JsonOwnedType_ReturnsCorrectColumnInformation()
382387
Assert.NotNull(streetWriter);
383388
Assert.Equal("Street", streetWriter.JsonPropertyName);
384389
Assert.Equal("Street", streetWriter.FullJsonPropertyName);
385-
Assert.Equal("Street", streetWriter.FullClrPropertyName);
390+
Assert.Equal("ShippingAddress.Street", streetWriter.FullClrPropertyName);
386391
Assert.Equal(typeof(string), streetWriter.PropertyType);
387392
Assert.False(streetWriter.IsNestedComplexType);
388393

389394
var locationWriter = jsonProperties.FirstOrDefault(w => w.ClrPropertyName == "Location");
390395
Assert.NotNull(locationWriter);
391396
Assert.Equal("xxx", locationWriter.JsonPropertyName); // Mapped to "xxx" in TestDbContext
392397
Assert.Equal("xxx", locationWriter.FullJsonPropertyName);
393-
Assert.Equal("Location", locationWriter.FullClrPropertyName);
398+
Assert.Equal("ShippingAddress.Location", locationWriter.FullClrPropertyName);
394399
Assert.Equal(typeof(OwnedTypeLocation), locationWriter.PropertyType);
395400
Assert.True(locationWriter.IsNestedComplexType);
396401
Assert.NotNull(locationWriter.NestedProperties);
@@ -399,27 +404,31 @@ public void GetProperties_JsonOwnedType_ReturnsCorrectColumnInformation()
399404
var latWriter = locationWriter.NestedProperties.FirstOrDefault(w => w.ClrPropertyName == "Lat");
400405
Assert.NotNull(latWriter);
401406
Assert.Equal("xxx.Lat", latWriter.FullJsonPropertyName);
402-
Assert.Equal("Location.Lat", latWriter.FullClrPropertyName);
407+
Assert.Equal("ShippingAddress.Location.Lat", latWriter.FullClrPropertyName);
403408
Assert.Equal(typeof(double), latWriter.PropertyType);
404409

405410
var lngWriter = locationWriter.NestedProperties.FirstOrDefault(w => w.ClrPropertyName == "Lng");
406411
Assert.NotNull(lngWriter);
407412
Assert.Equal("xxx.Lng", lngWriter.FullJsonPropertyName);
408-
Assert.Equal("Location.Lng", lngWriter.FullClrPropertyName);
413+
Assert.Equal("ShippingAddress.Location.Lng", lngWriter.FullClrPropertyName);
409414
Assert.Equal(typeof(double), lngWriter.PropertyType);
410415

411416
// Verify serialization using JsonProperties
412-
var testAddress = new OwnedTypeAddress
417+
var testOrder = new JsonOwnedTypeOrder
413418
{
414-
Street = "123 Main St",
415-
Location = new OwnedTypeLocation
419+
Id = 1,
420+
ShippingAddress = new OwnedTypeAddress
416421
{
417-
Lat = 40.7128,
418-
Lng = -74.0060
422+
Street = "123 Main St",
423+
Location = new OwnedTypeLocation
424+
{
425+
Lat = 40.7128,
426+
Lng = -74.0060
427+
}
419428
}
420429
};
421430

422-
var json = JsonProperty.Serialize(testAddress, jsonProperties);
431+
var json = JsonPropertyWriter.Write(testOrder, flattenedJsonProperties);
423432
Assert.NotNull(json);
424433
Assert.Contains("Street", json);
425434
Assert.Contains("123 Main St", json);
@@ -469,15 +478,15 @@ public void GetProperties_JsonComplexOwnedType_ReturnsCorrectColumnInformation()
469478
Assert.NotNull(streetWriter);
470479
Assert.Equal("Street", streetWriter.JsonPropertyName);
471480
Assert.Equal("Street", streetWriter.FullJsonPropertyName);
472-
Assert.Equal("Street", streetWriter.FullClrPropertyName);
481+
Assert.Equal("ComplexShippingAddress.Street", streetWriter.FullClrPropertyName);
473482
Assert.Equal(typeof(string), streetWriter.PropertyType);
474483
Assert.False(streetWriter.IsNestedComplexType);
475484

476485
var locationWriter = jsonProperties.FirstOrDefault(w => w.ClrPropertyName == "Location");
477486
Assert.NotNull(locationWriter);
478487
Assert.Equal("xxx", locationWriter.JsonPropertyName); // Mapped to "xxx" in TestDbContext
479488
Assert.Equal("xxx", locationWriter.FullJsonPropertyName);
480-
Assert.Equal("Location", locationWriter.FullClrPropertyName);
489+
Assert.Equal("ComplexShippingAddress.Location", locationWriter.FullClrPropertyName);
481490
Assert.Equal(typeof(ComplexTypeLocation), locationWriter.PropertyType);
482491
Assert.True(locationWriter.IsNestedComplexType);
483492
Assert.NotNull(locationWriter.NestedProperties);
@@ -486,27 +495,31 @@ public void GetProperties_JsonComplexOwnedType_ReturnsCorrectColumnInformation()
486495
var latWriter = locationWriter.NestedProperties.FirstOrDefault(w => w.ClrPropertyName == "Lat");
487496
Assert.NotNull(latWriter);
488497
Assert.Equal("xxx.Lat", latWriter.FullJsonPropertyName);
489-
Assert.Equal("Location.Lat", latWriter.FullClrPropertyName);
498+
Assert.Equal("ComplexShippingAddress.Location.Lat", latWriter.FullClrPropertyName);
490499
Assert.Equal(typeof(double), latWriter.PropertyType);
491500

492501
var lngWriter = locationWriter.NestedProperties.FirstOrDefault(w => w.ClrPropertyName == "Lng");
493502
Assert.NotNull(lngWriter);
494503
Assert.Equal("xxx.Lng", lngWriter.FullJsonPropertyName);
495-
Assert.Equal("Location.Lng", lngWriter.FullClrPropertyName);
504+
Assert.Equal("ComplexShippingAddress.Location.Lng", lngWriter.FullClrPropertyName);
496505
Assert.Equal(typeof(double), lngWriter.PropertyType);
497506

498507
// Verify serialization using JsonProperties
499-
var testComplexAddress = new ComplexTypeAddress
508+
var testOrder = new JsonComplexOwnedTypeOrder
500509
{
501-
Street = "123 Main St",
502-
Location = new ComplexTypeLocation
510+
Id = 1,
511+
ComplexShippingAddress = new ComplexTypeAddress
503512
{
504-
Lat = 40.7128,
505-
Lng = -74.0060
513+
Street = "123 Main St",
514+
Location = new ComplexTypeLocation
515+
{
516+
Lat = 40.7128,
517+
Lng = -74.0060
518+
}
506519
}
507520
};
508521

509-
var json = JsonProperty.Serialize(testComplexAddress, jsonProperties);
522+
var json = JsonPropertyWriter.Write(testOrder, flattenedJsonProperties);
510523
Assert.NotNull(json);
511524
Assert.Contains("Street", json);
512525
Assert.Contains("123 Main St", json);
@@ -534,15 +547,15 @@ public void GetProperties_JsonComplexOwnedType_ReturnsCorrectColumnInformation()
534547
Assert.NotNull(streetWriter);
535548
Assert.Equal("Street", streetWriter.JsonPropertyName);
536549
Assert.Equal("Street", streetWriter.FullJsonPropertyName);
537-
Assert.Equal("Street", streetWriter.FullClrPropertyName);
550+
Assert.Equal("OwnedShippingAddress.Street", streetWriter.FullClrPropertyName);
538551
Assert.Equal(typeof(string), streetWriter.PropertyType);
539552
Assert.False(streetWriter.IsNestedComplexType);
540553

541554
locationWriter = jsonProperties.FirstOrDefault(w => w.ClrPropertyName == "Location");
542555
Assert.NotNull(locationWriter);
543556
Assert.Equal("xxx", locationWriter.JsonPropertyName); // Mapped to "xxx" in TestDbContext
544557
Assert.Equal("xxx", locationWriter.FullJsonPropertyName);
545-
Assert.Equal("Location", locationWriter.FullClrPropertyName);
558+
Assert.Equal("OwnedShippingAddress.Location", locationWriter.FullClrPropertyName);
546559
Assert.Equal(typeof(OwnedTypeLocation), locationWriter.PropertyType);
547560
Assert.True(locationWriter.IsNestedComplexType);
548561
Assert.NotNull(locationWriter.NestedProperties);
@@ -551,27 +564,31 @@ public void GetProperties_JsonComplexOwnedType_ReturnsCorrectColumnInformation()
551564
latWriter = locationWriter.NestedProperties.FirstOrDefault(w => w.ClrPropertyName == "Lat");
552565
Assert.NotNull(latWriter);
553566
Assert.Equal("xxx.Lat", latWriter.FullJsonPropertyName);
554-
Assert.Equal("Location.Lat", latWriter.FullClrPropertyName);
567+
Assert.Equal("OwnedShippingAddress.Location.Lat", latWriter.FullClrPropertyName);
555568
Assert.Equal(typeof(double), latWriter.PropertyType);
556569

557570
lngWriter = locationWriter.NestedProperties.FirstOrDefault(w => w.ClrPropertyName == "Lng");
558571
Assert.NotNull(lngWriter);
559572
Assert.Equal("xxx.Lng", lngWriter.FullJsonPropertyName);
560-
Assert.Equal("Location.Lng", lngWriter.FullClrPropertyName);
573+
Assert.Equal("OwnedShippingAddress.Location.Lng", lngWriter.FullClrPropertyName);
561574
Assert.Equal(typeof(double), lngWriter.PropertyType);
562575

563576
// Verify serialization using JsonProperties
564-
var testOwnedAddress = new OwnedTypeAddress
577+
testOrder = new JsonComplexOwnedTypeOrder
565578
{
566-
Street = "123 Main St",
567-
Location = new OwnedTypeLocation
579+
Id = 1,
580+
OwnedShippingAddress = new OwnedTypeAddress
568581
{
569-
Lat = 40.7128,
570-
Lng = -74.0060
582+
Street = "123 Main St",
583+
Location = new OwnedTypeLocation
584+
{
585+
Lat = 40.7128,
586+
Lng = -74.0060
587+
}
571588
}
572589
};
573590

574-
json = JsonProperty.Serialize(testOwnedAddress, jsonProperties);
591+
json = JsonPropertyWriter.Write(testOrder, flattenedJsonProperties);
575592
Assert.NotNull(json);
576593
Assert.Contains("Street", json);
577594
Assert.Contains("123 Main St", json);
Lines changed: 0 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
using Microsoft.EntityFrameworkCore.Metadata;
2-
using Microsoft.EntityFrameworkCore.Storage.Json;
32
using System;
43
using System.Collections.Generic;
5-
using System.IO;
6-
using System.Text;
7-
using System.Text.Json;
84

95
namespace EntityFrameworkCore.SqlServer.SimpleBulks;
106

@@ -34,117 +30,3 @@ public class ColumnInfor
3430

3531
public IReadOnlyDictionary<string, JsonProperty> FlattenedJsonProperties { get; init; }
3632
}
37-
38-
public class JsonProperty
39-
{
40-
public string JsonPropertyName { get; init; }
41-
42-
public string ClrPropertyName { get; init; }
43-
44-
public string FullJsonPropertyName { get; init; }
45-
46-
public string FullClrPropertyName { get; init; }
47-
48-
public bool IsShadowProperty { get; init; }
49-
50-
public bool IsForeignKey { get; init; }
51-
52-
public Type PropertyType { get; init; }
53-
54-
public JsonValueReaderWriter? ReaderWriter { get; init; }
55-
56-
public IReadOnlyList<JsonProperty>? NestedProperties { get; init; }
57-
58-
public bool IsNestedComplexType => NestedProperties != null && NestedProperties.Count > 0;
59-
60-
public void WriteValue(Utf8JsonWriter writer, object? value)
61-
{
62-
if (value == null)
63-
{
64-
writer.WriteNullValue();
65-
return;
66-
}
67-
68-
if (ReaderWriter != null)
69-
{
70-
ReaderWriter.ToJson(writer, value);
71-
}
72-
else
73-
{
74-
// Fallback to System.Text.Json for types without a specific JsonValueReaderWriter
75-
JsonSerializer.Serialize(writer, value, PropertyType);
76-
}
77-
}
78-
79-
public static string Serialize(object obj, IReadOnlyList<JsonProperty> writers)
80-
{
81-
using var stream = new MemoryStream();
82-
using var writer = new Utf8JsonWriter(stream);
83-
84-
WriteObject(writer, obj, writers);
85-
86-
writer.Flush();
87-
return Encoding.UTF8.GetString(stream.ToArray());
88-
}
89-
90-
private static void WriteObject(Utf8JsonWriter writer, object obj, IReadOnlyList<JsonProperty> propertyWriters)
91-
{
92-
writer.WriteStartObject();
93-
94-
var type = obj.GetType();
95-
foreach (var propWriter in propertyWriters)
96-
{
97-
var propInfo = type.GetProperty(propWriter.ClrPropertyName);
98-
if (propInfo == null) continue;
99-
100-
var value = propInfo.GetValue(obj);
101-
102-
writer.WritePropertyName(propWriter.JsonPropertyName);
103-
104-
if (propWriter.IsNestedComplexType)
105-
{
106-
if (value == null)
107-
{
108-
writer.WriteNullValue();
109-
}
110-
else
111-
{
112-
WriteObject(writer, value, propWriter.NestedProperties);
113-
}
114-
}
115-
else
116-
{
117-
propWriter.WriteValue(writer, value);
118-
}
119-
}
120-
121-
writer.WriteEndObject();
122-
}
123-
124-
public static IReadOnlyDictionary<string, JsonProperty> FlattenJsonProperties(IReadOnlyList<JsonProperty>? jsonProperties)
125-
{
126-
if (jsonProperties == null || jsonProperties.Count == 0)
127-
{
128-
return new Dictionary<string, JsonProperty>();
129-
}
130-
131-
var result = new Dictionary<string, JsonProperty>();
132-
FlattenJsonProperties(jsonProperties, result);
133-
return result;
134-
}
135-
136-
private static void FlattenJsonProperties(IReadOnlyList<JsonProperty> jsonProperties, Dictionary<string, JsonProperty> result)
137-
{
138-
foreach (var jsonProperty in jsonProperties)
139-
{
140-
if (jsonProperty.IsNestedComplexType)
141-
{
142-
FlattenJsonProperties(jsonProperty.NestedProperties, result);
143-
}
144-
else
145-
{
146-
result[jsonProperty.FullClrPropertyName] = jsonProperty;
147-
}
148-
}
149-
}
150-
}

0 commit comments

Comments
 (0)