Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DATAREST-1524 - Fix deserialization of transient fiels with setters. #381

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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