1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
53
53
import org .springframework .web .server .ServerWebExchange ;
54
54
import org .springframework .web .server .ServerWebInputException ;
55
55
56
- import static org .junit .Assert .*;
57
- import static org .springframework .core .ResolvableType .*;
58
- import static org .springframework .web .method .MvcAnnotationPredicates .*;
56
+ import static org .junit .Assert .assertEquals ;
57
+ import static org .junit .Assert .assertFalse ;
58
+ import static org .junit .Assert .assertNotNull ;
59
+ import static org .junit .Assert .assertNull ;
60
+ import static org .junit .Assert .assertTrue ;
61
+ import static org .springframework .core .ResolvableType .forClass ;
62
+ import static org .springframework .web .method .MvcAnnotationPredicates .requestPart ;
59
63
60
64
/**
61
65
* Unit tests for {@link RequestPartMethodArgumentResolver}.
62
66
* @author Rossen Stoyanchev
67
+ * @author Ilya Lukyanovich
63
68
*/
64
69
public class RequestPartMethodArgumentResolverTests {
65
70
@@ -131,6 +136,15 @@ public void listPerson() {
131
136
assertEquals ("James" , actual .get (1 ).getName ());
132
137
}
133
138
139
+ @ Test // gh-23060
140
+ public void listPersonNotRequired () {
141
+ MethodParameter param = this .testMethod .annot (requestPart ().notRequired ()).arg (List .class , Person .class );
142
+ MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
143
+ List <Person > actual = resolveArgument (param , bodyBuilder );
144
+
145
+ assertEquals (Collections .emptyList (), actual );
146
+ }
147
+
134
148
@ Test
135
149
public void monoPerson () {
136
150
MethodParameter param = this .testMethod .annot (requestPart ()).arg (Mono .class , Person .class );
@@ -141,6 +155,15 @@ public void monoPerson() {
141
155
assertEquals ("Jones" , actual .block ().getName ());
142
156
}
143
157
158
+ @ Test // gh-23060
159
+ public void monoPersonNotRequired () {
160
+ MethodParameter param = this .testMethod .annot (requestPart ().notRequired ()).arg (Mono .class , Person .class );
161
+ MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
162
+ Mono <Person > actual = resolveArgument (param , bodyBuilder );
163
+
164
+ assertNull (actual .block ());
165
+ }
166
+
144
167
@ Test
145
168
public void fluxPerson () {
146
169
MethodParameter param = this .testMethod .annot (requestPart ()).arg (Flux .class , Person .class );
@@ -154,6 +177,15 @@ public void fluxPerson() {
154
177
assertEquals ("James" , persons .get (1 ).getName ());
155
178
}
156
179
180
+ @ Test // gh-23060
181
+ public void fluxPersonNotRequired () {
182
+ MethodParameter param = this .testMethod .annot (requestPart ().notRequired ()).arg (Flux .class , Person .class );
183
+ MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
184
+ Flux <Person > actual = resolveArgument (param , bodyBuilder );
185
+
186
+ assertEquals (Collections .emptyList (), actual .collectList ().block ());
187
+ }
188
+
157
189
@ Test
158
190
public void part () {
159
191
MethodParameter param = this .testMethod .annot (requestPart ()).arg (Part .class );
@@ -177,6 +209,15 @@ public void listPart() {
177
209
assertEquals ("{\" name\" :\" James\" }" , partToUtf8String (actual .get (1 )));
178
210
}
179
211
212
+ @ Test // gh-23060
213
+ public void listPartNotRequired () {
214
+ MethodParameter param = this .testMethod .annot (requestPart ().notRequired ()).arg (List .class , Part .class );
215
+ MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
216
+ List <Part > actual = resolveArgument (param , bodyBuilder );
217
+
218
+ assertEquals (Collections .emptyList (), actual );
219
+ }
220
+
180
221
@ Test
181
222
public void monoPart () {
182
223
MethodParameter param = this .testMethod .annot (requestPart ()).arg (Mono .class , Part .class );
@@ -188,6 +229,15 @@ public void monoPart() {
188
229
assertEquals ("{\" name\" :\" Jones\" }" , partToUtf8String (part ));
189
230
}
190
231
232
+ @ Test // gh-23060
233
+ public void monoPartNotRequired () {
234
+ MethodParameter param = this .testMethod .annot (requestPart ().notRequired ()).arg (Mono .class , Part .class );
235
+ MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
236
+ Mono <Part > actual = resolveArgument (param , bodyBuilder );
237
+
238
+ assertNull (actual .block ());
239
+ }
240
+
191
241
@ Test
192
242
public void fluxPart () {
193
243
MethodParameter param = this .testMethod .annot (requestPart ()).arg (Flux .class , Part .class );
@@ -201,6 +251,15 @@ public void fluxPart() {
201
251
assertEquals ("{\" name\" :\" James\" }" , partToUtf8String (parts .get (1 )));
202
252
}
203
253
254
+ @ Test // gh-23060
255
+ public void fluxPartNotRequired () {
256
+ MethodParameter param = this .testMethod .annot (requestPart ().notRequired ()).arg (Flux .class , Part .class );
257
+ MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
258
+ Flux <Part > actual = resolveArgument (param , bodyBuilder );
259
+
260
+ assertEquals (Collections .emptyList (), actual .collectList ().block ());
261
+ }
262
+
204
263
@ Test
205
264
public void personRequired () {
206
265
MethodParameter param = this .testMethod .annot (requestPart ()).arg (Person .class );
@@ -278,7 +337,13 @@ void handle(
278
337
@ RequestPart ("name" ) Flux <Part > partFlux ,
279
338
@ RequestPart ("name" ) List <Part > partList ,
280
339
@ RequestPart (name = "anotherPart" , required = false ) Person anotherPerson ,
340
+ @ RequestPart (name = "name" , required = false ) Mono <Person > anotherPersonMono ,
341
+ @ RequestPart (name = "name" , required = false ) Flux <Person > anotherPersonFlux ,
342
+ @ RequestPart (name = "name" , required = false ) List <Person > anotherPersonList ,
281
343
@ RequestPart (name = "anotherPart" , required = false ) Part anotherPart ,
344
+ @ RequestPart (name = "name" , required = false ) Mono <Part > anotherPartMono ,
345
+ @ RequestPart (name = "name" , required = false ) Flux <Part > anotherPartFlux ,
346
+ @ RequestPart (name = "name" , required = false ) List <Part > anotherPartList ,
282
347
Person notAnnotated ) {}
283
348
284
349
0 commit comments