@@ -472,4 +472,156 @@ public void GetProperties_JsonOwnedType_ReturnsCorrectColumnInformation()
472472 Assert . Contains ( "Lat" , json ) ;
473473 Assert . Contains ( "Lng" , json ) ;
474474 }
475+
476+ [ Fact ]
477+ public void GetProperties_JsonComplexOwnedType_ReturnsCorrectColumnInformation ( )
478+ {
479+ // Arrange
480+ var dbContext = new TestDbContext ( "" , "" ) ;
481+
482+ // Act
483+ var properties = dbContext . GetProperties ( typeof ( JsonComplexOwnedTypeOrder ) ) ;
484+
485+ // Assert
486+ Assert . Equal ( 3 , properties . Count ) ;
487+
488+ var property = properties . First ( p => p . PropertyName == "Id" ) ;
489+ Assert . Equal ( typeof ( int ) , property . PropertyType ) ;
490+ Assert . Equal ( "Id" , property . ColumnName ) ;
491+ Assert . Equal ( "int" , property . ColumnType ) ;
492+ Assert . Equal ( ValueGenerated . OnAdd , property . ValueGenerated ) ;
493+ Assert . Null ( property . DefaultValueSql ) ;
494+ Assert . True ( property . IsPrimaryKey ) ;
495+ Assert . False ( property . IsRowVersion ) ;
496+
497+ property = properties . First ( p => p . PropertyName == "ComplexShippingAddress" ) ;
498+ Assert . Equal ( typeof ( ComplexTypeAddress ) , property . PropertyType ) ;
499+ Assert . Equal ( "ComplexShippingAddress" , property . ColumnName ) ;
500+ Assert . Equal ( "nvarchar(max)" , property . ColumnType ) ;
501+ Assert . Equal ( ValueGenerated . Never , property . ValueGenerated ) ;
502+ Assert . Null ( property . DefaultValueSql ) ;
503+ Assert . False ( property . IsPrimaryKey ) ;
504+ Assert . False ( property . IsRowVersion ) ;
505+ Assert . True ( property . IsJson ) ;
506+ Assert . NotNull ( property . JsonPropertyWriters ) ;
507+
508+ // Verify that the JsonPropertyWriters have correct structure
509+ var writers = property . JsonPropertyWriters ;
510+ var flattenedWriters = property . GetFlattenedJsonPropertyWriters ( ) ;
511+ Assert . Equal ( 2 , writers . Count ) ; // Street and Location
512+
513+ var streetWriter = writers . FirstOrDefault ( w => w . ClrPropertyName == "Street" ) ;
514+ Assert . NotNull ( streetWriter ) ;
515+ Assert . Equal ( "Street" , streetWriter . JsonPropertyName ) ;
516+ Assert . Equal ( "Street" , streetWriter . FullJsonPath ) ;
517+ Assert . Equal ( "Street" , streetWriter . FullClrPropertyName ) ;
518+ Assert . Equal ( typeof ( string ) , streetWriter . PropertyType ) ;
519+ Assert . False ( streetWriter . IsNestedComplexType ) ;
520+
521+ var locationWriter = writers . FirstOrDefault ( w => w . ClrPropertyName == "Location" ) ;
522+ Assert . NotNull ( locationWriter ) ;
523+ Assert . Equal ( "xxx" , locationWriter . JsonPropertyName ) ; // Mapped to "xxx" in TestDbContext
524+ Assert . Equal ( "xxx" , locationWriter . FullJsonPath ) ;
525+ Assert . Equal ( "Location" , locationWriter . FullClrPropertyName ) ;
526+ Assert . Equal ( typeof ( ComplexTypeLocation ) , locationWriter . PropertyType ) ;
527+ Assert . True ( locationWriter . IsNestedComplexType ) ;
528+ Assert . NotNull ( locationWriter . NestedProperties ) ;
529+ Assert . Equal ( 2 , locationWriter . NestedProperties . Count ) ; // Lat and Lng
530+
531+ var latWriter = locationWriter . NestedProperties . FirstOrDefault ( w => w . ClrPropertyName == "Lat" ) ;
532+ Assert . NotNull ( latWriter ) ;
533+ Assert . Equal ( "xxx.Lat" , latWriter . FullJsonPath ) ;
534+ Assert . Equal ( "Location.Lat" , latWriter . FullClrPropertyName ) ;
535+ Assert . Equal ( typeof ( double ) , latWriter . PropertyType ) ;
536+
537+ var lngWriter = locationWriter . NestedProperties . FirstOrDefault ( w => w . ClrPropertyName == "Lng" ) ;
538+ Assert . NotNull ( lngWriter ) ;
539+ Assert . Equal ( "xxx.Lng" , lngWriter . FullJsonPath ) ;
540+ Assert . Equal ( "Location.Lng" , lngWriter . FullClrPropertyName ) ;
541+ Assert . Equal ( typeof ( double ) , lngWriter . PropertyType ) ;
542+
543+ // Verify serialization using JsonPropertyWriters
544+ var testComplexAddress = new ComplexTypeAddress
545+ {
546+ Street = "123 Main St" ,
547+ Location = new ComplexTypeLocation
548+ {
549+ Lat = 40.7128 ,
550+ Lng = - 74.0060
551+ }
552+ } ;
553+
554+ var json = SerializeWithJsonPropertyWriters ( testComplexAddress , writers ) ;
555+ Assert . NotNull ( json ) ;
556+ Assert . Contains ( "Street" , json ) ;
557+ Assert . Contains ( "123 Main St" , json ) ;
558+ Assert . Contains ( "xxx" , json ) ; // Location is mapped to "xxx"
559+ Assert . Contains ( "Lat" , json ) ;
560+ Assert . Contains ( "Lng" , json ) ;
561+
562+ property = properties . First ( p => p . PropertyName == "OwnedShippingAddress" ) ;
563+ Assert . Equal ( typeof ( OwnedTypeAddress ) , property . PropertyType ) ;
564+ Assert . Equal ( "OwnedShippingAddress" , property . ColumnName ) ;
565+ Assert . Equal ( "nvarchar(max)" , property . ColumnType ) ;
566+ Assert . Equal ( ValueGenerated . Never , property . ValueGenerated ) ;
567+ Assert . Null ( property . DefaultValueSql ) ;
568+ Assert . False ( property . IsPrimaryKey ) ;
569+ Assert . False ( property . IsRowVersion ) ;
570+ Assert . True ( property . IsJson ) ;
571+ Assert . NotNull ( property . JsonPropertyWriters ) ;
572+
573+ // Verify that the JsonPropertyWriters have correct structure
574+ writers = property . JsonPropertyWriters ;
575+ flattenedWriters = property . GetFlattenedJsonPropertyWriters ( ) ;
576+ Assert . Equal ( 3 , writers . Count ) ; // Street and Location
577+
578+ streetWriter = writers . FirstOrDefault ( w => w . ClrPropertyName == "Street" ) ;
579+ Assert . NotNull ( streetWriter ) ;
580+ Assert . Equal ( "Street" , streetWriter . JsonPropertyName ) ;
581+ Assert . Equal ( "Street" , streetWriter . FullJsonPath ) ;
582+ Assert . Equal ( "Street" , streetWriter . FullClrPropertyName ) ;
583+ Assert . Equal ( typeof ( string ) , streetWriter . PropertyType ) ;
584+ Assert . False ( streetWriter . IsNestedComplexType ) ;
585+
586+ locationWriter = writers . FirstOrDefault ( w => w . ClrPropertyName == "Location" ) ;
587+ Assert . NotNull ( locationWriter ) ;
588+ Assert . Equal ( "xxx" , locationWriter . JsonPropertyName ) ; // Mapped to "xxx" in TestDbContext
589+ Assert . Equal ( "xxx" , locationWriter . FullJsonPath ) ;
590+ Assert . Equal ( "Location" , locationWriter . FullClrPropertyName ) ;
591+ Assert . Equal ( typeof ( OwnedTypeLocation ) , locationWriter . PropertyType ) ;
592+ Assert . True ( locationWriter . IsNestedComplexType ) ;
593+ Assert . NotNull ( locationWriter . NestedProperties ) ;
594+ Assert . Equal ( 3 , locationWriter . NestedProperties . Count ) ; // Lat and Lng
595+
596+ latWriter = locationWriter . NestedProperties . FirstOrDefault ( w => w . ClrPropertyName == "Lat" ) ;
597+ Assert . NotNull ( latWriter ) ;
598+ Assert . Equal ( "xxx.Lat" , latWriter . FullJsonPath ) ;
599+ Assert . Equal ( "Location.Lat" , latWriter . FullClrPropertyName ) ;
600+ Assert . Equal ( typeof ( double ) , latWriter . PropertyType ) ;
601+
602+ lngWriter = locationWriter . NestedProperties . FirstOrDefault ( w => w . ClrPropertyName == "Lng" ) ;
603+ Assert . NotNull ( lngWriter ) ;
604+ Assert . Equal ( "xxx.Lng" , lngWriter . FullJsonPath ) ;
605+ Assert . Equal ( "Location.Lng" , lngWriter . FullClrPropertyName ) ;
606+ Assert . Equal ( typeof ( double ) , lngWriter . PropertyType ) ;
607+
608+ // Verify serialization using JsonPropertyWriters
609+ var testOwnedAddress = new OwnedTypeAddress
610+ {
611+ Street = "123 Main St" ,
612+ Location = new OwnedTypeLocation
613+ {
614+ Lat = 40.7128 ,
615+ Lng = - 74.0060
616+ }
617+ } ;
618+
619+ json = SerializeWithJsonPropertyWriters ( testOwnedAddress , writers ) ;
620+ Assert . NotNull ( json ) ;
621+ Assert . Contains ( "Street" , json ) ;
622+ Assert . Contains ( "123 Main St" , json ) ;
623+ Assert . Contains ( "xxx" , json ) ; // Location is mapped to "xxx"
624+ Assert . Contains ( "Lat" , json ) ;
625+ Assert . Contains ( "Lng" , json ) ;
626+ }
475627}
0 commit comments