Skip to content

Commit 442894b

Browse files
DATAREST-1524 - Fix deserialization of transient fields with setters.
Patching of non persistent field was broken after DATAREST-1383. And now it is again inline with PUT.
1 parent 060c84d commit 442894b

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
* @author Craig Andrews
6161
* @author Mathias Düsterhöft
6262
* @author Thomas Mrozinski
63+
* @author Bas Schoenmaeckers
6364
* @since 2.2
6465
*/
6566
public class DomainObjectReader {
@@ -235,8 +236,12 @@ <T> T doMerge(ObjectNode root, T target, ObjectMapper mapper) throws Exception {
235236
String fieldName = entry.getKey();
236237

237238
if (!mappedProperties.isWritableProperty(fieldName)) {
239+
PropertyAccessor targetPropertyAccessor = PropertyAccessorFactory.forBeanPropertyAccess(target);
240+
241+
if (!targetPropertyAccessor.isWritableProperty(fieldName)) {
242+
i.remove();
243+
}
238244

239-
i.remove();
240245
continue;
241246
}
242247

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
* @author Mathias Düsterhöft
7575
* @author Ken Dombeck
7676
* @author Thomas Mrozinski
77+
* @author Bas Schoenmaeckers
7778
*/
7879
@RunWith(MockitoJUnitRunner.class)
7980
public class DomainObjectReaderUnitTests {
@@ -576,6 +577,22 @@ public void doesNotWipeReadOnlyPropertyForPatch() throws Exception {
576577
assertThat(result.email).isEqualTo("foo@bar.com");
577578
}
578579

580+
@Test // DATAREST-1524
581+
public void useTransientSettersWithNonPersistentPropertiesForPatch() throws Exception {
582+
SampleUser user = new SampleUser("name", "password");
583+
user.lastLogin = new Date();
584+
user.email = "foo@bar.com";
585+
user.nonPersistentField = false;
586+
587+
ObjectMapper mapper = new ObjectMapper();
588+
ObjectNode source = (ObjectNode) mapper.readTree("{ \"online\" : true}");
589+
590+
@SuppressWarnings("deprecation")
591+
SampleUser result = reader.merge(source, user, mapper);
592+
593+
assertThat(result.isOnline()).isTrue();
594+
}
595+
579596
@Test // DATAREST-1068
580597
public void arraysCanBeResizedDuringMerge() throws Exception {
581598
ObjectMapper mapper = new ObjectMapper();
@@ -606,6 +623,22 @@ static class SampleUser {
606623
@ReadOnlyProperty //
607624
private String email;
608625

626+
@Transient
627+
@JsonIgnore
628+
boolean nonPersistentField;
629+
630+
@Transient
631+
@JsonProperty
632+
public boolean isOnline() {
633+
return nonPersistentField;
634+
}
635+
636+
@Transient
637+
@JsonProperty
638+
public void setOnline(Boolean online) {
639+
this.nonPersistentField = online;
640+
}
641+
609642
public SampleUser(String name, String password) {
610643

611644
this.name = name;

0 commit comments

Comments
 (0)