DATAREST-95 - Add support for PATCH to partially update entities.
Added general support for HTTP PATCH to partially update entities and amend property resources.
This commit is contained in:
committed by
Oliver Gierke
parent
c662065ef2
commit
d65179ccc8
@@ -61,7 +61,7 @@ public class DomainObjectMerger {
|
||||
* @param from can be {@literal null}.
|
||||
* @param target can be {@literal null}.
|
||||
*/
|
||||
public void merge(Object from, Object target) {
|
||||
public void merge(Object from, Object target, final MergeNullPolicy nullPolicy) {
|
||||
|
||||
if (null == from || null == target) {
|
||||
return;
|
||||
@@ -88,7 +88,9 @@ public class DomainObjectMerger {
|
||||
}
|
||||
|
||||
if (!ObjectUtils.nullSafeEquals(sourceValue, targetValue)) {
|
||||
targetWrapper.setProperty(persistentProperty, sourceValue);
|
||||
if (nullPolicy == MergeNullPolicy.APPLY_NULLS || sourceValue != null) {
|
||||
targetWrapper.setProperty(persistentProperty, sourceValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -110,4 +112,15 @@ public class DomainObjectMerger {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* A switch on whether or not to ignore nulls.
|
||||
* NOTE: This could have been a simple boolean flag but the enumerated value clearly
|
||||
* denotes which version is being used.
|
||||
*/
|
||||
public static enum MergeNullPolicy {
|
||||
APPLY_NULLS, IGNORE_NULLS;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ public class DomainObjectMergerTests {
|
||||
Person existingDomainObject = new Person("Frodo", "Baggins");
|
||||
|
||||
DomainObjectMerger merger = new DomainObjectMerger(repositories, conversionService);
|
||||
merger.merge(incoming, existingDomainObject);
|
||||
merger.merge(incoming, existingDomainObject, DomainObjectMerger.MergeNullPolicy.APPLY_NULLS);
|
||||
|
||||
assertThat(existingDomainObject.getFirstName(), equalTo(incoming.getFirstName()));
|
||||
assertThat(existingDomainObject.getLastName(), equalTo(incoming.getLastName()));
|
||||
@@ -76,7 +76,7 @@ public class DomainObjectMergerTests {
|
||||
Person existingDomainObject = new Person("Frodo", "Baggins");
|
||||
|
||||
DomainObjectMerger merger = new DomainObjectMerger(repositories, conversionService);
|
||||
merger.merge(incoming, existingDomainObject);
|
||||
merger.merge(incoming, existingDomainObject, DomainObjectMerger.MergeNullPolicy.APPLY_NULLS);
|
||||
|
||||
assertThat(existingDomainObject.getFirstName(), equalTo(incoming.getFirstName()));
|
||||
assertThat(existingDomainObject.getLastName(), equalTo(incoming.getLastName()));
|
||||
|
||||
Reference in New Issue
Block a user