Fix AOT processing of @MultipartConfig annotated @WebServlet

Closes gh-37637
This commit is contained in:
Andy Wilkinson 2023-10-13 16:21:15 +01:00
parent ae2693b7f1
commit e314e11985
2 changed files with 24 additions and 3 deletions

View File

@ -59,14 +59,18 @@ class WebServletHandler extends ServletComponentHandler {
: beanDefinition.getBeanClassName()); : beanDefinition.getBeanClassName());
} }
private MultipartConfigElement determineMultipartConfig(AnnotatedBeanDefinition beanDefinition) { private BeanDefinition determineMultipartConfig(AnnotatedBeanDefinition beanDefinition) {
Map<String, Object> attributes = beanDefinition.getMetadata() Map<String, Object> attributes = beanDefinition.getMetadata()
.getAnnotationAttributes(MultipartConfig.class.getName()); .getAnnotationAttributes(MultipartConfig.class.getName());
if (attributes == null) { if (attributes == null) {
return null; return null;
} }
return new MultipartConfigElement((String) attributes.get("location"), (Long) attributes.get("maxFileSize"), BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(MultipartConfigElement.class);
(Long) attributes.get("maxRequestSize"), (Integer) attributes.get("fileSizeThreshold")); builder.addConstructorArgValue(attributes.get("location"));
builder.addConstructorArgValue(attributes.get("maxFileSize"));
builder.addConstructorArgValue(attributes.get("maxRequestSize"));
builder.addConstructorArgValue(attributes.get("fileSizeThreshold"));
return builder.getBeanDefinition();
} }
} }

View File

@ -39,6 +39,7 @@ import org.springframework.javapoet.ClassName;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatNoException;
/** /**
* Tests for {@link ServletComponentScanRegistrar} * Tests for {@link ServletComponentScanRegistrar}
@ -158,6 +159,16 @@ class ServletComponentScanRegistrarTests {
.accepts(generationContext.getRuntimeHints()); .accepts(generationContext.getRuntimeHints());
} }
@Test
void processAheadOfTimeSucceedsForWebServletWithMultipartConfig() {
AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext();
context.registerBean(ScanServletPackage.class);
TestGenerationContext generationContext = new TestGenerationContext(
ClassName.get(getClass().getPackageName(), "TestTarget"));
assertThatNoException()
.isThrownBy(() -> new ApplicationContextAotGenerator().processAheadOfTime(context, generationContext));
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void compile(GenericApplicationContext context, Consumer<GenericApplicationContext> freshContext) { private void compile(GenericApplicationContext context, Consumer<GenericApplicationContext> freshContext) {
TestGenerationContext generationContext = new TestGenerationContext( TestGenerationContext generationContext = new TestGenerationContext(
@ -215,4 +226,10 @@ class ServletComponentScanRegistrarTests {
} }
@Configuration(proxyBeanMethods = false)
@ServletComponentScan("org.springframework.boot.web.servlet.testcomponents.servlet")
static class ScanServletPackage {
}
} }