Skip to content

Commit 2b14ee3

Browse files
committed
Polishing.
Related ticket: GH-2252.
1 parent 487c2ac commit 2b14ee3

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

spring-data-rest-core/src/main/java/org/springframework/data/rest/core/ValidationErrors.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@
2828
import org.springframework.beans.PropertyAccessorUtils;
2929
import org.springframework.data.mapping.PersistentEntity;
3030
import org.springframework.data.mapping.PersistentProperty;
31-
import org.springframework.data.mapping.PersistentPropertyAccessor;
3231
import org.springframework.data.mapping.context.PersistentEntities;
32+
import org.springframework.lang.Nullable;
3333
import org.springframework.util.Assert;
3434
import org.springframework.validation.AbstractPropertyBindingResult;
35-
import org.springframework.validation.Errors;
3635

3736
/**
3837
* An {@link Errors} implementation for use in the events mechanism of Spring Data REST. Customizes actual field lookup
@@ -92,22 +91,29 @@ public Object getPropertyValue(String propertyName) throws BeansException {
9291
* @param segment the property segment to look up, must not be {@literal null} or empty.
9392
* @return
9493
*/
94+
@Nullable
9595
private Object lookupValueOn(Object value, String segment) {
9696

97-
Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> entity = entities.getPersistentEntity(value.getClass());
97+
Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> entity = entities
98+
.getPersistentEntity(value.getClass());
99+
100+
return getAccessor(entity, value, segment).getPropertyValue(segment);
101+
}
102+
103+
private static ConfigurablePropertyAccessor getAccessor(
104+
Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> entity, Object value, String segment) {
105+
98106
if (!entity.isPresent()) {
99-
return new DirectFieldAccessor(value).getPropertyValue(segment);
107+
return PropertyAccessorFactory.forDirectFieldAccess(value);
100108
}
101109

102110
PersistentProperty<?> property = entity //
103111
.map(it -> it.getPersistentProperty(PropertyAccessorUtils.getPropertyName(segment))) //
104112
.orElseThrow(() -> new NotReadablePropertyException(value.getClass(), segment));
105113

106-
ConfigurablePropertyAccessor accessor = property.usePropertyAccess() //
114+
return property.usePropertyAccess() //
107115
? PropertyAccessorFactory.forBeanPropertyAccess(value) //
108116
: PropertyAccessorFactory.forDirectFieldAccess(value);
109-
110-
return accessor.getPropertyValue(segment);
111117
}
112118
};
113119
}

spring-data-rest-core/src/test/java/org/springframework/data/rest/core/ValidationErrorsUnitTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private static void expectedErrorBehavior(Errors errors) {
9494
fail("Expected NotReadablePropertyException");
9595
} catch (NotReadablePropertyException e) {}
9696

97-
assertThat(errors.getFieldValue("field")).isEqualTo((Object) "Hello");
97+
assertThat(errors.getFieldValue("field")).isEqualTo("Hello");
9898
}
9999

100100
static class Foo {
@@ -111,7 +111,8 @@ static class Qux {
111111
String field = "World";
112112
}
113113

114-
static class TestKeyValueMappingContext<E extends KeyValuePersistentEntity<?, P>, P extends KeyValuePersistentProperty<P>> extends KeyValueMappingContext<E, P> {
114+
static class TestKeyValueMappingContext<E extends KeyValuePersistentEntity<?, P>, P extends KeyValuePersistentProperty<P>>
115+
extends KeyValueMappingContext<E, P> {
115116

116117
@Override
117118
protected boolean shouldCreatePersistentEntityFor(TypeInformation<?> type) {

0 commit comments

Comments
 (0)