Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Related ticket: GH-2252.
  • Loading branch information
odrotbohm committed Mar 14, 2024
1 parent ece17d5 commit 1114ad7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
import org.springframework.beans.PropertyAccessorUtils;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.validation.AbstractPropertyBindingResult;
import org.springframework.validation.Errors;

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

Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> entity = entities.getPersistentEntity(value.getClass());
Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> entity = entities
.getPersistentEntity(value.getClass());

return getAccessor(entity, value, segment).getPropertyValue(segment);
}

private static ConfigurablePropertyAccessor getAccessor(
Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> entity, Object value, String segment) {

if (!entity.isPresent()) {
return new DirectFieldAccessor(value).getPropertyValue(segment);
return PropertyAccessorFactory.forDirectFieldAccess(value);
}

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

ConfigurablePropertyAccessor accessor = property.usePropertyAccess() //
return property.usePropertyAccess() //
? PropertyAccessorFactory.forBeanPropertyAccess(value) //
: PropertyAccessorFactory.forDirectFieldAccess(value);

return accessor.getPropertyValue(segment);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private static void expectedErrorBehavior(Errors errors) {
fail("Expected NotReadablePropertyException");
} catch (NotReadablePropertyException e) {}

assertThat(errors.getFieldValue("field")).isEqualTo((Object) "Hello");
assertThat(errors.getFieldValue("field")).isEqualTo("Hello");
}

static class Foo {
Expand All @@ -111,7 +111,8 @@ static class Qux {
String field = "World";
}

static class TestKeyValueMappingContext<E extends KeyValuePersistentEntity<?, P>, P extends KeyValuePersistentProperty<P>> extends KeyValueMappingContext<E, P> {
static class TestKeyValueMappingContext<E extends KeyValuePersistentEntity<?, P>, P extends KeyValuePersistentProperty<P>>
extends KeyValueMappingContext<E, P> {

@Override
protected boolean shouldCreatePersistentEntityFor(TypeInformation<?> type) {
Expand Down

0 comments on commit 1114ad7

Please sign in to comment.