16
16
17
17
package org .springframework .graphql .data ;
18
18
19
- import java .util .ArrayList ;
20
19
import java .util .Collections ;
21
20
import java .util .HashMap ;
22
- import java .util .HashSet ;
23
21
import java .util .List ;
24
22
import java .util .Map ;
25
23
import java .util .Objects ;
26
24
import java .util .Set ;
27
25
import java .util .stream .Collectors ;
28
26
import java .util .stream .IntStream ;
29
- import java .util .stream .Stream ;
30
27
31
28
import com .fasterxml .jackson .core .JsonProcessingException ;
32
29
import com .fasterxml .jackson .databind .ObjectMapper ;
33
30
import graphql .schema .DataFetchingEnvironment ;
34
31
import graphql .schema .DataFetchingEnvironmentImpl ;
35
- import org .assertj .core .api .CollectionAssert ;
36
32
import org .junit .jupiter .api .Test ;
37
33
38
34
import org .springframework .core .ResolvableType ;
39
35
import org .springframework .graphql .Book ;
40
- import org .springframework .graphql .data .GraphQlArgumentBinder ;
41
- import org .springframework .util .ReflectionUtils ;
42
- import org .springframework .util .StringUtils ;
43
36
import org .springframework .validation .BindException ;
44
37
import org .springframework .validation .FieldError ;
45
38
@@ -61,7 +54,7 @@ class GraphQlArgumentBinderTests {
61
54
62
55
63
56
@ Test
64
- void defaultConstructor () throws Exception {
57
+ void dataBinding () throws Exception {
65
58
66
59
Object result = this .binder .bind (
67
60
environment ("{\" key\" :{\" name\" :\" test\" }}" ), "key" ,
@@ -72,7 +65,7 @@ void defaultConstructor() throws Exception {
72
65
}
73
66
74
67
@ Test
75
- void defaultConstructorWithNestedBeanProperty () throws Exception {
68
+ void dataBindingWithNestedBeanProperty () throws Exception {
76
69
77
70
Object result = this .binder .bind (
78
71
environment (
@@ -93,7 +86,7 @@ void defaultConstructorWithNestedBeanProperty() throws Exception {
93
86
}
94
87
95
88
@ Test
96
- void defaultConstructorWithNestedBeanListProperty () throws Exception {
89
+ void dataBindingWithNestedBeanListProperty () throws Exception {
97
90
98
91
Object result = this .binder .bind (
99
92
environment ("{\" key\" :{\" items\" :[{\" name\" :\" first\" },{\" name\" :\" second\" }]}}" ), "key" ,
@@ -105,7 +98,7 @@ void defaultConstructorWithNestedBeanListProperty() throws Exception {
105
98
}
106
99
107
100
@ Test // gh-301
108
- void defaultConstructorWithNestedBeanListEmpty () throws Exception {
101
+ void dataBindingWithNestedBeanListEmpty () throws Exception {
109
102
110
103
Object result = this .binder .bind (
111
104
environment ("{\" key\" :{\" items\" : []}}" ), "key" ,
@@ -116,7 +109,7 @@ void defaultConstructorWithNestedBeanListEmpty() throws Exception {
116
109
}
117
110
118
111
@ Test // gh-280
119
- void defaultConstructorBindingError () {
112
+ void dataBindingBindingError () {
120
113
121
114
assertThatThrownBy (
122
115
() -> this .binder .bind (
@@ -131,6 +124,35 @@ void defaultConstructorBindingError() {
131
124
});
132
125
}
133
126
127
+ @ Test
128
+ @ SuppressWarnings ("unchecked" )
129
+ void dataBindingToList () throws Exception {
130
+
131
+ Object result = this .binder .bind (
132
+ environment ("{\" key\" : [\" 1\" , \" 2\" , \" 3\" ]}" ), "key" ,
133
+ ResolvableType .forClassWithGenerics (List .class , String .class ));
134
+
135
+ assertThat (result ).isNotNull ().isInstanceOf (List .class );
136
+ assertThat ((List <String >) result ).containsExactly ("1" , "2" , "3" );
137
+
138
+ // gh-486: List with null element
139
+ result = this .binder .bind (
140
+ environment ("{\" key\" : [\" 1\" , null, \" 3\" ]}" ), "key" ,
141
+ ResolvableType .forClassWithGenerics (List .class , String .class ));
142
+
143
+ assertThat (result ).isNotNull ().isInstanceOf (List .class );
144
+ assertThat ((List <String >) result ).containsExactly ("1" , null , "3" );
145
+
146
+ // Empty list
147
+
148
+ result = this .binder .bind (
149
+ environment ("{\" key\" : []}" ), "key" ,
150
+ ResolvableType .forClassWithGenerics (List .class , String .class ));
151
+
152
+ assertThat (result ).isNotNull ().isInstanceOf (List .class );
153
+ assertThat ((List <String >) result ).isEmpty ();
154
+ }
155
+
134
156
@ Test
135
157
void primaryConstructor () throws Exception {
136
158
@@ -262,7 +284,6 @@ void coercionWithSingletonList() throws Exception {
262
284
}
263
285
264
286
@ Test // gh-392
265
- @ SuppressWarnings ("unchecked" )
266
287
void shouldHaveHigherDefaultAutoGrowLimit () throws Exception {
267
288
String items = IntStream .range (0 , 260 ).mapToObj (value -> "{\" name\" :\" test\" }" ).collect (Collectors .joining ("," ));
268
289
Object result = this .binder .bind (
@@ -282,45 +303,6 @@ void shouldUseTargetCollectionType() throws Exception {
282
303
assertThat (((ItemSetHolder ) result ).getItems ()).hasSize (5 );
283
304
}
284
305
285
- @ Test
286
- @ SuppressWarnings ("unchecked" )
287
- void list () throws Exception {
288
- Object result = this .binder .bind (
289
- environment ("{\" key\" : [\" 1\" , \" 2\" , \" 3\" ]}" ),
290
- "key" ,
291
- ResolvableType .forClassWithGenerics (List .class , String .class )
292
- );
293
-
294
- assertThat (result ).isNotNull ().isInstanceOf (List .class );
295
- new CollectionAssert <>((List <String >) result ).containsExactly ("1" , "2" , "3" );
296
- }
297
-
298
- @ Test
299
- @ SuppressWarnings ("unchecked" )
300
- void listWithNullItem () throws Exception {
301
- Object result = this .binder .bind (
302
- environment ("{\" key\" : [\" 1\" , null, \" 3\" ]}" ),
303
- "key" ,
304
- ResolvableType .forClassWithGenerics (List .class , String .class )
305
- );
306
-
307
- assertThat (result ).isNotNull ().isInstanceOf (List .class );
308
- new CollectionAssert <>((List <String >) result ).containsExactly ("1" , null , "3" );
309
- }
310
-
311
- @ Test
312
- @ SuppressWarnings ("unchecked" )
313
- void emptyList () throws Exception {
314
- Object result = this .binder .bind (
315
- environment ("{\" key\" : []}" ),
316
- "key" ,
317
- ResolvableType .forClassWithGenerics (List .class , String .class )
318
- );
319
-
320
- assertThat (result ).isNotNull ().isInstanceOf (List .class );
321
- new CollectionAssert <>((List <String >) result ).isEmpty ();
322
- }
323
-
324
306
@ SuppressWarnings ("unchecked" )
325
307
private DataFetchingEnvironment environment (String jsonPayload ) throws JsonProcessingException {
326
308
Map <String , Object > arguments = this .mapper .readValue (jsonPayload , Map .class );
0 commit comments