51
51
import org .mockito .Mock ;
52
52
import org .mockito .Mockito ;
53
53
import org .mockito .junit .jupiter .MockitoExtension ;
54
-
55
54
import org .springframework .aop .framework .ProxyFactory ;
56
55
import org .springframework .beans .ConversionNotSupportedException ;
57
56
import org .springframework .beans .factory .annotation .Autowired ;
108
107
import org .springframework .lang .NonNull ;
109
108
import org .springframework .lang .Nullable ;
110
109
import org .springframework .test .util .ReflectionTestUtils ;
110
+ import org .springframework .util .ObjectUtils ;
111
111
112
112
import com .mongodb .BasicDBList ;
113
113
import com .mongodb .BasicDBObject ;
@@ -3410,11 +3410,38 @@ void usesStringNumericFormat() {
3410
3410
assertThat (document ).containsEntry ("map.foo" , "2.5" );
3411
3411
}
3412
3412
3413
+ @ Test // GH-5036
3414
+ void withCustomBigIntegerConversion () {
3415
+
3416
+ MappingMongoConverter converter = createConverter (MongoCustomConversions .BigDecimalRepresentation .STRING ,
3417
+ new CustomBigIntegerToStringConverter ());
3418
+
3419
+ WithBigNumericValues container = new WithBigNumericValues ();
3420
+ container .bigInteger = BigInteger .TEN ;
3421
+
3422
+ org .bson .Document document = new org .bson .Document ();
3423
+ converter .write (container , document );
3424
+
3425
+ assertThat (document ).containsEntry ("bigInteger" , "BigInteger('10')" );
3426
+ }
3427
+
3413
3428
private MappingMongoConverter createConverter (
3414
3429
MongoCustomConversions .BigDecimalRepresentation bigDecimalRepresentation ) {
3415
3430
3416
- MongoCustomConversions conversions = MongoCustomConversions .create (
3417
- it -> it .registerConverter (new ByteBufferToDoubleHolderConverter ()).bigDecimal (bigDecimalRepresentation ));
3431
+ return createConverter (bigDecimalRepresentation , new ByteBufferToDoubleHolderConverter ());
3432
+ }
3433
+
3434
+ private MappingMongoConverter createConverter (
3435
+ MongoCustomConversions .BigDecimalRepresentation bigDecimalRepresentation , Converter <?, ?>... customConverters ) {
3436
+
3437
+ MongoCustomConversions conversions = MongoCustomConversions .create (it -> {
3438
+ if (!ObjectUtils .isEmpty (customConverters )) {
3439
+ for (Converter <?, ?> customConverter : customConverters ) {
3440
+ it .registerConverter (customConverter );
3441
+ }
3442
+ }
3443
+ it .bigDecimal (bigDecimalRepresentation );
3444
+ });
3418
3445
3419
3446
MongoMappingContext mappingContext = new MongoMappingContext ();
3420
3447
mappingContext .setApplicationContext (context );
@@ -3437,6 +3464,14 @@ org.bson.Document write(Object source) {
3437
3464
return target ;
3438
3465
}
3439
3466
3467
+ @ WritingConverter
3468
+ static class CustomBigIntegerToStringConverter implements Converter <BigInteger , String > {
3469
+ @ Override
3470
+ public String convert (BigInteger source ) {
3471
+ return "BigInteger('%s')" .formatted (source .toString ());
3472
+ }
3473
+ }
3474
+
3440
3475
static class WithVector {
3441
3476
3442
3477
Vector embeddings ;
@@ -4084,8 +4119,7 @@ static class WithExplicitTargetTypes {
4084
4119
@ Field (targetType = FieldType .DECIMAL128 ) //
4085
4120
BigDecimal bigDecimal ;
4086
4121
4087
- @ Field (targetType = FieldType .DECIMAL128 )
4088
- BigInteger bigInteger ;
4122
+ @ Field (targetType = FieldType .DECIMAL128 ) BigInteger bigInteger ;
4089
4123
4090
4124
@ Field (targetType = FieldType .INT64 ) //
4091
4125
Date dateAsLong ;
@@ -4100,6 +4134,10 @@ static class WithExplicitTargetTypes {
4100
4134
Date dateAsObjectId ;
4101
4135
}
4102
4136
4137
+ static class WithBigNumericValues {
4138
+ BigInteger bigInteger ;
4139
+ }
4140
+
4103
4141
static class WrapperAroundWithUnwrapped {
4104
4142
4105
4143
String someValue ;
0 commit comments