|
1 | 1 | using EntityFrameworkCore.SqlServer.SimpleBulks.Extensions; |
2 | 2 | using EntityFrameworkCore.SqlServer.SimpleBulks.Tests.Database; |
3 | 3 | using Microsoft.EntityFrameworkCore.Metadata; |
| 4 | +using System.Text; |
| 5 | +using System.Text.Json; |
4 | 6 |
|
5 | 7 | namespace EntityFrameworkCore.SqlServer.SimpleBulks.Tests.DbContextExtensions; |
6 | 8 |
|
@@ -251,4 +253,136 @@ public void GetProperties_ComplexOwnedType_ReturnsCorrectColumnInformation() |
251 | 253 | Assert.False(property.IsPrimaryKey); |
252 | 254 | Assert.False(property.IsRowVersion); |
253 | 255 | } |
| 256 | + |
| 257 | + [Fact] |
| 258 | + public void GetProperties_JsonComplexType_ReturnsCorrectColumnInformation() |
| 259 | + { |
| 260 | + // Arrange |
| 261 | + var dbContext = new TestDbContext("", ""); |
| 262 | + |
| 263 | + // Act |
| 264 | + var properties = dbContext.GetProperties(typeof(JsonComplexTypeOrder)); |
| 265 | + |
| 266 | + // Assert |
| 267 | + Assert.Equal(2, properties.Count); |
| 268 | + |
| 269 | + var property = properties.First(p => p.PropertyName == "Id"); |
| 270 | + Assert.Equal(typeof(int), property.PropertyType); |
| 271 | + Assert.Equal("Id", property.ColumnName); |
| 272 | + Assert.Equal("int", property.ColumnType); |
| 273 | + Assert.Equal(ValueGenerated.OnAdd, property.ValueGenerated); |
| 274 | + Assert.Null(property.DefaultValueSql); |
| 275 | + Assert.True(property.IsPrimaryKey); |
| 276 | + Assert.False(property.IsRowVersion); |
| 277 | + |
| 278 | + property = properties.First(p => p.PropertyName == "ShippingAddress"); |
| 279 | + Assert.Equal(typeof(ComplexTypeAddress), property.PropertyType); |
| 280 | + Assert.Equal("ShippingAddress", property.ColumnName); |
| 281 | + Assert.Equal("nvarchar(max)", property.ColumnType); |
| 282 | + Assert.Equal(ValueGenerated.Never, property.ValueGenerated); |
| 283 | + Assert.Null(property.DefaultValueSql); |
| 284 | + Assert.False(property.IsPrimaryKey); |
| 285 | + Assert.False(property.IsRowVersion); |
| 286 | + Assert.True(property.IsJson); |
| 287 | + Assert.NotNull(property.JsonPropertyWriters); |
| 288 | + |
| 289 | + // Verify that the JsonPropertyWriters have correct structure |
| 290 | + var writers = property.JsonPropertyWriters; |
| 291 | + var flattenedWriters = property.GetFlattenedJsonPropertyWriters(); |
| 292 | + Assert.Equal(2, writers.Count); // Street and Location |
| 293 | + |
| 294 | + var streetWriter = writers.FirstOrDefault(w => w.ClrPropertyName == "Street"); |
| 295 | + Assert.NotNull(streetWriter); |
| 296 | + Assert.Equal("Street", streetWriter.JsonPropertyName); |
| 297 | + Assert.Equal("Street", streetWriter.FullJsonPath); |
| 298 | + Assert.Equal("Street", streetWriter.FullClrPropertyName); |
| 299 | + Assert.Equal(typeof(string), streetWriter.PropertyType); |
| 300 | + Assert.False(streetWriter.IsNestedComplexType); |
| 301 | + |
| 302 | + var locationWriter = writers.FirstOrDefault(w => w.ClrPropertyName == "Location"); |
| 303 | + Assert.NotNull(locationWriter); |
| 304 | + Assert.Equal("xxx", locationWriter.JsonPropertyName); // Mapped to "xxx" in TestDbContext |
| 305 | + Assert.Equal("xxx", locationWriter.FullJsonPath); |
| 306 | + Assert.Equal("Location", locationWriter.FullClrPropertyName); |
| 307 | + Assert.Equal(typeof(ComplexTypeLocation), locationWriter.PropertyType); |
| 308 | + Assert.True(locationWriter.IsNestedComplexType); |
| 309 | + Assert.NotNull(locationWriter.NestedProperties); |
| 310 | + Assert.Equal(2, locationWriter.NestedProperties.Count); // Lat and Lng |
| 311 | + |
| 312 | + var latWriter = locationWriter.NestedProperties.FirstOrDefault(w => w.ClrPropertyName == "Lat"); |
| 313 | + Assert.NotNull(latWriter); |
| 314 | + Assert.Equal("xxx.Lat", latWriter.FullJsonPath); |
| 315 | + Assert.Equal("Location.Lat", latWriter.FullClrPropertyName); |
| 316 | + Assert.Equal(typeof(double), latWriter.PropertyType); |
| 317 | + |
| 318 | + var lngWriter = locationWriter.NestedProperties.FirstOrDefault(w => w.ClrPropertyName == "Lng"); |
| 319 | + Assert.NotNull(lngWriter); |
| 320 | + Assert.Equal("xxx.Lng", lngWriter.FullJsonPath); |
| 321 | + Assert.Equal("Location.Lng", lngWriter.FullClrPropertyName); |
| 322 | + Assert.Equal(typeof(double), lngWriter.PropertyType); |
| 323 | + |
| 324 | + // Verify serialization using JsonPropertyWriters |
| 325 | + var testAddress = new ComplexTypeAddress |
| 326 | + { |
| 327 | + Street = "123 Main St", |
| 328 | + Location = new ComplexTypeLocation |
| 329 | + { |
| 330 | + Lat = 40.7128, |
| 331 | + Lng = -74.0060 |
| 332 | + } |
| 333 | + }; |
| 334 | + |
| 335 | + var json = SerializeWithJsonPropertyWriters(testAddress, writers); |
| 336 | + Assert.NotNull(json); |
| 337 | + Assert.Contains("Street", json); |
| 338 | + Assert.Contains("123 Main St", json); |
| 339 | + Assert.Contains("xxx", json); // Location is mapped to "xxx" |
| 340 | + Assert.Contains("Lat", json); |
| 341 | + Assert.Contains("Lng", json); |
| 342 | + } |
| 343 | + |
| 344 | + private static string SerializeWithJsonPropertyWriters(object obj, IReadOnlyList<JsonPropertyWriter> writers) |
| 345 | + { |
| 346 | + using var stream = new MemoryStream(); |
| 347 | + using var writer = new Utf8JsonWriter(stream); |
| 348 | + |
| 349 | + WriteObject(writer, obj, writers); |
| 350 | + |
| 351 | + writer.Flush(); |
| 352 | + return Encoding.UTF8.GetString(stream.ToArray()); |
| 353 | + } |
| 354 | + |
| 355 | + private static void WriteObject(Utf8JsonWriter writer, object obj, IReadOnlyList<JsonPropertyWriter> propertyWriters) |
| 356 | + { |
| 357 | + writer.WriteStartObject(); |
| 358 | + |
| 359 | + var type = obj.GetType(); |
| 360 | + foreach (var propWriter in propertyWriters) |
| 361 | + { |
| 362 | + var propInfo = type.GetProperty(propWriter.ClrPropertyName); |
| 363 | + if (propInfo == null) continue; |
| 364 | + |
| 365 | + var value = propInfo.GetValue(obj); |
| 366 | + |
| 367 | + writer.WritePropertyName(propWriter.JsonPropertyName); |
| 368 | + |
| 369 | + if (propWriter.IsNestedComplexType) |
| 370 | + { |
| 371 | + if (value == null) |
| 372 | + { |
| 373 | + writer.WriteNullValue(); |
| 374 | + } |
| 375 | + else |
| 376 | + { |
| 377 | + WriteObject(writer, value, propWriter.NestedProperties); |
| 378 | + } |
| 379 | + } |
| 380 | + else |
| 381 | + { |
| 382 | + propWriter.WriteValue(writer, value); |
| 383 | + } |
| 384 | + } |
| 385 | + |
| 386 | + writer.WriteEndObject(); |
| 387 | + } |
254 | 388 | } |
0 commit comments