|
53 | 53 | import org.mockito.Mock; |
54 | 54 | import org.mockito.Mockito; |
55 | 55 | import org.mockito.junit.jupiter.MockitoExtension; |
56 | | - |
57 | 56 | import org.springframework.aop.framework.ProxyFactory; |
58 | 57 | import org.springframework.beans.ConversionNotSupportedException; |
59 | 58 | import org.springframework.beans.factory.annotation.Autowired; |
|
109 | 108 | import org.springframework.data.projection.EntityProjectionIntrospector; |
110 | 109 | import org.springframework.data.util.TypeInformation; |
111 | 110 | import org.springframework.test.util.ReflectionTestUtils; |
| 111 | +import org.springframework.util.ObjectUtils; |
112 | 112 |
|
113 | 113 | import com.mongodb.BasicDBList; |
114 | 114 | import com.mongodb.BasicDBObject; |
@@ -3577,16 +3577,39 @@ void usesStringNumericFormat() { |
3577 | 3577 | assertThat(document).containsEntry("map.foo", "2.5"); |
3578 | 3578 | } |
3579 | 3579 |
|
| 3580 | + @Test // GH-5036 |
| 3581 | + void withCustomBigIntegerConversion() { |
| 3582 | + |
| 3583 | + MappingMongoConverter converter = createConverter(MongoCustomConversions.BigDecimalRepresentation.STRING, |
| 3584 | + new CustomBigIntegerToStringConverter()); |
| 3585 | + |
| 3586 | + WithoutExplicitTargetTypes container = new WithoutExplicitTargetTypes(); |
| 3587 | + container.bigInteger = BigInteger.TEN; |
| 3588 | + |
| 3589 | + org.bson.Document document = new org.bson.Document(); |
| 3590 | + converter.write(container, document); |
| 3591 | + |
| 3592 | + assertThat(document).containsEntry("bigInteger", "BigInteger('10')"); |
| 3593 | + } |
| 3594 | + |
3580 | 3595 | private MappingMongoConverter createConverter() { |
3581 | | - return createConverter(null); |
| 3596 | + return createConverter(null, new ByteBufferToDoubleHolderConverter()); |
3582 | 3597 | } |
3583 | 3598 |
|
3584 | 3599 | private MappingMongoConverter createConverter( |
3585 | | - MongoCustomConversions.BigDecimalRepresentation bigDecimalRepresentation) { |
| 3600 | + MongoCustomConversions.BigDecimalRepresentation bigDecimalRepresentation) { |
| 3601 | + return createConverter(bigDecimalRepresentation, new ByteBufferToDoubleHolderConverter()); |
| 3602 | + } |
| 3603 | + |
| 3604 | + private MappingMongoConverter createConverter( |
| 3605 | + MongoCustomConversions.BigDecimalRepresentation bigDecimalRepresentation, Converter<?,?>... customConverters) { |
3586 | 3606 |
|
3587 | 3607 | MongoCustomConversions conversions = MongoCustomConversions.create( |
3588 | 3608 | it -> { |
3589 | | - it.registerConverter(new ByteBufferToDoubleHolderConverter()); |
| 3609 | + |
| 3610 | + if(!ObjectUtils.isEmpty(customConverters)) { |
| 3611 | + Arrays.stream(customConverters).forEach(it::registerConverter); |
| 3612 | + } |
3590 | 3613 |
|
3591 | 3614 | if (bigDecimalRepresentation != null) { |
3592 | 3615 | it.bigDecimal(bigDecimalRepresentation); |
@@ -3614,6 +3637,14 @@ org.bson.Document write(Object source) { |
3614 | 3637 | return target; |
3615 | 3638 | } |
3616 | 3639 |
|
| 3640 | + @WritingConverter |
| 3641 | + static class CustomBigIntegerToStringConverter implements Converter<BigInteger, String> { |
| 3642 | + @Override |
| 3643 | + public String convert(BigInteger source) { |
| 3644 | + return "BigInteger('%s')".formatted(source.toString()); |
| 3645 | + } |
| 3646 | + } |
| 3647 | + |
3617 | 3648 | static class WithVector { |
3618 | 3649 |
|
3619 | 3650 | Vector embeddings; |
|
0 commit comments