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,7 +84,36 @@ 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 ());
89
+ }
90
+
91
+ @ Test
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 ));
87
117
}
88
118
89
119
@ Test
@@ -94,8 +124,38 @@ void shouldResolveKotlinBeanArgument() throws Exception {
94
124
MethodParameter methodParameter = getMethodParameter (addBook , 0 );
95
125
Object result = resolver .resolveArgument (methodParameter , environment );
96
126
assertThat (result ).isNotNull ().isInstanceOf (KotlinBookInput .class );
97
- assertThat ((KotlinBookInput ) result ).hasFieldOrPropertyWithValue ("name" , "test name" )
98
- .hasFieldOrPropertyWithValue ("authorId" , 42L );
127
+ assertThat ((KotlinBookInput ) result )
128
+ .hasFieldOrPropertyWithValue ("name" , "test name" )
129
+ .hasFieldOrPropertyWithValue ("authorId" , 42L )
130
+ .hasFieldOrPropertyWithValue ("notes" , OptionalInput .undefined ());
131
+ }
132
+
133
+ @ Test
134
+ void shouldResolveKotlinBeanOptionalArgument () throws Exception {
135
+ Method addBook = ClassUtils .getMethod (BookController .class , "ktAddBook" , KotlinBookInput .class );
136
+ String payload = "{\" bookInput\" : { \" name\" : \" test name\" , \" authorId\" : 42, \" notes\" : \" Hello\" } }" ;
137
+ DataFetchingEnvironment environment = initEnvironment (payload );
138
+ MethodParameter methodParameter = getMethodParameter (addBook , 0 );
139
+ Object result = resolver .resolveArgument (methodParameter , environment );
140
+ assertThat (result ).isNotNull ().isInstanceOf (KotlinBookInput .class );
141
+ assertThat ((KotlinBookInput ) result )
142
+ .hasFieldOrPropertyWithValue ("name" , "test name" )
143
+ .hasFieldOrPropertyWithValue ("authorId" , 42L )
144
+ .hasFieldOrPropertyWithValue ("notes" , OptionalInput .defined ("Hello" ));
145
+ }
146
+
147
+ @ Test
148
+ void shouldResolveKotlinBeanOptionalNullArgument () throws Exception {
149
+ Method addBook = ClassUtils .getMethod (BookController .class , "ktAddBook" , KotlinBookInput .class );
150
+ String payload = "{\" bookInput\" : { \" name\" : \" test name\" , \" authorId\" : 42, \" notes\" : null} }" ;
151
+ DataFetchingEnvironment environment = initEnvironment (payload );
152
+ MethodParameter methodParameter = getMethodParameter (addBook , 0 );
153
+ Object result = resolver .resolveArgument (methodParameter , environment );
154
+ assertThat (result ).isNotNull ().isInstanceOf (KotlinBookInput .class );
155
+ assertThat ((KotlinBookInput ) result )
156
+ .hasFieldOrPropertyWithValue ("name" , "test name" )
157
+ .hasFieldOrPropertyWithValue ("authorId" , 42L )
158
+ .hasFieldOrPropertyWithValue ("notes" , OptionalInput .defined (null ));
99
159
}
100
160
101
161
@ Test
@@ -181,6 +241,8 @@ static class BookInput {
181
241
182
242
Long authorId ;
183
243
244
+ OptionalInput <String > notes = OptionalInput .undefined ();
245
+
184
246
public String getName () {
185
247
return this .name ;
186
248
}
@@ -196,6 +258,14 @@ public Long getAuthorId() {
196
258
public void setAuthorId (Long authorId ) {
197
259
this .authorId = authorId ;
198
260
}
261
+
262
+ public OptionalInput <String > getNotes () {
263
+ return this .notes ;
264
+ }
265
+
266
+ public void setNotes (OptionalInput <String > notes ) {
267
+ this .notes = (notes == null ) ? OptionalInput .defined (null ) : notes ;
268
+ }
199
269
}
200
270
201
271
}
0 commit comments