1
1
/*
2
- * Copyright 2016-2022 original author or authors.
2
+ * Copyright 2016-2023 original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
24
24
import org .junit .jupiter .api .BeforeEach ;
25
25
import org .junit .jupiter .api .Test ;
26
26
import org .springframework .beans .NotReadablePropertyException ;
27
+ import org .springframework .data .keyvalue .core .mapping .KeyValuePersistentEntity ;
28
+ import org .springframework .data .keyvalue .core .mapping .KeyValuePersistentProperty ;
27
29
import org .springframework .data .keyvalue .core .mapping .context .KeyValueMappingContext ;
28
30
import org .springframework .data .mapping .context .PersistentEntities ;
31
+ import org .springframework .data .util .TypeInformation ;
29
32
import org .springframework .validation .Errors ;
30
33
31
34
/**
32
35
* Unit tests for {@link ValidationErrors}.
33
36
*
34
37
* @author Oliver Gierke
38
+ * @author Florian Cramer
35
39
*/
36
40
class ValidationErrorsUnitTests {
37
41
@@ -40,7 +44,7 @@ class ValidationErrorsUnitTests {
40
44
@ BeforeEach
41
45
void setUp () {
42
46
43
- KeyValueMappingContext <?, ?> context = new KeyValueMappingContext <>();
47
+ KeyValueMappingContext <?, ?> context = new TestKeyValueMappingContext <>();
44
48
context .getPersistentEntity (Foo .class );
45
49
46
50
this .entities = new PersistentEntities (Arrays .asList (context ));
@@ -71,6 +75,14 @@ void returnsNullForPropertyValue() {
71
75
assertThat (errors .getFieldValue ("bar" )).isNull ();
72
76
}
73
77
78
+ @ Test // GH-2252
79
+ void getsTheNestedFieldsValueForNonPersistentEntity () {
80
+
81
+ ValidationErrors errors = new ValidationErrors (new Foo (), entities );
82
+
83
+ assertThat (errors .getFieldValue ("qux.field" )).isEqualTo ("World" );
84
+ }
85
+
74
86
private static void expectedErrorBehavior (Errors errors ) {
75
87
76
88
assertThat (errors .getFieldValue ("bars" )).isNotNull ();
@@ -88,9 +100,22 @@ private static void expectedErrorBehavior(Errors errors) {
88
100
static class Foo {
89
101
List <Bar > bars = Collections .singletonList (new Bar ());
90
102
Bar bar = null ;
103
+ Qux qux = new Qux ();
91
104
}
92
105
93
106
static class Bar {
94
107
String field = "Hello" ;
95
108
}
109
+
110
+ static class Qux {
111
+ String field = "World" ;
112
+ }
113
+
114
+ static class TestKeyValueMappingContext <E extends KeyValuePersistentEntity <?, P >, P extends KeyValuePersistentProperty <P >> extends KeyValueMappingContext <E , P > {
115
+
116
+ @ Override
117
+ protected boolean shouldCreatePersistentEntityFor (TypeInformation <?> type ) {
118
+ return Qux .class != type .getType () && super .shouldCreatePersistentEntityFor (type );
119
+ }
120
+ }
96
121
}
0 commit comments