Skip to content

Commit c217618

Browse files
committed
Polishing.
Reorder methods and types. Rename MongoPersistentProperty.isOmitNullProperty to writeNullValues. Adapt caching MongoPersistentProperty and add tests. Tweak Javadoc wording, add author and since tags. See #3407 Original pull request: #3646.
1 parent b1020d1 commit c217618

File tree

11 files changed

+242
-102
lines changed

11 files changed

+242
-102
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

+13-7
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
* @author Mark Paluch
104104
* @author Roman Puchkovskiy
105105
* @author Heesu Jung
106+
* @author Divya Srivastava
106107
*/
107108
public class MappingMongoConverter extends AbstractMongoConverter implements ApplicationContextAware {
108109

@@ -737,20 +738,18 @@ private void writeProperties(Bson bson, MongoPersistentEntity<?> entity, Persist
737738
continue;
738739
}
739740
if (prop.isAssociation()) {
741+
740742
writeAssociation(prop.getRequiredAssociation(), accessor, dbObjectAccessor);
741743
continue;
742744
}
743745

744746
Object value = accessor.getProperty(prop);
745747

746748
if (value == null) {
747-
if(!prop.isPropertyOmittableOnNull()) {
748-
writeSimpleInternal(value, bson , prop);
749+
if (prop.writeNullValues()) {
750+
dbObjectAccessor.put(prop, null);
749751
}
750-
continue;
751-
}
752-
753-
if (!conversions.isSimpleType(value.getClass())) {
752+
} else if (!conversions.isSimpleType(value.getClass())) {
754753
writePropertyInternal(value, dbObjectAccessor, prop);
755754
} else {
756755
writeSimpleInternal(value, bson, prop);
@@ -763,7 +762,14 @@ private void writeAssociation(Association<MongoPersistentProperty> association,
763762

764763
MongoPersistentProperty inverseProp = association.getInverse();
765764

766-
writePropertyInternal(accessor.getProperty(inverseProp), dbObjectAccessor, inverseProp);
765+
Object value = accessor.getProperty(inverseProp);
766+
767+
if (value == null && !inverseProp.isUnwrapped() && inverseProp.writeNullValues()) {
768+
dbObjectAccessor.put(inverseProp, null);
769+
return;
770+
}
771+
772+
writePropertyInternal(value, dbObjectAccessor, inverseProp);
767773
}
768774

769775
@SuppressWarnings({ "unchecked" })

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentProperty.java

+14-13
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* @author Thomas Darimont
4242
* @author Christoph Strobl
4343
* @author Mark Paluch
44+
* @author Divya Srivastava
4445
*/
4546
public class BasicMongoPersistentProperty extends AnnotationBasedPersistentProperty<MongoPersistentProperty>
4647
implements MongoPersistentProperty {
@@ -214,6 +215,19 @@ public int getFieldOrder() {
214215
return annotation != null ? annotation.order() : Integer.MAX_VALUE;
215216
}
216217

218+
/*
219+
* (non-Javadoc)
220+
* @see org.springframework.data.mongodb.core.mapping.MongoPersistentProperty#skipNullValues()
221+
*/
222+
@Override
223+
public boolean writeNullValues() {
224+
225+
org.springframework.data.mongodb.core.mapping.Field annotation = findAnnotation(
226+
org.springframework.data.mongodb.core.mapping.Field.class);
227+
228+
return annotation != null && annotation.write() == Field.Write.ALWAYS;
229+
}
230+
217231
/*
218232
* (non-Javadoc)
219233
* @see org.springframework.data.mapping.model.AbstractPersistentProperty#createAssociation()
@@ -286,17 +300,4 @@ public boolean isTextScoreProperty() {
286300
return isAnnotationPresent(TextScore.class);
287301
}
288302

289-
/*
290-
* (non-Javadoc)
291-
* @see org.springframework.data.mongodb.core.mapping.MongoPersistentProperty#isPropertyOmittableOnNull()
292-
*/
293-
public boolean isPropertyOmittableOnNull() {
294-
org.springframework.data.mongodb.core.mapping.Field annotation = findAnnotation(
295-
org.springframework.data.mongodb.core.mapping.Field.class);
296-
297-
if ( annotation != null && annotation.write().equals(Field.Write.ALWAYS) ) {
298-
return false;
299-
}
300-
return true;
301-
}
302303
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/CachingMongoPersistentProperty.java

+15
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class CachingMongoPersistentProperty extends BasicMongoPersistentProperty
3333
private boolean dbRefResolved;
3434
private @Nullable DBRef dbref;
3535
private @Nullable String fieldName;
36+
private @Nullable Boolean writeNullValues;
3637
private @Nullable Class<?> fieldType;
3738
private @Nullable Boolean usePropertyAccess;
3839
private @Nullable Boolean isTransient;
@@ -90,6 +91,20 @@ public String getFieldName() {
9091
return this.fieldName;
9192
}
9293

94+
/*
95+
* (non-Javadoc)
96+
* @see org.springframework.data.mongodb.core.mapping.BasicMongoPersistentProperty#skipNullValues()
97+
*/
98+
@Override
99+
public boolean writeNullValues() {
100+
101+
if (this.writeNullValues == null) {
102+
this.writeNullValues = super.writeNullValues();
103+
}
104+
105+
return this.writeNullValues;
106+
}
107+
93108
/*
94109
* (non-Javadoc)
95110
* @see org.springframework.data.mongodb.core.mapping.BasicMongoPersistentProperty#getFieldType()

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Field.java

+25-19
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,13 @@
2828
*
2929
* @author Oliver Gierke
3030
* @author Christoph Strobl
31+
* @author Divya Srivastava
3132
*/
3233
@Documented
3334
@Retention(RetentionPolicy.RUNTIME)
3435
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
3536
public @interface Field {
3637

37-
/**
38-
* Enumeration of write strategies for a field with null value.It decides whether a field with null value has to be
39-
* written to the resulting document to be saved to the database.
40-
*/
41-
enum Write{
42-
/*
43-
* The field will always be written to the database irrespective of null value.
44-
*/
45-
ALWAYS,
46-
/*
47-
* The field will only be written to the database if it has a non null value.
48-
*/
49-
NON_NULL
50-
}
51-
5238
/**
5339
* The key to be used to store the field inside the document. Alias for {@link #name()}.
5440
*
@@ -82,12 +68,32 @@ enum Write{
8268
FieldType targetType() default FieldType.IMPLICIT;
8369

8470
/**
85-
* If set to {@link Write#NON_NULL} {@literal null} values will be omitted.
86-
* Setting the value to {@link Write#ALWAYS} explicitly adds an entry for the given field
87-
* holding {@literal null} as a value {@code 'fieldName' : null }.
71+
* Write rules when to include a property value upon conversion. If set to {@link Write#NON_NULL} (default)
72+
* {@literal null} values are not written to the target {@code Document}. Setting the value to {@link Write#ALWAYS}
73+
* explicitly adds an entry for the given field holding {@literal null} as a value {@code 'fieldName' : null }.
8874
* <p />
89-
* <strong>NOTE</strong> Setting the value to {@link Write#ALWAYS} may lead to increased document size.
75+
* <strong>NOTE</strong>Setting the value to {@link Write#ALWAYS} may lead to increased document size.
76+
*
9077
* @return {@link Write#NON_NULL} by default.
78+
* @since 3.3
9179
*/
9280
Write write() default Write.NON_NULL;
81+
82+
/**
83+
* Enumeration of write strategies to define when a property is included for write conversion.
84+
*
85+
* @since 3.3
86+
*/
87+
enum Write {
88+
89+
/**
90+
* Value that indicates that property is to be always included, independent of value of the property.
91+
*/
92+
ALWAYS,
93+
94+
/**
95+
* Value that indicates that only properties with non-{@literal null} values are to be included.
96+
*/
97+
NON_NULL
98+
}
9399
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoPersistentProperty.java

+10-18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* @author Patryk Wasik
2929
* @author Thomas Darimont
3030
* @author Christoph Strobl
31+
* @author Divya Srivastava
3132
*/
3233
public interface MongoPersistentProperty extends PersistentProperty<MongoPersistentProperty> {
3334

@@ -54,6 +55,15 @@ public interface MongoPersistentProperty extends PersistentProperty<MongoPersist
5455
*/
5556
int getFieldOrder();
5657

58+
/**
59+
* Returns whether the property should be written to the database if its value is {@literal null}.
60+
*
61+
* @return
62+
* @since 3.3
63+
* @see Field.Write
64+
*/
65+
boolean writeNullValues();
66+
5767
/**
5868
* Returns whether the property is a {@link com.mongodb.DBRef}. If this returns {@literal true} you can expect
5969
* {@link #getDBRef()} to return an non-{@literal null} value.
@@ -104,24 +114,6 @@ public interface MongoPersistentProperty extends PersistentProperty<MongoPersist
104114
* @since 1.6
105115
*/
106116
boolean isTextScoreProperty();
107-
108-
/**
109-
* Returns whether the property is to be written to the document if the value is null <br/>
110-
* It's annotated with {@link Field.Write}.
111-
*
112-
* @return
113-
* @since 1.6
114-
*/
115-
boolean isPropertyOmittableOnNull();
116-
117-
/**
118-
* Returns whether the property is to be written to the document if the value is null <br/>
119-
* It's annotated with {@link omitNull}.
120-
*
121-
* @return
122-
* @since 1.6
123-
*/
124-
boolean isOmitNullProperty();
125117

126118
/**
127119
* Returns the {@link DBRef} if the property is a reference.

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/UnwrappedMongoPersistentProperty.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public int getFieldOrder() {
6363
return delegate.getFieldOrder();
6464
}
6565

66+
@Override
67+
public boolean writeNullValues() {
68+
return delegate.writeNullValues();
69+
}
70+
6671
@Override
6772
public boolean isDbReference() {
6873
return delegate.isDbReference();
@@ -92,11 +97,6 @@ public boolean isExplicitLanguageProperty() {
9297
public boolean isTextScoreProperty() {
9398
return delegate.isTextScoreProperty();
9499
}
95-
96-
@Override
97-
public boolean isOmitNullProperty() {
98-
return delegate.isOmitNullProperty();
99-
}
100100

101101
@Override
102102
@Nullable
@@ -321,8 +321,4 @@ public <T> PersistentPropertyAccessor<T> getAccessorForOwner(T owner) {
321321
return delegate.getAccessorForOwner(owner);
322322
}
323323

324-
@Override
325-
public boolean isPropertyOmittableOnNull() {
326-
return delegate.isPropertyOmittableOnNull();
327-
}
328324
}

0 commit comments

Comments
 (0)