31
31
import org .springframework .core .DefaultParameterNameDiscoverer ;
32
32
import org .springframework .core .MethodParameter ;
33
33
import org .springframework .graphql .Book ;
34
+ import org .springframework .graphql .data .method .OptionalInput ;
34
35
import org .springframework .graphql .data .method .annotation .Argument ;
35
36
import org .springframework .graphql .data .method .annotation .MutationMapping ;
36
37
import org .springframework .graphql .data .method .annotation .QueryMapping ;
@@ -83,11 +84,40 @@ void shouldResolveJavaBeanArgument() throws Exception {
83
84
Object result = resolver .resolveArgument (methodParameter , environment );
84
85
assertThat (result ).isNotNull ().isInstanceOf (BookInput .class );
85
86
assertThat ((BookInput ) result ).hasFieldOrPropertyWithValue ("name" , "test name" )
86
- .hasFieldOrPropertyWithValue ("authorId" , 42L );
87
+ .hasFieldOrPropertyWithValue ("authorId" , 42L )
88
+ .hasFieldOrPropertyWithValue ("notes" , OptionalInput .undefined ());
87
89
}
88
90
89
91
@ Test
90
- void shouldResolveDefaultValue () throws Exception {
92
+ void shouldResolveJavaBeanOptionalArgument () throws Exception {
93
+ Method addBook = ClassUtils .getMethod (BookController .class , "addBook" , BookInput .class );
94
+ String payload = "{\" bookInput\" : { \" name\" : \" test name\" , \" authorId\" : 42, \" notes\" : \" Hello\" } }" ;
95
+ DataFetchingEnvironment environment = initEnvironment (payload );
96
+ MethodParameter methodParameter = getMethodParameter (addBook , 0 );
97
+ Object result = resolver .resolveArgument (methodParameter , environment );
98
+ assertThat (result ).isNotNull ().isInstanceOf (BookInput .class );
99
+ assertThat ((BookInput ) result )
100
+ .hasFieldOrPropertyWithValue ("name" , "test name" )
101
+ .hasFieldOrPropertyWithValue ("authorId" , 42L )
102
+ .hasFieldOrPropertyWithValue ("notes" , OptionalInput .defined ("Hello" ));
103
+ }
104
+
105
+ @ Test
106
+ void shouldResolveJavaBeanOptionalNullArgument () throws Exception {
107
+ Method addBook = ClassUtils .getMethod (BookController .class , "addBook" , BookInput .class );
108
+ String payload = "{\" bookInput\" : { \" name\" : \" test name\" , \" authorId\" : 42, \" notes\" : null} }" ;
109
+ DataFetchingEnvironment environment = initEnvironment (payload );
110
+ MethodParameter methodParameter = getMethodParameter (addBook , 0 );
111
+ Object result = resolver .resolveArgument (methodParameter , environment );
112
+ assertThat (result ).isNotNull ().isInstanceOf (BookInput .class );
113
+ assertThat ((BookInput ) result )
114
+ .hasFieldOrPropertyWithValue ("name" , "test name" )
115
+ .hasFieldOrPropertyWithValue ("authorId" , 42L )
116
+ .hasFieldOrPropertyWithValue ("notes" , OptionalInput .defined (null ));
117
+ }
118
+
119
+ @ Test
120
+ void shouldResolveDefaultValue () throws Exception {
91
121
Method findWithDefault = ClassUtils .getMethod (BookController .class , "findWithDefault" , Long .class );
92
122
String payload = "{\" name\" : \" test\" }" ;
93
123
DataFetchingEnvironment environment = initEnvironment (payload );
@@ -106,6 +136,47 @@ void shouldNotFailIfArgumentNotRequired() throws Exception {
106
136
assertThat (result ).isNull ();
107
137
}
108
138
139
+ void shouldResolveKotlinBeanArgument () throws Exception {
140
+ Method addBook = ClassUtils .getMethod (BookController .class , "ktAddBook" , KotlinBookInput .class );
141
+ String payload = "{\" bookInput\" : { \" name\" : \" test name\" , \" authorId\" : 42} }" ;
142
+ DataFetchingEnvironment environment = initEnvironment (payload );
143
+ MethodParameter methodParameter = getMethodParameter (addBook , 0 );
144
+ Object result = resolver .resolveArgument (methodParameter , environment );
145
+ assertThat (result ).isNotNull ().isInstanceOf (KotlinBookInput .class );
146
+ assertThat ((KotlinBookInput ) result )
147
+ .hasFieldOrPropertyWithValue ("name" , "test name" )
148
+ .hasFieldOrPropertyWithValue ("authorId" , 42L )
149
+ .hasFieldOrPropertyWithValue ("notes" , OptionalInput .undefined ());
150
+ }
151
+
152
+ @ Test
153
+ void shouldResolveKotlinBeanOptionalArgument () throws Exception {
154
+ Method addBook = ClassUtils .getMethod (BookController .class , "ktAddBook" , KotlinBookInput .class );
155
+ String payload = "{\" bookInput\" : { \" name\" : \" test name\" , \" authorId\" : 42, \" notes\" : \" Hello\" } }" ;
156
+ DataFetchingEnvironment environment = initEnvironment (payload );
157
+ MethodParameter methodParameter = getMethodParameter (addBook , 0 );
158
+ Object result = resolver .resolveArgument (methodParameter , environment );
159
+ assertThat (result ).isNotNull ().isInstanceOf (KotlinBookInput .class );
160
+ assertThat ((KotlinBookInput ) result )
161
+ .hasFieldOrPropertyWithValue ("name" , "test name" )
162
+ .hasFieldOrPropertyWithValue ("authorId" , 42L )
163
+ .hasFieldOrPropertyWithValue ("notes" , OptionalInput .defined ("Hello" ));
164
+ }
165
+
166
+ @ Test
167
+ void shouldResolveKotlinBeanOptionalNullArgument () throws Exception {
168
+ Method addBook = ClassUtils .getMethod (BookController .class , "ktAddBook" , KotlinBookInput .class );
169
+ String payload = "{\" bookInput\" : { \" name\" : \" test name\" , \" authorId\" : 42, \" notes\" : null} }" ;
170
+ DataFetchingEnvironment environment = initEnvironment (payload );
171
+ MethodParameter methodParameter = getMethodParameter (addBook , 0 );
172
+ Object result = resolver .resolveArgument (methodParameter , environment );
173
+ assertThat (result ).isNotNull ().isInstanceOf (KotlinBookInput .class );
174
+ assertThat ((KotlinBookInput ) result )
175
+ .hasFieldOrPropertyWithValue ("name" , "test name" )
176
+ .hasFieldOrPropertyWithValue ("authorId" , 42L )
177
+ .hasFieldOrPropertyWithValue ("notes" , OptionalInput .defined (null ));
178
+ }
179
+
109
180
@ Test
110
181
void shouldResolveListOfJavaBeansArgument () throws Exception {
111
182
Method addBooks = ClassUtils .getMethod (BookController .class , "addBooks" , List .class );
@@ -157,6 +228,11 @@ public Book addBook(@Argument BookInput bookInput) {
157
228
return null ;
158
229
}
159
230
231
+ @ MutationMapping
232
+ public Book ktAddBook (@ Argument KotlinBookInput bookInput ) {
233
+ return null ;
234
+ }
235
+
160
236
@ MutationMapping
161
237
public List <Book > addBooks (@ Argument List <Book > books ) {
162
238
return null ;
@@ -170,6 +246,8 @@ static class BookInput {
170
246
171
247
Long authorId ;
172
248
249
+ OptionalInput <String > notes = OptionalInput .undefined ();
250
+
173
251
public String getName () {
174
252
return this .name ;
175
253
}
@@ -185,6 +263,14 @@ public Long getAuthorId() {
185
263
public void setAuthorId (Long authorId ) {
186
264
this .authorId = authorId ;
187
265
}
266
+
267
+ public OptionalInput <String > getNotes () {
268
+ return this .notes ;
269
+ }
270
+
271
+ public void setNotes (OptionalInput <String > notes ) {
272
+ this .notes = (notes == null ) ? OptionalInput .defined (null ) : notes ;
273
+ }
188
274
}
189
275
190
276
static class Keyword {
0 commit comments