@@ -385,4 +385,91 @@ private static void WriteObject(Utf8JsonWriter writer, object obj, IReadOnlyList
385385
386386 writer . WriteEndObject ( ) ;
387387 }
388+
389+ [ Fact ]
390+ public void GetProperties_JsonOwnedType_ReturnsCorrectColumnInformation ( )
391+ {
392+ // Arrange
393+ var dbContext = new TestDbContext ( "" , "" ) ;
394+
395+ // Act
396+ var properties = dbContext . GetProperties ( typeof ( JsonOwnedTypeOrder ) ) ;
397+
398+ // Assert
399+ Assert . Equal ( 2 , properties . Count ) ;
400+
401+ var property = properties . First ( p => p . PropertyName == "Id" ) ;
402+ Assert . Equal ( typeof ( int ) , property . PropertyType ) ;
403+ Assert . Equal ( "Id" , property . ColumnName ) ;
404+ Assert . Equal ( "int" , property . ColumnType ) ;
405+ Assert . Equal ( ValueGenerated . OnAdd , property . ValueGenerated ) ;
406+ Assert . Null ( property . DefaultValueSql ) ;
407+ Assert . True ( property . IsPrimaryKey ) ;
408+ Assert . False ( property . IsRowVersion ) ;
409+
410+ property = properties . First ( p => p . PropertyName == "ShippingAddress" ) ;
411+ Assert . Equal ( typeof ( OwnedTypeAddress ) , property . PropertyType ) ;
412+ Assert . Equal ( "ShippingAddress" , property . ColumnName ) ;
413+ Assert . Equal ( "nvarchar(max)" , property . ColumnType ) ;
414+ Assert . Equal ( ValueGenerated . Never , property . ValueGenerated ) ;
415+ Assert . Null ( property . DefaultValueSql ) ;
416+ Assert . False ( property . IsPrimaryKey ) ;
417+ Assert . False ( property . IsRowVersion ) ;
418+ Assert . True ( property . IsJson ) ;
419+ Assert . NotNull ( property . JsonPropertyWriters ) ;
420+
421+ // Verify that the JsonPropertyWriters have correct structure
422+ var writers = property . JsonPropertyWriters ;
423+ var flattenedWriters = property . GetFlattenedJsonPropertyWriters ( ) ;
424+ Assert . Equal ( 3 , writers . Count ) ; // Street and Location
425+
426+ var streetWriter = writers . FirstOrDefault ( w => w . ClrPropertyName == "Street" ) ;
427+ Assert . NotNull ( streetWriter ) ;
428+ Assert . Equal ( "Street" , streetWriter . JsonPropertyName ) ;
429+ Assert . Equal ( "Street" , streetWriter . FullJsonPath ) ;
430+ Assert . Equal ( "Street" , streetWriter . FullClrPropertyName ) ;
431+ Assert . Equal ( typeof ( string ) , streetWriter . PropertyType ) ;
432+ Assert . False ( streetWriter . IsNestedComplexType ) ;
433+
434+ var locationWriter = writers . FirstOrDefault ( w => w . ClrPropertyName == "Location" ) ;
435+ Assert . NotNull ( locationWriter ) ;
436+ Assert . Equal ( "xxx" , locationWriter . JsonPropertyName ) ; // Mapped to "xxx" in TestDbContext
437+ Assert . Equal ( "xxx" , locationWriter . FullJsonPath ) ;
438+ Assert . Equal ( "Location" , locationWriter . FullClrPropertyName ) ;
439+ Assert . Equal ( typeof ( OwnedTypeLocation ) , locationWriter . PropertyType ) ;
440+ Assert . True ( locationWriter . IsNestedComplexType ) ;
441+ Assert . NotNull ( locationWriter . NestedProperties ) ;
442+ Assert . Equal ( 3 , locationWriter . NestedProperties . Count ) ; // Lat and Lng
443+
444+ var latWriter = locationWriter . NestedProperties . FirstOrDefault ( w => w . ClrPropertyName == "Lat" ) ;
445+ Assert . NotNull ( latWriter ) ;
446+ Assert . Equal ( "xxx.Lat" , latWriter . FullJsonPath ) ;
447+ Assert . Equal ( "Location.Lat" , latWriter . FullClrPropertyName ) ;
448+ Assert . Equal ( typeof ( double ) , latWriter . PropertyType ) ;
449+
450+ var lngWriter = locationWriter . NestedProperties . FirstOrDefault ( w => w . ClrPropertyName == "Lng" ) ;
451+ Assert . NotNull ( lngWriter ) ;
452+ Assert . Equal ( "xxx.Lng" , lngWriter . FullJsonPath ) ;
453+ Assert . Equal ( "Location.Lng" , lngWriter . FullClrPropertyName ) ;
454+ Assert . Equal ( typeof ( double ) , lngWriter . PropertyType ) ;
455+
456+ // Verify serialization using JsonPropertyWriters
457+ var testAddress = new OwnedTypeAddress
458+ {
459+ Street = "123 Main St" ,
460+ Location = new OwnedTypeLocation
461+ {
462+ Lat = 40.7128 ,
463+ Lng = - 74.0060
464+ }
465+ } ;
466+
467+ var json = SerializeWithJsonPropertyWriters ( testAddress , writers ) ;
468+ Assert . NotNull ( json ) ;
469+ Assert . Contains ( "Street" , json ) ;
470+ Assert . Contains ( "123 Main St" , json ) ;
471+ Assert . Contains ( "xxx" , json ) ; // Location is mapped to "xxx"
472+ Assert . Contains ( "Lat" , json ) ;
473+ Assert . Contains ( "Lng" , json ) ;
474+ }
388475}
0 commit comments