2424import org .junit .jupiter .api .BeforeEach ;
2525import org .junit .jupiter .api .Test ;
2626import org .springframework .beans .NotReadablePropertyException ;
27+ import org .springframework .data .keyvalue .core .mapping .KeyValuePersistentEntity ;
28+ import org .springframework .data .keyvalue .core .mapping .KeyValuePersistentProperty ;
2729import org .springframework .data .keyvalue .core .mapping .context .KeyValueMappingContext ;
2830import org .springframework .data .mapping .context .PersistentEntities ;
31+ import org .springframework .data .util .TypeInformation ;
2932import 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