3131import org .springframework .core .DefaultParameterNameDiscoverer ;
3232import org .springframework .core .MethodParameter ;
3333import org .springframework .graphql .Book ;
34+ import org .springframework .graphql .data .method .OptionalInput ;
3435import org .springframework .graphql .data .method .annotation .Argument ;
3536import org .springframework .graphql .data .method .annotation .MutationMapping ;
3637import org .springframework .graphql .data .method .annotation .QueryMapping ;
@@ -83,7 +84,36 @@ void shouldResolveJavaBeanArgument() throws Exception {
8384 Object result = resolver .resolveArgument (methodParameter , environment );
8485 assertThat (result ).isNotNull ().isInstanceOf (BookInput .class );
8586 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 ));
87117 }
88118
89119 @ Test
@@ -94,8 +124,38 @@ void shouldResolveKotlinBeanArgument() throws Exception {
94124 MethodParameter methodParameter = getMethodParameter (addBook , 0 );
95125 Object result = resolver .resolveArgument (methodParameter , environment );
96126 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 ));
99159 }
100160
101161 @ Test
@@ -181,6 +241,8 @@ static class BookInput {
181241
182242 Long authorId ;
183243
244+ OptionalInput <String > notes = OptionalInput .undefined ();
245+
184246 public String getName () {
185247 return this .name ;
186248 }
@@ -196,6 +258,14 @@ public Long getAuthorId() {
196258 public void setAuthorId (Long authorId ) {
197259 this .authorId = authorId ;
198260 }
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+ }
199269 }
200270
201271}
0 commit comments