28
28
import java .math .BigDecimal ;
29
29
import java .math .BigInteger ;
30
30
import java .net .URL ;
31
+ import java .nio .ByteBuffer ;
31
32
import java .time .LocalDate ;
32
33
import java .time .LocalDateTime ;
33
34
import java .time .temporal .ChronoUnit ;
34
35
import java .util .*;
35
36
37
+ import org .assertj .core .data .Percentage ;
38
+ import org .bson .BsonUndefined ;
36
39
import org .bson .types .Binary ;
37
40
import org .bson .types .Code ;
38
41
import org .bson .types .Decimal128 ;
@@ -125,7 +128,8 @@ class MappingMongoConverterUnitTests {
125
128
@ BeforeEach
126
129
void beforeEach () {
127
130
128
- MongoCustomConversions conversions = new MongoCustomConversions ();
131
+ MongoCustomConversions conversions = new MongoCustomConversions (
132
+ Arrays .asList (new ByteBufferToDoubleHolderConverter ()));
129
133
130
134
mappingContext = new MongoMappingContext ();
131
135
mappingContext .setApplicationContext (context );
@@ -1437,7 +1441,7 @@ void shouldWriteEntityWithGeoCircleCorrectly() {
1437
1441
assertThat (document .get ("circle" )).isInstanceOf (org .bson .Document .class );
1438
1442
assertThat (document .get ("circle" )).isEqualTo ((Object ) new org .bson .Document ("center" ,
1439
1443
new org .bson .Document ("x" , circle .getCenter ().getX ()).append ("y" , circle .getCenter ().getY ()))
1440
- .append ("radius" , radius .getNormalizedValue ()).append ("metric" , radius .getMetric ().toString ()));
1444
+ .append ("radius" , radius .getNormalizedValue ()).append ("metric" , radius .getMetric ().toString ()));
1441
1445
}
1442
1446
1443
1447
@ Test // DATAMONGO-858
@@ -1470,7 +1474,7 @@ void shouldWriteEntityWithGeoSphereCorrectly() {
1470
1474
assertThat (document .get ("sphere" )).isInstanceOf (org .bson .Document .class );
1471
1475
assertThat (document .get ("sphere" )).isEqualTo ((Object ) new org .bson .Document ("center" ,
1472
1476
new org .bson .Document ("x" , sphere .getCenter ().getX ()).append ("y" , sphere .getCenter ().getY ()))
1473
- .append ("radius" , radius .getNormalizedValue ()).append ("metric" , radius .getMetric ().toString ()));
1477
+ .append ("radius" , radius .getNormalizedValue ()).append ("metric" , radius .getMetric ().toString ()));
1474
1478
}
1475
1479
1476
1480
@ Test // DATAMONGO-858
@@ -1488,7 +1492,7 @@ void shouldWriteEntityWithGeoSphereWithMetricDistanceCorrectly() {
1488
1492
assertThat (document .get ("sphere" )).isInstanceOf (org .bson .Document .class );
1489
1493
assertThat (document .get ("sphere" )).isEqualTo ((Object ) new org .bson .Document ("center" ,
1490
1494
new org .bson .Document ("x" , sphere .getCenter ().getX ()).append ("y" , sphere .getCenter ().getY ()))
1491
- .append ("radius" , radius .getNormalizedValue ()).append ("metric" , radius .getMetric ().toString ()));
1495
+ .append ("radius" , radius .getNormalizedValue ()).append ("metric" , radius .getMetric ().toString ()));
1492
1496
}
1493
1497
1494
1498
@ Test // DATAMONGO-858
@@ -1521,7 +1525,7 @@ void shouldWriteEntityWithGeoShapeCorrectly() {
1521
1525
assertThat (document .get ("shape" )).isInstanceOf (org .bson .Document .class );
1522
1526
assertThat (document .get ("shape" )).isEqualTo ((Object ) new org .bson .Document ("center" ,
1523
1527
new org .bson .Document ("x" , sphere .getCenter ().getX ()).append ("y" , sphere .getCenter ().getY ()))
1524
- .append ("radius" , radius .getNormalizedValue ()).append ("metric" , radius .getMetric ().toString ()));
1528
+ .append ("radius" , radius .getNormalizedValue ()).append ("metric" , radius .getMetric ().toString ()));
1525
1529
}
1526
1530
1527
1531
@ Test // DATAMONGO-858
@@ -2531,8 +2535,8 @@ void usesCustomConverterForPropertiesUsingTypesImplementingMapOnRead() {
2531
2535
converter .afterPropertiesSet ();
2532
2536
2533
2537
org .bson .Document source = new org .bson .Document ("typeImplementingMap" ,
2534
- new org .bson .Document ("1st" , "one" ).append ("2nd" , 2 )). append ( "_class" ,
2535
- TypeWrappingTypeImplementingMap .class .getName ());
2538
+ new org .bson .Document ("1st" , "one" ).append ("2nd" , 2 ))
2539
+ . append ( "_class" , TypeWrappingTypeImplementingMap .class .getName ());
2536
2540
2537
2541
TypeWrappingTypeImplementingMap target = converter .read (TypeWrappingTypeImplementingMap .class , source );
2538
2542
@@ -2720,8 +2724,8 @@ void projectShouldReadNestedInterfaceProjection() {
2720
2724
.and ((target , underlyingType ) -> !converter .conversions .isSimpleType (target )),
2721
2725
mappingContext );
2722
2726
2723
- EntityProjection <WithNestedInterfaceProjection , Person > projection = introspector . introspect ( WithNestedInterfaceProjection . class ,
2724
- Person .class );
2727
+ EntityProjection <WithNestedInterfaceProjection , Person > projection = introspector
2728
+ . introspect ( WithNestedInterfaceProjection . class , Person .class );
2725
2729
WithNestedInterfaceProjection person = converter .project (projection , source );
2726
2730
2727
2731
assertThat (person .getFirstname ()).isEqualTo ("spring" );
@@ -2739,14 +2743,35 @@ void projectShouldReadNestedDtoProjection() {
2739
2743
.and ((target , underlyingType ) -> !converter .conversions .isSimpleType (target )),
2740
2744
mappingContext );
2741
2745
2742
- EntityProjection <WithNestedDtoProjection , Person > projection = introspector . introspect ( WithNestedDtoProjection . class ,
2743
- Person .class );
2746
+ EntityProjection <WithNestedDtoProjection , Person > projection = introspector
2747
+ . introspect ( WithNestedDtoProjection . class , Person .class );
2744
2748
WithNestedDtoProjection person = converter .project (projection , source );
2745
2749
2746
2750
assertThat (person .getFirstname ()).isEqualTo ("spring" );
2747
2751
assertThat (person .getAddress ().getStreet ()).isEqualTo ("data" );
2748
2752
}
2749
2753
2754
+ @ Test // GH-4626
2755
+ void projectShouldReadDtoProjectionPropertiesOnlyOnce () {
2756
+
2757
+ ByteBuffer number = ByteBuffer .allocate (8 );
2758
+ number .putDouble (1.2d );
2759
+ number .flip ();
2760
+
2761
+ org .bson .Document source = new org .bson .Document ("number" , number );
2762
+
2763
+ EntityProjectionIntrospector introspector = EntityProjectionIntrospector .create (converter .getProjectionFactory (),
2764
+ EntityProjectionIntrospector .ProjectionPredicate .typeHierarchy ()
2765
+ .and ((target , underlyingType ) -> !converter .conversions .isSimpleType (target )),
2766
+ mappingContext );
2767
+
2768
+ EntityProjection <DoubleHolderDto , WithDoubleHolder > projection = introspector .introspect (DoubleHolderDto .class ,
2769
+ WithDoubleHolder .class );
2770
+ DoubleHolderDto result = converter .project (projection , source );
2771
+
2772
+ assertThat (result .number .number ).isCloseTo (1.2 , Percentage .withPercentage (1 ));
2773
+ }
2774
+
2750
2775
@ Test // GH-2860
2751
2776
void projectShouldReadProjectionWithNestedEntity () {
2752
2777
@@ -3022,11 +3047,13 @@ interface WithNestedProjection {
3022
3047
3023
3048
interface WithNestedInterfaceProjection {
3024
3049
String getFirstname ();
3050
+
3025
3051
AddressProjection getAddress ();
3026
3052
}
3027
3053
3028
3054
interface WithNestedDtoProjection {
3029
3055
String getFirstname ();
3056
+
3030
3057
AddressDto getAddress ();
3031
3058
}
3032
3059
@@ -3925,4 +3952,30 @@ static class ComplexIdAndNoAnnotation {
3925
3952
ComplexId id ;
3926
3953
String value ;
3927
3954
}
3955
+
3956
+ @ ReadingConverter
3957
+ static class ByteBufferToDoubleHolderConverter implements Converter <ByteBuffer , DoubleHolder > {
3958
+
3959
+ @ Override
3960
+ public DoubleHolder convert (ByteBuffer source ) {
3961
+ return new DoubleHolder (source .getDouble ());
3962
+ }
3963
+ }
3964
+
3965
+ record DoubleHolder (double number ) {
3966
+
3967
+ }
3968
+
3969
+ static class WithDoubleHolder {
3970
+ DoubleHolder number ;
3971
+ }
3972
+
3973
+ static class DoubleHolderDto {
3974
+ DoubleHolder number ;
3975
+
3976
+ public DoubleHolderDto (DoubleHolder number ) {
3977
+ this .number = number ;
3978
+ }
3979
+ }
3980
+
3928
3981
}
0 commit comments