3535import org .springframework .http .codec .FormHttpMessageWriter ;
3636import org .springframework .http .codec .multipart .FilePart ;
3737import org .springframework .http .codec .multipart .MultipartHttpMessageWriter ;
38+ import org .springframework .lang .Nullable ;
3839import org .springframework .util .LinkedMultiValueMap ;
3940import org .springframework .util .MultiValueMap ;
4041import org .springframework .web .server .ServerWebExchange ;
@@ -59,15 +60,15 @@ public class WebExchangeDataBinderTests {
5960
6061
6162 @ BeforeEach
62- public void setup () throws Exception {
63+ public void setup () {
6364 this .testBean = new TestBean ();
6465 this .binder = new WebExchangeDataBinder (this .testBean , "person" );
6566 this .binder .registerCustomEditor (ITestBean .class , new TestBeanPropertyEditor ());
6667 }
6768
6869
6970 @ Test
70- public void testBindingWithNestedObjectCreation () throws Exception {
71+ public void testBindingWithNestedObjectCreation () {
7172 MultiValueMap <String , String > formData = new LinkedMultiValueMap <>();
7273 formData .add ("spouse" , "someValue" );
7374 formData .add ("spouse.name" , "test" );
@@ -78,7 +79,7 @@ public void testBindingWithNestedObjectCreation() throws Exception {
7879 }
7980
8081 @ Test
81- public void testFieldPrefixCausesFieldReset () throws Exception {
82+ public void testFieldPrefixCausesFieldReset () {
8283 MultiValueMap <String , String > formData = new LinkedMultiValueMap <>();
8384 formData .add ("_postProcessed" , "visible" );
8485 formData .add ("postProcessed" , "on" );
@@ -91,7 +92,7 @@ public void testFieldPrefixCausesFieldReset() throws Exception {
9192 }
9293
9394 @ Test
94- public void testFieldPrefixCausesFieldResetWithIgnoreUnknownFields () throws Exception {
95+ public void testFieldPrefixCausesFieldResetWithIgnoreUnknownFields () {
9596 this .binder .setIgnoreUnknownFields (false );
9697
9798 MultiValueMap <String , String > formData = new LinkedMultiValueMap <>();
@@ -115,7 +116,7 @@ public void testFieldWithEmptyArrayIndex() {
115116 }
116117
117118 @ Test
118- public void testFieldDefault () throws Exception {
119+ public void testFieldDefault () {
119120 MultiValueMap <String , String > formData = new LinkedMultiValueMap <>();
120121 formData .add ("!postProcessed" , "off" );
121122 formData .add ("postProcessed" , "on" );
@@ -128,7 +129,7 @@ public void testFieldDefault() throws Exception {
128129 }
129130
130131 @ Test
131- public void testFieldDefaultPreemptsFieldMarker () throws Exception {
132+ public void testFieldDefaultPreemptsFieldMarker () {
132133 MultiValueMap <String , String > formData = new LinkedMultiValueMap <>();
133134 formData .add ("!postProcessed" , "on" );
134135 formData .add ("_postProcessed" , "visible" );
@@ -146,7 +147,7 @@ public void testFieldDefaultPreemptsFieldMarker() throws Exception {
146147 }
147148
148149 @ Test
149- public void testFieldDefaultNonBoolean () throws Exception {
150+ public void testFieldDefaultNonBoolean () {
150151 MultiValueMap <String , String > formData = new LinkedMultiValueMap <>();
151152 formData .add ("!name" , "anonymous" );
152153 formData .add ("name" , "Scott" );
@@ -159,7 +160,7 @@ public void testFieldDefaultNonBoolean() throws Exception {
159160 }
160161
161162 @ Test
162- public void testWithCommaSeparatedStringArray () throws Exception {
163+ public void testWithCommaSeparatedStringArray () {
163164 MultiValueMap <String , String > formData = new LinkedMultiValueMap <>();
164165 formData .add ("stringArray" , "bar" );
165166 formData .add ("stringArray" , "abc" );
@@ -174,7 +175,7 @@ public void testWithCommaSeparatedStringArray() throws Exception {
174175 }
175176
176177 @ Test
177- public void testBindingWithNestedObjectCreationAndWrongOrder () throws Exception {
178+ public void testBindingWithNestedObjectCreationAndWrongOrder () {
178179 MultiValueMap <String , String > formData = new LinkedMultiValueMap <>();
179180 formData .add ("spouse.name" , "test" );
180181 formData .add ("spouse" , "someValue" );
@@ -185,7 +186,7 @@ public void testBindingWithNestedObjectCreationAndWrongOrder() throws Exception
185186 }
186187
187188 @ Test
188- public void testBindingWithQueryParams () throws Exception {
189+ public void testBindingWithQueryParams () {
189190 String url = "/path?spouse=someValue&spouse.name=test" ;
190191 ServerWebExchange exchange = MockServerWebExchange .from (MockServerHttpRequest .post (url ));
191192 this .binder .bind (exchange ).block (Duration .ofSeconds (5 ));
@@ -195,7 +196,7 @@ public void testBindingWithQueryParams() throws Exception {
195196 }
196197
197198 @ Test
198- public void testMultipart () throws Exception {
199+ public void testMultipart () {
199200
200201 MultipartBean bean = new MultipartBean ();
201202 WebExchangeDataBinder binder = new WebExchangeDataBinder (bean );
@@ -221,17 +222,17 @@ public void testMultipart() throws Exception {
221222 }
222223
223224 @ Test
224- public void testConstructorMultipart () throws Exception {
225+ public void testMultipartDataClass () {
225226 WebExchangeDataBinder binder = new WebExchangeDataBinder (null );
226- binder .setTargetType (ResolvableType .forClass (ConstructorMultipartBean .class ));
227+ binder .setTargetType (ResolvableType .forClass (MultipartDataClass .class ));
227228
228229 MultiValueMap <String , Object > data = new LinkedMultiValueMap <>();
229230 data .add ("part" , new ClassPathResource ("org/springframework/http/codec/multipart/foo.txt" ));
230231 binder .construct (exchangeMultipart (data )).block (Duration .ofMillis (5000 ));
231- ConstructorMultipartBean bean = (ConstructorMultipartBean ) binder .getTarget ();
232232
233+ MultipartDataClass bean = (MultipartDataClass ) binder .getTarget ();
233234 assertThat (bean .getPart ().filename ()).isEqualTo ("foo.txt" );
234- assertThat (bean .getNullableFilePart ()).isNull ();
235+ assertThat (bean .getNullablePart ()).isNull (); // gh-31778
235236 }
236237
237238
@@ -257,10 +258,11 @@ private ServerWebExchange exchangeMultipart(MultiValueMap<String, ?> multipartDa
257258 new MultipartHttpMessageWriter ().write (Mono .just (multipartData ), forClass (MultiValueMap .class ),
258259 MediaType .MULTIPART_FORM_DATA , request , Collections .emptyMap ()).block ();
259260
260- return MockServerWebExchange .from (MockServerHttpRequest
261- .post ("/" )
261+ MockServerHttpRequest serverRequest = MockServerHttpRequest .post ("/" )
262262 .contentType (request .getHeaders ().getContentType ())
263- .body (request .getBody ()));
263+ .body (request .getBody ());
264+
265+ return MockServerWebExchange .from (serverRequest );
264266 }
265267
266268
@@ -327,24 +329,26 @@ public void setSomePartList(List<FilePart> somePartList) {
327329 }
328330 }
329331
330- private static class ConstructorMultipartBean {
332+
333+ private static class MultipartDataClass {
334+
331335 private final FilePart part ;
332- private final FilePart nullableFilePart ;
333336
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 ) {
338341 this .part = part ;
339- this .nullableFilePart = nullableFilePart ;
342+ this .nullablePart = nullablePart ;
340343 }
341344
342345 public FilePart getPart () {
343346 return part ;
344347 }
345348
346- public FilePart getNullableFilePart () {
347- return nullableFilePart ;
349+ @ Nullable
350+ public FilePart getNullablePart () {
351+ return nullablePart ;
348352 }
349353 }
350354}
0 commit comments