Skip to content

Commit 2409a6a

Browse files
committed
Support Validation for fields the type of which is not a PersistentEntity
1 parent 88b6414 commit 2409a6a

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Arrays;
1919
import java.util.Collection;
2020
import java.util.Iterator;
21+
import java.util.Optional;
2122

2223
import org.springframework.beans.BeansException;
2324
import org.springframework.beans.ConfigurablePropertyAccessor;
@@ -92,7 +93,12 @@ public Object getPropertyValue(String propertyName) throws BeansException {
9293
*/
9394
private Object lookupValueOn(Object value, String segment) {
9495

95-
PersistentProperty<?> property = entities.getPersistentEntity(value.getClass()) //
96+
Optional<PersistentEntity<?, ? extends PersistentProperty<?>>> entity = entities.getPersistentEntity(value.getClass());
97+
if (!entity.isPresent()) {
98+
return new DirectFieldAccessor(value).getPropertyValue(segment);
99+
}
100+
101+
PersistentProperty<?> property = entity //
96102
.map(it -> it.getPersistentProperty(PropertyAccessorUtils.getPropertyName(segment))) //
97103
.orElseThrow(() -> new NotReadablePropertyException(value.getClass(), segment));
98104

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
import org.junit.jupiter.api.BeforeEach;
2525
import org.junit.jupiter.api.Test;
2626
import org.springframework.beans.NotReadablePropertyException;
27+
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentEntity;
28+
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentProperty;
2729
import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
2830
import org.springframework.data.mapping.context.PersistentEntities;
31+
import org.springframework.data.util.TypeInformation;
2932
import org.springframework.validation.Errors;
3033

3134
/**
@@ -40,7 +43,7 @@ class ValidationErrorsUnitTests {
4043
@BeforeEach
4144
void setUp() {
4245

43-
KeyValueMappingContext<?, ?> context = new KeyValueMappingContext<>();
46+
KeyValueMappingContext<?, ?> context = new TestKeyValueMappingContext<>();
4447
context.getPersistentEntity(Foo.class);
4548

4649
this.entities = new PersistentEntities(Arrays.asList(context));
@@ -71,6 +74,14 @@ void returnsNullForPropertyValue() {
7174
assertThat(errors.getFieldValue("bar")).isNull();
7275
}
7376

77+
@Test
78+
void getsTheNestedFieldsValueForNonPersistentEntity() {
79+
80+
ValidationErrors errors = new ValidationErrors(new Foo(), entities);
81+
82+
assertThat(errors.getFieldValue("qux.field")).isEqualTo("World");
83+
}
84+
7485
private static void expectedErrorBehavior(Errors errors) {
7586

7687
assertThat(errors.getFieldValue("bars")).isNotNull();
@@ -88,9 +99,22 @@ private static void expectedErrorBehavior(Errors errors) {
8899
static class Foo {
89100
List<Bar> bars = Collections.singletonList(new Bar());
90101
Bar bar = null;
102+
Qux qux = new Qux();
91103
}
92104

93105
static class Bar {
94106
String field = "Hello";
95107
}
108+
109+
static class Qux {
110+
String field = "World";
111+
}
112+
113+
static class TestKeyValueMappingContext<E extends KeyValuePersistentEntity<?, P>, P extends KeyValuePersistentProperty<P>> extends KeyValueMappingContext<E, P> {
114+
115+
@Override
116+
protected boolean shouldCreatePersistentEntityFor(TypeInformation<?> type) {
117+
return Qux.class != type.getType() && super.shouldCreatePersistentEntityFor(type);
118+
}
119+
}
96120
}

0 commit comments

Comments
 (0)