23
23
import java .math .BigDecimal ;
24
24
import java .math .BigInteger ;
25
25
import java .net .URL ;
26
+ import java .nio .ByteBuffer ;
26
27
import java .time .LocalDate ;
27
28
import java .time .LocalDateTime ;
28
29
import java .time .temporal .ChronoUnit ;
31
32
import java .util .function .Function ;
32
33
import java .util .stream .Stream ;
33
34
35
+ import org .assertj .core .data .Percentage ;
34
36
import org .bson .BsonUndefined ;
35
37
import org .bson .types .Binary ;
36
38
import org .bson .types .Code ;
@@ -129,7 +131,8 @@ class MappingMongoConverterUnitTests {
129
131
@ BeforeEach
130
132
void beforeEach () {
131
133
132
- MongoCustomConversions conversions = new MongoCustomConversions ();
134
+ MongoCustomConversions conversions = new MongoCustomConversions (
135
+ Arrays .asList (new ByteBufferToDoubleHolderConverter ()));
133
136
134
137
mappingContext = new MongoMappingContext ();
135
138
mappingContext .setApplicationContext (context );
@@ -1579,7 +1582,7 @@ void shouldWriteEntityWithGeoCircleCorrectly() {
1579
1582
assertThat (document .get ("circle" )).isInstanceOf (org .bson .Document .class );
1580
1583
assertThat (document .get ("circle" )).isEqualTo ((Object ) new org .bson .Document ("center" ,
1581
1584
new org .bson .Document ("x" , circle .getCenter ().getX ()).append ("y" , circle .getCenter ().getY ()))
1582
- .append ("radius" , radius .getNormalizedValue ()).append ("metric" , radius .getMetric ().toString ()));
1585
+ .append ("radius" , radius .getNormalizedValue ()).append ("metric" , radius .getMetric ().toString ()));
1583
1586
}
1584
1587
1585
1588
@ Test // DATAMONGO-858
@@ -1612,7 +1615,7 @@ void shouldWriteEntityWithGeoSphereCorrectly() {
1612
1615
assertThat (document .get ("sphere" )).isInstanceOf (org .bson .Document .class );
1613
1616
assertThat (document .get ("sphere" )).isEqualTo ((Object ) new org .bson .Document ("center" ,
1614
1617
new org .bson .Document ("x" , sphere .getCenter ().getX ()).append ("y" , sphere .getCenter ().getY ()))
1615
- .append ("radius" , radius .getNormalizedValue ()).append ("metric" , radius .getMetric ().toString ()));
1618
+ .append ("radius" , radius .getNormalizedValue ()).append ("metric" , radius .getMetric ().toString ()));
1616
1619
}
1617
1620
1618
1621
@ Test // DATAMONGO-858
@@ -1630,7 +1633,7 @@ void shouldWriteEntityWithGeoSphereWithMetricDistanceCorrectly() {
1630
1633
assertThat (document .get ("sphere" )).isInstanceOf (org .bson .Document .class );
1631
1634
assertThat (document .get ("sphere" )).isEqualTo ((Object ) new org .bson .Document ("center" ,
1632
1635
new org .bson .Document ("x" , sphere .getCenter ().getX ()).append ("y" , sphere .getCenter ().getY ()))
1633
- .append ("radius" , radius .getNormalizedValue ()).append ("metric" , radius .getMetric ().toString ()));
1636
+ .append ("radius" , radius .getNormalizedValue ()).append ("metric" , radius .getMetric ().toString ()));
1634
1637
}
1635
1638
1636
1639
@ Test // DATAMONGO-858
@@ -1663,7 +1666,7 @@ void shouldWriteEntityWithGeoShapeCorrectly() {
1663
1666
assertThat (document .get ("shape" )).isInstanceOf (org .bson .Document .class );
1664
1667
assertThat (document .get ("shape" )).isEqualTo ((Object ) new org .bson .Document ("center" ,
1665
1668
new org .bson .Document ("x" , sphere .getCenter ().getX ()).append ("y" , sphere .getCenter ().getY ()))
1666
- .append ("radius" , radius .getNormalizedValue ()).append ("metric" , radius .getMetric ().toString ()));
1669
+ .append ("radius" , radius .getNormalizedValue ()).append ("metric" , radius .getMetric ().toString ()));
1667
1670
}
1668
1671
1669
1672
@ Test // DATAMONGO-858
@@ -2673,8 +2676,8 @@ void usesCustomConverterForPropertiesUsingTypesImplementingMapOnRead() {
2673
2676
converter .afterPropertiesSet ();
2674
2677
2675
2678
org .bson .Document source = new org .bson .Document ("typeImplementingMap" ,
2676
- new org .bson .Document ("1st" , "one" ).append ("2nd" , 2 )). append ( "_class" ,
2677
- TypeWrappingTypeImplementingMap .class .getName ());
2679
+ new org .bson .Document ("1st" , "one" ).append ("2nd" , 2 ))
2680
+ . append ( "_class" , TypeWrappingTypeImplementingMap .class .getName ());
2678
2681
2679
2682
TypeWrappingTypeImplementingMap target = converter .read (TypeWrappingTypeImplementingMap .class , source );
2680
2683
@@ -2862,8 +2865,8 @@ void projectShouldReadNestedInterfaceProjection() {
2862
2865
.and ((target , underlyingType ) -> !converter .conversions .isSimpleType (target )),
2863
2866
mappingContext );
2864
2867
2865
- EntityProjection <WithNestedInterfaceProjection , Person > projection = introspector . introspect ( WithNestedInterfaceProjection . class ,
2866
- Person .class );
2868
+ EntityProjection <WithNestedInterfaceProjection , Person > projection = introspector
2869
+ . introspect ( WithNestedInterfaceProjection . class , Person .class );
2867
2870
WithNestedInterfaceProjection person = converter .project (projection , source );
2868
2871
2869
2872
assertThat (person .getFirstname ()).isEqualTo ("spring" );
@@ -2881,14 +2884,35 @@ void projectShouldReadNestedDtoProjection() {
2881
2884
.and ((target , underlyingType ) -> !converter .conversions .isSimpleType (target )),
2882
2885
mappingContext );
2883
2886
2884
- EntityProjection <WithNestedDtoProjection , Person > projection = introspector . introspect ( WithNestedDtoProjection . class ,
2885
- Person .class );
2887
+ EntityProjection <WithNestedDtoProjection , Person > projection = introspector
2888
+ . introspect ( WithNestedDtoProjection . class , Person .class );
2886
2889
WithNestedDtoProjection person = converter .project (projection , source );
2887
2890
2888
2891
assertThat (person .getFirstname ()).isEqualTo ("spring" );
2889
2892
assertThat (person .getAddress ().getStreet ()).isEqualTo ("data" );
2890
2893
}
2891
2894
2895
+ @ Test // GH-4626
2896
+ void projectShouldReadDtoProjectionPropertiesOnlyOnce () {
2897
+
2898
+ ByteBuffer number = ByteBuffer .allocate (8 );
2899
+ number .putDouble (1.2d );
2900
+ number .flip ();
2901
+
2902
+ org .bson .Document source = new org .bson .Document ("number" , number );
2903
+
2904
+ EntityProjectionIntrospector introspector = EntityProjectionIntrospector .create (converter .getProjectionFactory (),
2905
+ EntityProjectionIntrospector .ProjectionPredicate .typeHierarchy ()
2906
+ .and ((target , underlyingType ) -> !converter .conversions .isSimpleType (target )),
2907
+ mappingContext );
2908
+
2909
+ EntityProjection <DoubleHolderDto , WithDoubleHolder > projection = introspector .introspect (DoubleHolderDto .class ,
2910
+ WithDoubleHolder .class );
2911
+ DoubleHolderDto result = converter .project (projection , source );
2912
+
2913
+ assertThat (result .number .number ).isCloseTo (1.2 , Percentage .withPercentage (1 ));
2914
+ }
2915
+
2892
2916
@ Test // GH-2860
2893
2917
void projectShouldReadProjectionWithNestedEntity () {
2894
2918
@@ -3289,11 +3313,13 @@ interface WithNestedProjection {
3289
3313
3290
3314
interface WithNestedInterfaceProjection {
3291
3315
String getFirstname ();
3316
+
3292
3317
AddressProjection getAddress ();
3293
3318
}
3294
3319
3295
3320
interface WithNestedDtoProjection {
3296
3321
String getFirstname ();
3322
+
3297
3323
AddressDto getAddress ();
3298
3324
}
3299
3325
@@ -4342,4 +4368,30 @@ static class ComplexIdAndNoAnnotation {
4342
4368
ComplexId id ;
4343
4369
String value ;
4344
4370
}
4371
+
4372
+ @ ReadingConverter
4373
+ static class ByteBufferToDoubleHolderConverter implements Converter <ByteBuffer , DoubleHolder > {
4374
+
4375
+ @ Override
4376
+ public DoubleHolder convert (ByteBuffer source ) {
4377
+ return new DoubleHolder (source .getDouble ());
4378
+ }
4379
+ }
4380
+
4381
+ record DoubleHolder (double number ) {
4382
+
4383
+ }
4384
+
4385
+ static class WithDoubleHolder {
4386
+ DoubleHolder number ;
4387
+ }
4388
+
4389
+ static class DoubleHolderDto {
4390
+ DoubleHolder number ;
4391
+
4392
+ public DoubleHolderDto (DoubleHolder number ) {
4393
+ this .number = number ;
4394
+ }
4395
+ }
4396
+
4345
4397
}
0 commit comments