|
63 | 63 | import org.springframework.data.annotation.PersistenceConstructor;
|
64 | 64 | import org.springframework.data.annotation.Transient;
|
65 | 65 | import org.springframework.data.annotation.TypeAlias;
|
| 66 | +import org.springframework.data.convert.ConverterBuilder; |
66 | 67 | import org.springframework.data.convert.CustomConversions;
|
67 | 68 | import org.springframework.data.convert.PropertyValueConverter;
|
68 | 69 | import org.springframework.data.convert.PropertyValueConverterFactory;
|
@@ -2693,9 +2694,85 @@ void shouldWriteNullPropertyCorrectly() {
|
2693 | 2694 | converter.write(fieldWrite, document);
|
2694 | 2695 |
|
2695 | 2696 | assertThat(document).containsEntry("writeAlways", null).doesNotContainKey("writeNonNull");
|
| 2697 | + assertThat(document).containsEntry("writeAlwaysPersonDBRef", null).doesNotContainKey("writeNonNullPersonDBRef"); |
| 2698 | + } |
| 2699 | + |
| 2700 | + @Test // GH-4710 |
| 2701 | + void shouldWriteSimplePropertyCorrectlyAfterConversionReturnsNull() { |
| 2702 | + |
| 2703 | + MongoCustomConversions conversions = new MongoCustomConversions(ConverterBuilder.writing(Integer.class, String.class, it -> null).andReading(it -> null).getConverters().stream().toList()); |
| 2704 | + |
| 2705 | + converter = new MappingMongoConverter(resolver, mappingContext); |
| 2706 | + converter.setCustomConversions(conversions); |
| 2707 | + converter.afterPropertiesSet(); |
| 2708 | + |
| 2709 | + WithFieldWrite fieldWrite = new WithFieldWrite(); |
| 2710 | + fieldWrite.writeAlways = 10; |
| 2711 | + fieldWrite.writeNonNull = 20; |
| 2712 | + |
| 2713 | + org.bson.Document document = new org.bson.Document(); |
| 2714 | + converter.write(fieldWrite, document); |
| 2715 | + |
| 2716 | + assertThat(document).containsEntry("writeAlways", null).doesNotContainKey("writeNonNull"); |
| 2717 | + } |
| 2718 | + |
| 2719 | + @Test // GH-4710 |
| 2720 | + void shouldWriteComplexPropertyCorrectlyAfterConversionReturnsNull() { |
| 2721 | + |
| 2722 | + MongoCustomConversions conversions = new MongoCustomConversions(ConverterBuilder.writing(Person.class, String.class, it -> null).andReading(it -> null).getConverters().stream().toList()); |
| 2723 | + |
| 2724 | + converter = new MappingMongoConverter(resolver, mappingContext); |
| 2725 | + converter.setCustomConversions(conversions); |
| 2726 | + converter.afterPropertiesSet(); |
| 2727 | + |
| 2728 | + WithFieldWrite fieldWrite = new WithFieldWrite(); |
| 2729 | + fieldWrite.writeAlwaysPerson = new Person(); |
| 2730 | + fieldWrite.writeNonNullPerson = new Person(); |
| 2731 | + |
| 2732 | + org.bson.Document document = new org.bson.Document(); |
| 2733 | + converter.write(fieldWrite, document); |
| 2734 | + |
2696 | 2735 | assertThat(document).containsEntry("writeAlwaysPerson", null).doesNotContainKey("writeNonNullPerson");
|
2697 | 2736 | }
|
2698 | 2737 |
|
| 2738 | + @Test // GH-4710 |
| 2739 | + void shouldDelegateWriteOfDBRefToCustomConversionIfConfigured() { |
| 2740 | + |
| 2741 | + MongoCustomConversions conversions = new MongoCustomConversions(ConverterBuilder.writing(Person.class, DBRef.class, it -> new DBRef("persons", "n/a")).andReading(it -> null).getConverters().stream().toList()); |
| 2742 | + |
| 2743 | + converter = new MappingMongoConverter(resolver, mappingContext); |
| 2744 | + converter.setCustomConversions(conversions); |
| 2745 | + converter.afterPropertiesSet(); |
| 2746 | + |
| 2747 | + WithFieldWrite fieldWrite = new WithFieldWrite(); |
| 2748 | + fieldWrite.writeAlwaysPersonDBRef = new Person(); |
| 2749 | + fieldWrite.writeNonNullPersonDBRef = new Person(); |
| 2750 | + |
| 2751 | + org.bson.Document document = new org.bson.Document(); |
| 2752 | + converter.write(fieldWrite, document); |
| 2753 | + |
| 2754 | + assertThat(document).containsEntry("writeAlwaysPersonDBRef", new DBRef("persons", "n/a"));//.doesNotContainKey("writeNonNullPersonDBRef"); |
| 2755 | + } |
| 2756 | + |
| 2757 | + @Test // GH-4710 |
| 2758 | + void shouldDelegateWriteOfDBRefToCustomConversionIfConfiguredAndCheckNulls() { |
| 2759 | + |
| 2760 | + MongoCustomConversions conversions = new MongoCustomConversions(ConverterBuilder.writing(Person.class, DBRef.class, it -> null).andReading(it -> null).getConverters().stream().toList()); |
| 2761 | + |
| 2762 | + converter = new MappingMongoConverter(resolver, mappingContext); |
| 2763 | + converter.setCustomConversions(conversions); |
| 2764 | + converter.afterPropertiesSet(); |
| 2765 | + |
| 2766 | + WithFieldWrite fieldWrite = new WithFieldWrite(); |
| 2767 | + fieldWrite.writeAlwaysPersonDBRef = new Person(); |
| 2768 | + fieldWrite.writeNonNullPersonDBRef = new Person(); |
| 2769 | + |
| 2770 | + org.bson.Document document = new org.bson.Document(); |
| 2771 | + converter.write(fieldWrite, document); |
| 2772 | + |
| 2773 | + assertThat(document).containsEntry("writeAlwaysPersonDBRef", null).doesNotContainKey("writeNonNullPersonDBRef"); |
| 2774 | + } |
| 2775 | + |
2699 | 2776 | @Test // GH-3686
|
2700 | 2777 | void readsCollectionContainingNullValue() {
|
2701 | 2778 |
|
@@ -4102,13 +4179,19 @@ static class WithFieldWrite {
|
4102 | 4179 | @org.springframework.data.mongodb.core.mapping.Field(
|
4103 | 4180 | write = org.springframework.data.mongodb.core.mapping.Field.Write.ALWAYS) Integer writeAlways;
|
4104 | 4181 |
|
| 4182 | + @org.springframework.data.mongodb.core.mapping.Field( |
| 4183 | + write = org.springframework.data.mongodb.core.mapping.Field.Write.NON_NULL) Person writeNonNullPerson; |
| 4184 | + |
| 4185 | + @org.springframework.data.mongodb.core.mapping.Field( |
| 4186 | + write = org.springframework.data.mongodb.core.mapping.Field.Write.ALWAYS) Person writeAlwaysPerson; |
| 4187 | + |
4105 | 4188 | @org.springframework.data.mongodb.core.mapping.DBRef
|
4106 | 4189 | @org.springframework.data.mongodb.core.mapping.Field(
|
4107 |
| - write = org.springframework.data.mongodb.core.mapping.Field.Write.NON_NULL) Person writeNonNullPerson; |
| 4190 | + write = org.springframework.data.mongodb.core.mapping.Field.Write.NON_NULL) Person writeNonNullPersonDBRef; |
4108 | 4191 |
|
4109 | 4192 | @org.springframework.data.mongodb.core.mapping.DBRef
|
4110 | 4193 | @org.springframework.data.mongodb.core.mapping.Field(
|
4111 |
| - write = org.springframework.data.mongodb.core.mapping.Field.Write.ALWAYS) Person writeAlwaysPerson; |
| 4194 | + write = org.springframework.data.mongodb.core.mapping.Field.Write.ALWAYS) Person writeAlwaysPersonDBRef; |
4112 | 4195 |
|
4113 | 4196 | }
|
4114 | 4197 |
|
|
0 commit comments