35
35
import org .springframework .http .codec .FormHttpMessageWriter ;
36
36
import org .springframework .http .codec .multipart .FilePart ;
37
37
import org .springframework .http .codec .multipart .MultipartHttpMessageWriter ;
38
+ import org .springframework .lang .Nullable ;
38
39
import org .springframework .util .LinkedMultiValueMap ;
39
40
import org .springframework .util .MultiValueMap ;
40
41
import org .springframework .web .server .ServerWebExchange ;
@@ -59,15 +60,15 @@ public class WebExchangeDataBinderTests {
59
60
60
61
61
62
@ BeforeEach
62
- public void setup () throws Exception {
63
+ public void setup () {
63
64
this .testBean = new TestBean ();
64
65
this .binder = new WebExchangeDataBinder (this .testBean , "person" );
65
66
this .binder .registerCustomEditor (ITestBean .class , new TestBeanPropertyEditor ());
66
67
}
67
68
68
69
69
70
@ Test
70
- public void testBindingWithNestedObjectCreation () throws Exception {
71
+ public void testBindingWithNestedObjectCreation () {
71
72
MultiValueMap <String , String > formData = new LinkedMultiValueMap <>();
72
73
formData .add ("spouse" , "someValue" );
73
74
formData .add ("spouse.name" , "test" );
@@ -78,7 +79,7 @@ public void testBindingWithNestedObjectCreation() throws Exception {
78
79
}
79
80
80
81
@ Test
81
- public void testFieldPrefixCausesFieldReset () throws Exception {
82
+ public void testFieldPrefixCausesFieldReset () {
82
83
MultiValueMap <String , String > formData = new LinkedMultiValueMap <>();
83
84
formData .add ("_postProcessed" , "visible" );
84
85
formData .add ("postProcessed" , "on" );
@@ -91,7 +92,7 @@ public void testFieldPrefixCausesFieldReset() throws Exception {
91
92
}
92
93
93
94
@ Test
94
- public void testFieldPrefixCausesFieldResetWithIgnoreUnknownFields () throws Exception {
95
+ public void testFieldPrefixCausesFieldResetWithIgnoreUnknownFields () {
95
96
this .binder .setIgnoreUnknownFields (false );
96
97
97
98
MultiValueMap <String , String > formData = new LinkedMultiValueMap <>();
@@ -115,7 +116,7 @@ public void testFieldWithEmptyArrayIndex() {
115
116
}
116
117
117
118
@ Test
118
- public void testFieldDefault () throws Exception {
119
+ public void testFieldDefault () {
119
120
MultiValueMap <String , String > formData = new LinkedMultiValueMap <>();
120
121
formData .add ("!postProcessed" , "off" );
121
122
formData .add ("postProcessed" , "on" );
@@ -128,7 +129,7 @@ public void testFieldDefault() throws Exception {
128
129
}
129
130
130
131
@ Test
131
- public void testFieldDefaultPreemptsFieldMarker () throws Exception {
132
+ public void testFieldDefaultPreemptsFieldMarker () {
132
133
MultiValueMap <String , String > formData = new LinkedMultiValueMap <>();
133
134
formData .add ("!postProcessed" , "on" );
134
135
formData .add ("_postProcessed" , "visible" );
@@ -146,7 +147,7 @@ public void testFieldDefaultPreemptsFieldMarker() throws Exception {
146
147
}
147
148
148
149
@ Test
149
- public void testFieldDefaultNonBoolean () throws Exception {
150
+ public void testFieldDefaultNonBoolean () {
150
151
MultiValueMap <String , String > formData = new LinkedMultiValueMap <>();
151
152
formData .add ("!name" , "anonymous" );
152
153
formData .add ("name" , "Scott" );
@@ -159,7 +160,7 @@ public void testFieldDefaultNonBoolean() throws Exception {
159
160
}
160
161
161
162
@ Test
162
- public void testWithCommaSeparatedStringArray () throws Exception {
163
+ public void testWithCommaSeparatedStringArray () {
163
164
MultiValueMap <String , String > formData = new LinkedMultiValueMap <>();
164
165
formData .add ("stringArray" , "bar" );
165
166
formData .add ("stringArray" , "abc" );
@@ -174,7 +175,7 @@ public void testWithCommaSeparatedStringArray() throws Exception {
174
175
}
175
176
176
177
@ Test
177
- public void testBindingWithNestedObjectCreationAndWrongOrder () throws Exception {
178
+ public void testBindingWithNestedObjectCreationAndWrongOrder () {
178
179
MultiValueMap <String , String > formData = new LinkedMultiValueMap <>();
179
180
formData .add ("spouse.name" , "test" );
180
181
formData .add ("spouse" , "someValue" );
@@ -185,7 +186,7 @@ public void testBindingWithNestedObjectCreationAndWrongOrder() throws Exception
185
186
}
186
187
187
188
@ Test
188
- public void testBindingWithQueryParams () throws Exception {
189
+ public void testBindingWithQueryParams () {
189
190
String url = "/path?spouse=someValue&spouse.name=test" ;
190
191
ServerWebExchange exchange = MockServerWebExchange .from (MockServerHttpRequest .post (url ));
191
192
this .binder .bind (exchange ).block (Duration .ofSeconds (5 ));
@@ -195,7 +196,7 @@ public void testBindingWithQueryParams() throws Exception {
195
196
}
196
197
197
198
@ Test
198
- public void testMultipart () throws Exception {
199
+ public void testMultipart () {
199
200
200
201
MultipartBean bean = new MultipartBean ();
201
202
WebExchangeDataBinder binder = new WebExchangeDataBinder (bean );
@@ -221,17 +222,17 @@ public void testMultipart() throws Exception {
221
222
}
222
223
223
224
@ Test
224
- public void testConstructorMultipart () throws Exception {
225
+ public void testMultipartDataClass () {
225
226
WebExchangeDataBinder binder = new WebExchangeDataBinder (null );
226
- binder .setTargetType (ResolvableType .forClass (ConstructorMultipartBean .class ));
227
+ binder .setTargetType (ResolvableType .forClass (MultipartDataClass .class ));
227
228
228
229
MultiValueMap <String , Object > data = new LinkedMultiValueMap <>();
229
230
data .add ("part" , new ClassPathResource ("org/springframework/http/codec/multipart/foo.txt" ));
230
231
binder .construct (exchangeMultipart (data )).block (Duration .ofMillis (5000 ));
231
- ConstructorMultipartBean bean = (ConstructorMultipartBean ) binder .getTarget ();
232
232
233
+ MultipartDataClass bean = (MultipartDataClass ) binder .getTarget ();
233
234
assertThat (bean .getPart ().filename ()).isEqualTo ("foo.txt" );
234
- assertThat (bean .getNullableFilePart ()).isNull ();
235
+ assertThat (bean .getNullablePart ()).isNull (); // gh-31778
235
236
}
236
237
237
238
@@ -257,10 +258,11 @@ private ServerWebExchange exchangeMultipart(MultiValueMap<String, ?> multipartDa
257
258
new MultipartHttpMessageWriter ().write (Mono .just (multipartData ), forClass (MultiValueMap .class ),
258
259
MediaType .MULTIPART_FORM_DATA , request , Collections .emptyMap ()).block ();
259
260
260
- return MockServerWebExchange .from (MockServerHttpRequest
261
- .post ("/" )
261
+ MockServerHttpRequest serverRequest = MockServerHttpRequest .post ("/" )
262
262
.contentType (request .getHeaders ().getContentType ())
263
- .body (request .getBody ()));
263
+ .body (request .getBody ());
264
+
265
+ return MockServerWebExchange .from (serverRequest );
264
266
}
265
267
266
268
@@ -327,24 +329,26 @@ public void setSomePartList(List<FilePart> somePartList) {
327
329
}
328
330
}
329
331
330
- private static class ConstructorMultipartBean {
332
+
333
+ private static class MultipartDataClass {
334
+
331
335
private final FilePart part ;
332
- private final FilePart nullableFilePart ;
333
336
334
- public ConstructorMultipartBean (
335
- FilePart part ,
336
- FilePart nullableFilePart
337
- ) {
337
+ @ Nullable
338
+ private final FilePart nullablePart ;
339
+
340
+ MultipartDataClass ( FilePart part , @ Nullable FilePart nullablePart ) {
338
341
this .part = part ;
339
- this .nullableFilePart = nullableFilePart ;
342
+ this .nullablePart = nullablePart ;
340
343
}
341
344
342
345
public FilePart getPart () {
343
346
return part ;
344
347
}
345
348
346
- public FilePart getNullableFilePart () {
347
- return nullableFilePart ;
349
+ @ Nullable
350
+ public FilePart getNullablePart () {
351
+ return nullablePart ;
348
352
}
349
353
}
350
354
}
0 commit comments