5151import org .mockito .Mock ;
5252import org .mockito .Mockito ;
5353import org .mockito .junit .jupiter .MockitoExtension ;
54-
5554import org .springframework .aop .framework .ProxyFactory ;
5655import org .springframework .beans .ConversionNotSupportedException ;
5756import org .springframework .beans .factory .annotation .Autowired ;
108107import org .springframework .lang .NonNull ;
109108import org .springframework .lang .Nullable ;
110109import org .springframework .test .util .ReflectionTestUtils ;
110+ import org .springframework .util .ObjectUtils ;
111111
112112import com .mongodb .BasicDBList ;
113113import com .mongodb .BasicDBObject ;
@@ -3410,11 +3410,38 @@ void usesStringNumericFormat() {
34103410 assertThat (document ).containsEntry ("map.foo" , "2.5" );
34113411 }
34123412
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+
34133428 private MappingMongoConverter createConverter (
34143429 MongoCustomConversions .BigDecimalRepresentation bigDecimalRepresentation ) {
34153430
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+ });
34183445
34193446 MongoMappingContext mappingContext = new MongoMappingContext ();
34203447 mappingContext .setApplicationContext (context );
@@ -3437,6 +3464,14 @@ org.bson.Document write(Object source) {
34373464 return target ;
34383465 }
34393466
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+
34403475 static class WithVector {
34413476
34423477 Vector embeddings ;
@@ -4084,8 +4119,7 @@ static class WithExplicitTargetTypes {
40844119 @ Field (targetType = FieldType .DECIMAL128 ) //
40854120 BigDecimal bigDecimal ;
40864121
4087- @ Field (targetType = FieldType .DECIMAL128 )
4088- BigInteger bigInteger ;
4122+ @ Field (targetType = FieldType .DECIMAL128 ) BigInteger bigInteger ;
40894123
40904124 @ Field (targetType = FieldType .INT64 ) //
40914125 Date dateAsLong ;
@@ -4100,6 +4134,10 @@ static class WithExplicitTargetTypes {
41004134 Date dateAsObjectId ;
41014135 }
41024136
4137+ static class WithBigNumericValues {
4138+ BigInteger bigInteger ;
4139+ }
4140+
41034141 static class WrapperAroundWithUnwrapped {
41044142
41054143 String someValue ;
0 commit comments