Skip to content

Commit

Permalink
DATAREST-1524 - Fix deserialization of transient fiels with setters.
Browse files Browse the repository at this point in the history
Patching of non persistent field was broken after DATAREST-1383. And now it is again inline with PUT.
  • Loading branch information
bschoenmaeckers committed Sep 22, 2020
1 parent 060c84d commit 1d1370f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
* @author Craig Andrews
* @author Mathias Düsterhöft
* @author Thomas Mrozinski
* @author Bas Schoenmaeckers
* @since 2.2
*/
public class DomainObjectReader {
Expand Down Expand Up @@ -235,8 +236,12 @@ <T> T doMerge(ObjectNode root, T target, ObjectMapper mapper) throws Exception {
String fieldName = entry.getKey();

if (!mappedProperties.isWritableProperty(fieldName)) {
PropertyAccessor targetPropertyAccessor = PropertyAccessorFactory.forBeanPropertyAccess(target);

if (!targetPropertyAccessor.isWritableProperty(fieldName)) {
i.remove();
}

i.remove();
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
* @author Mathias Düsterhöft
* @author Ken Dombeck
* @author Thomas Mrozinski
* @author Bas Schoenmaeckers
*/
@RunWith(MockitoJUnitRunner.class)
public class DomainObjectReaderUnitTests {
Expand Down Expand Up @@ -576,6 +577,22 @@ public void doesNotWipeReadOnlyPropertyForPatch() throws Exception {
assertThat(result.email).isEqualTo("[email protected]");
}

@Test // DATAREST-1524
public void useTransientSettersWithNonPersistentPropertiesForPatch() throws Exception {
SampleUser user = new SampleUser("name", "password");
user.lastLogin = new Date();
user.email = "[email protected]";
user.nonPersistentField = false;

ObjectMapper mapper = new ObjectMapper();
ObjectNode source = (ObjectNode) mapper.readTree("{ \"online\" : true}");

@SuppressWarnings("deprecation")
SampleUser result = reader.merge(source, user, mapper);

assertThat(result.isOnline()).isTrue();
}

@Test // DATAREST-1068
public void arraysCanBeResizedDuringMerge() throws Exception {
ObjectMapper mapper = new ObjectMapper();
Expand Down Expand Up @@ -606,6 +623,22 @@ static class SampleUser {
@ReadOnlyProperty //
private String email;

@Transient
@JsonIgnore
boolean nonPersistentField;

@Transient
@JsonProperty
public boolean isOnline() {
return nonPersistentField;
}

@Transient
@JsonProperty
public void setOnline(Boolean online) {
this.nonPersistentField = online;
}

public SampleUser(String name, String password) {

this.name = name;
Expand Down

0 comments on commit 1d1370f

Please sign in to comment.