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 );
@@ -96,6 +126,48 @@ void shouldResolveDefaultValue() throws Exception {
96
126
assertThat (result ).isNotNull ().isInstanceOf (Long .class ).isEqualTo (42L );
97
127
}
98
128
129
+ @ Test
130
+ void shouldResolveKotlinBeanArgument () throws Exception {
131
+ Method addBook = ClassUtils .getMethod (BookController .class , "ktAddBook" , KotlinBookInput .class );
132
+ String payload = "{\" bookInput\" : { \" name\" : \" test name\" , \" authorId\" : 42} }" ;
133
+ DataFetchingEnvironment environment = initEnvironment (payload );
134
+ MethodParameter methodParameter = getMethodParameter (addBook , 0 );
135
+ Object result = resolver .resolveArgument (methodParameter , environment );
136
+ assertThat (result ).isNotNull ().isInstanceOf (KotlinBookInput .class );
137
+ assertThat ((KotlinBookInput ) result )
138
+ .hasFieldOrPropertyWithValue ("name" , "test name" )
139
+ .hasFieldOrPropertyWithValue ("authorId" , 42L )
140
+ .hasFieldOrPropertyWithValue ("notes" , OptionalInput .undefined ());
141
+ }
142
+
143
+ @ Test
144
+ void shouldResolveKotlinBeanOptionalArgument () throws Exception {
145
+ Method addBook = ClassUtils .getMethod (BookController .class , "ktAddBook" , KotlinBookInput .class );
146
+ String payload = "{\" bookInput\" : { \" name\" : \" test name\" , \" authorId\" : 42, \" notes\" : \" Hello\" } }" ;
147
+ DataFetchingEnvironment environment = initEnvironment (payload );
148
+ MethodParameter methodParameter = getMethodParameter (addBook , 0 );
149
+ Object result = resolver .resolveArgument (methodParameter , environment );
150
+ assertThat (result ).isNotNull ().isInstanceOf (KotlinBookInput .class );
151
+ assertThat ((KotlinBookInput ) result )
152
+ .hasFieldOrPropertyWithValue ("name" , "test name" )
153
+ .hasFieldOrPropertyWithValue ("authorId" , 42L )
154
+ .hasFieldOrPropertyWithValue ("notes" , OptionalInput .defined ("Hello" ));
155
+ }
156
+
157
+ @ Test
158
+ void shouldResolveKotlinBeanOptionalNullArgument () throws Exception {
159
+ Method addBook = ClassUtils .getMethod (BookController .class , "ktAddBook" , KotlinBookInput .class );
160
+ String payload = "{\" bookInput\" : { \" name\" : \" test name\" , \" authorId\" : 42, \" notes\" : null} }" ;
161
+ DataFetchingEnvironment environment = initEnvironment (payload );
162
+ MethodParameter methodParameter = getMethodParameter (addBook , 0 );
163
+ Object result = resolver .resolveArgument (methodParameter , environment );
164
+ assertThat (result ).isNotNull ().isInstanceOf (KotlinBookInput .class );
165
+ assertThat ((KotlinBookInput ) result )
166
+ .hasFieldOrPropertyWithValue ("name" , "test name" )
167
+ .hasFieldOrPropertyWithValue ("authorId" , 42L )
168
+ .hasFieldOrPropertyWithValue ("notes" , OptionalInput .defined (null ));
169
+ }
170
+
99
171
@ Test
100
172
void shouldResolveListOfJavaBeansArgument () throws Exception {
101
173
Method addBooks = ClassUtils .getMethod (BookController .class , "addBooks" , List .class );
@@ -142,6 +214,11 @@ public Book addBook(@Argument BookInput bookInput) {
142
214
return null ;
143
215
}
144
216
217
+ @ MutationMapping
218
+ public Book ktAddBook (@ Argument KotlinBookInput bookInput ) {
219
+ return null ;
220
+ }
221
+
145
222
@ MutationMapping
146
223
public List <Book > addBooks (@ Argument List <Book > books ) {
147
224
return null ;
@@ -155,6 +232,8 @@ static class BookInput {
155
232
156
233
Long authorId ;
157
234
235
+ OptionalInput <String > notes = OptionalInput .undefined ();
236
+
158
237
public String getName () {
159
238
return this .name ;
160
239
}
@@ -170,6 +249,14 @@ public Long getAuthorId() {
170
249
public void setAuthorId (Long authorId ) {
171
250
this .authorId = authorId ;
172
251
}
252
+
253
+ public OptionalInput <String > getNotes () {
254
+ return this .notes ;
255
+ }
256
+
257
+ public void setNotes (OptionalInput <String > notes ) {
258
+ this .notes = (notes == null ) ? OptionalInput .defined (null ) : notes ;
259
+ }
173
260
}
174
261
175
262
}
0 commit comments