113
113
import com .mongodb .BasicDBObject ;
114
114
import com .mongodb .DBObject ;
115
115
import com .mongodb .DBRef ;
116
+ import org .springframework .util .ObjectUtils ;
116
117
117
118
/**
118
119
* Unit tests for {@link MappingMongoConverter}.
@@ -3410,11 +3411,38 @@ void usesStringNumericFormat() {
3410
3411
assertThat (document ).containsEntry ("map.foo" , "2.5" );
3411
3412
}
3412
3413
3414
+ @ Test // GH-5036
3415
+ void withCustomBigIntegerConversion () {
3416
+
3417
+ MappingMongoConverter converter = createConverter (MongoCustomConversions .BigDecimalRepresentation .STRING ,
3418
+ new CustomBigIntegerToStringConverter ());
3419
+
3420
+ WithBigNumericValues container = new WithBigNumericValues ();
3421
+ container .bigInteger = BigInteger .TEN ;
3422
+
3423
+ org .bson .Document document = new org .bson .Document ();
3424
+ converter .write (container , document );
3425
+
3426
+ assertThat (document ).containsEntry ("bigInteger" , "BigInteger('10')" );
3427
+ }
3428
+
3413
3429
private MappingMongoConverter createConverter (
3414
3430
MongoCustomConversions .BigDecimalRepresentation bigDecimalRepresentation ) {
3415
3431
3416
- MongoCustomConversions conversions = MongoCustomConversions .create (
3417
- it -> it .registerConverter (new ByteBufferToDoubleHolderConverter ()).bigDecimal (bigDecimalRepresentation ));
3432
+ return createConverter (bigDecimalRepresentation , new ByteBufferToDoubleHolderConverter ());
3433
+ }
3434
+
3435
+ private MappingMongoConverter createConverter (
3436
+ MongoCustomConversions .BigDecimalRepresentation bigDecimalRepresentation , Converter <?, ?>... customConverters ) {
3437
+
3438
+ MongoCustomConversions conversions = MongoCustomConversions .create (it -> {
3439
+ if (!ObjectUtils .isEmpty (customConverters )) {
3440
+ for (Converter <?, ?> customConverter : customConverters ) {
3441
+ it .registerConverter (customConverter );
3442
+ }
3443
+ }
3444
+ it .bigDecimal (bigDecimalRepresentation );
3445
+ });
3418
3446
3419
3447
MongoMappingContext mappingContext = new MongoMappingContext ();
3420
3448
mappingContext .setApplicationContext (context );
@@ -3437,6 +3465,14 @@ org.bson.Document write(Object source) {
3437
3465
return target ;
3438
3466
}
3439
3467
3468
+ @ WritingConverter
3469
+ static class CustomBigIntegerToStringConverter implements Converter <BigInteger , String > {
3470
+ @ Override
3471
+ public String convert (BigInteger source ) {
3472
+ return "BigInteger('%s')" .formatted (source .toString ());
3473
+ }
3474
+ }
3475
+
3440
3476
static class WithVector {
3441
3477
3442
3478
Vector embeddings ;
@@ -4084,8 +4120,7 @@ static class WithExplicitTargetTypes {
4084
4120
@ Field (targetType = FieldType .DECIMAL128 ) //
4085
4121
BigDecimal bigDecimal ;
4086
4122
4087
- @ Field (targetType = FieldType .DECIMAL128 )
4088
- BigInteger bigInteger ;
4123
+ @ Field (targetType = FieldType .DECIMAL128 ) BigInteger bigInteger ;
4089
4124
4090
4125
@ Field (targetType = FieldType .INT64 ) //
4091
4126
Date dateAsLong ;
@@ -4100,6 +4135,10 @@ static class WithExplicitTargetTypes {
4100
4135
Date dateAsObjectId ;
4101
4136
}
4102
4137
4138
+ static class WithBigNumericValues {
4139
+ BigInteger bigInteger ;
4140
+ }
4141
+
4103
4142
static class WrapperAroundWithUnwrapped {
4104
4143
4105
4144
String someValue ;
0 commit comments