From b5e239208e75006dbb46333367447644ed87811d Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Tue, 28 May 2019 17:22:06 +0200 Subject: [PATCH] DATAREST-1383 - Polishing. Avoid repeated lookups of PersistentPropertyAccessor in DomainObjectReader for PATCH requests. Formatting. --- .../rest/webmvc/json/DomainObjectReader.java | 4 +--- .../json/DomainObjectReaderUnitTests.java | 24 +++++-------------- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java index 4f05f8b62..f3f9d970f 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java @@ -217,6 +217,7 @@ public class DomainObjectReader { PersistentEntity entity = candidate.get(); MappedProperties mappedProperties = MappedProperties.forDeserialization(entity, mapper); + PersistentPropertyAccessor accessor = entity.getPropertyAccessor(target); for (Iterator> i = root.fields(); i.hasNext();) { @@ -230,7 +231,6 @@ public class DomainObjectReader { } PersistentProperty property = mappedProperties.getPersistentProperty(fieldName); - PersistentPropertyAccessor accessor = entity.getPropertyAccessor(target); Optional rawValue = Optional.ofNullable(accessor.getProperty(property)); if (!rawValue.isPresent() || associationLinks.isLinkableAssociation(property)) { @@ -275,9 +275,7 @@ public class DomainObjectReader { execute(() -> doMerge(objectNode, it, mapper)); } } - }); - } return mapper.readerForUpdating(target).readValue(root); diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java index ca2ccf0da..c5b143e90 100755 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/DomainObjectReaderUnitTests.java @@ -29,19 +29,7 @@ import lombok.Value; import java.io.ByteArrayInputStream; import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.UUID; +import java.util.*; import org.junit.Before; import org.junit.Test; @@ -174,11 +162,11 @@ public class DomainObjectReaderUnitTests { TypeWithGenericMap result = reader.readPut((ObjectNode) node, target, mapper); - assertThat(result.map.get("a")).isEqualTo((Object) "1"); + assertThat(result.map.get("a")).isEqualTo("1"); Object object = result.map.get("b"); assertThat(object).isInstanceOf(Map.class); - assertThat(((Map) object).get("c")).isEqualTo((Object) "2"); + assertThat(((Map) object).get("c")).isEqualTo("2"); } @Test(expected = IllegalArgumentException.class) // DATAREST-701 @@ -280,7 +268,7 @@ public class DomainObjectReaderUnitTests { TypeWithGenericMap result = reader.readPut(payload, map, mapper); - assertThat(result.map.get("sub1")).isEqualTo((Object) "ok"); + assertThat(result.map.get("sub1")).isEqualTo("ok"); List sub2 = as(result.map.get("sub2"), List.class); assertThat(sub2.get(0)).isEqualTo("ok1"); @@ -423,8 +411,8 @@ public class DomainObjectReaderUnitTests { assertThat(collection).hasSize(2); Iterator> iterator = (Iterator>) collection.iterator(); - assertThat(iterator.next().get("some")).isEqualTo((Object) "value"); - assertThat(iterator.next().get("some")).isEqualTo((Object) "otherValue"); + assertThat(iterator.next().get("some")).isEqualTo("value"); + assertThat(iterator.next().get("some")).isEqualTo("otherValue"); } @Test // DATAREST-965