Skip to content

Commit e7b03f7

Browse files
committed
Don't auto-configure MultipartConfigElement when using Commons FileUpload
Previously, when a user had declared a custom MultipartResolver bean that is a CommonsMultipartResolver, part resolution would fail. The failure was occurring as the servlet container was consuming the parts before CommonsMultipartResolver had a chance to read them. This was happening because a MultipartConfigElement was being auto-configured. This commit updates the multipart auto-configuration so that a MultipartConfigElement is not auto-configured when there is a CommonsMultipartResolver bean in the context. Closes gh-7735
1 parent 92f6204 commit e7b03f7

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

spring-boot-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,11 @@
662662
<artifactId>logback-classic</artifactId>
663663
<scope>test</scope>
664664
</dependency>
665+
<dependency>
666+
<groupId>commons-fileupload</groupId>
667+
<artifactId>commons-fileupload</artifactId>
668+
<scope>test</scope>
669+
</dependency>
665670
<dependency>
666671
<groupId>com.atomikos</groupId>
667672
<artifactId>transactions-jms</artifactId>

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import org.springframework.context.annotation.Bean;
2929
import org.springframework.context.annotation.Configuration;
3030
import org.springframework.web.multipart.MultipartResolver;
31+
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
3132
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
3233
import org.springframework.web.servlet.DispatcherServlet;
3334

@@ -59,7 +60,8 @@ public MultipartAutoConfiguration(MultipartProperties multipartProperties) {
5960
}
6061

6162
@Bean
62-
@ConditionalOnMissingBean
63+
@ConditionalOnMissingBean({ MultipartConfigElement.class,
64+
CommonsMultipartResolver.class })
6365
public MultipartConfigElement multipartConfigElement() {
6466
return this.multipartProperties.createMultipartConfig();
6567
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/MultipartAutoConfigurationTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.springframework.web.bind.annotation.ResponseBody;
5050
import org.springframework.web.client.RestTemplate;
5151
import org.springframework.web.multipart.MultipartResolver;
52+
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
5253
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
5354
import org.springframework.web.servlet.DispatcherServlet;
5455
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@@ -200,6 +201,17 @@ public void containerWithCustomMultipartResolver() throws Exception {
200201
.getBean(MultipartResolver.class);
201202
assertThat(multipartResolver)
202203
.isNotInstanceOf(StandardServletMultipartResolver.class);
204+
assertThat(this.context.getBeansOfType(MultipartConfigElement.class)).hasSize(1);
205+
}
206+
207+
@Test
208+
public void containerWithCommonsMultipartResolver() throws Exception {
209+
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
210+
ContainerWithCommonsMultipartResolver.class, BaseConfiguration.class);
211+
MultipartResolver multipartResolver = this.context
212+
.getBean(MultipartResolver.class);
213+
assertThat(multipartResolver).isInstanceOf(CommonsMultipartResolver.class);
214+
assertThat(this.context.getBeansOfType(MultipartConfigElement.class)).hasSize(0);
203215
}
204216

205217
@Test
@@ -370,6 +382,15 @@ MultipartResolver multipartResolver() {
370382

371383
}
372384

385+
public static class ContainerWithCommonsMultipartResolver {
386+
387+
@Bean
388+
CommonsMultipartResolver multipartResolver() {
389+
return mock(CommonsMultipartResolver.class);
390+
}
391+
392+
}
393+
373394
@Controller
374395
public static class WebController {
375396

spring-boot-parent/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
<artifactId>log4j</artifactId>
5555
<version>1.2.17</version>
5656
</dependency>
57+
<dependency>
58+
<groupId>commons-fileupload</groupId>
59+
<artifactId>commons-fileupload</artifactId>
60+
<version>1.3.3</version>
61+
</dependency>
5762
<dependency>
5863
<groupId>com.google.guava</groupId>
5964
<artifactId>guava</artifactId>

0 commit comments

Comments
 (0)