From 67c5d100514272ae093fbe31f1173edd97fa753b Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 10 Nov 2023 11:36:13 +0100 Subject: [PATCH] Process multipart properties for PartEvent support Prior to this commit, some properties in the `spring.webflux.multipart` namespace were ignored for the streaming use case because those were not supported in streaming mode with `PartEvent`. As of Spring Framework 6.1, the `max-parts` and `max-disk-usage-per-part` properties can be supported and this commit maps those properties accordingly. Fixes gh-37642 --- .../web/reactive/ReactiveMultipartAutoConfiguration.java | 4 ++++ .../web/reactive/ReactiveMultipartProperties.java | 8 ++++---- .../reactive/ReactiveMultipartAutoConfigurationTests.java | 6 +++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveMultipartAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveMultipartAutoConfiguration.java index 722c5706f31..45ba6300716 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveMultipartAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveMultipartAutoConfiguration.java @@ -78,6 +78,10 @@ public class ReactiveMultipartAutoConfiguration { map.from(multipartProperties::getMaxHeadersSize) .asInt(DataSize::toBytes) .to(partEventHttpMessageReader::setMaxHeadersSize); + map.from(multipartProperties::getMaxDiskUsagePerPart) + .as(DataSize::toBytes) + .to(partEventHttpMessageReader::setMaxPartSize); + map.from(multipartProperties::getMaxParts).to(partEventHttpMessageReader::setMaxParts); map.from(multipartProperties::getHeadersCharset).to(partEventHttpMessageReader::setHeadersCharset); } }); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveMultipartProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveMultipartProperties.java index b1bd6d7854d..01b4e005a2e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveMultipartProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveMultipartProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,7 +37,7 @@ public class ReactiveMultipartProperties { /** * Maximum amount of memory allowed per part before it's written to disk. Set to -1 to - * store all contents in memory. Ignored when streaming is enabled. + * store all contents in memory. */ private DataSize maxInMemorySize = DataSize.ofKilobytes(256); @@ -49,7 +49,7 @@ public class ReactiveMultipartProperties { /** * Maximum amount of disk space allowed per part. Default is -1 which enforces no - * limits. Ignored when streaming is enabled. + * limits. */ private DataSize maxDiskUsagePerPart = DataSize.ofBytes(-1); @@ -62,7 +62,7 @@ public class ReactiveMultipartProperties { /** * Directory used to store file parts larger than 'maxInMemorySize'. Default is a * directory named 'spring-multipart' created under the system temporary directory. - * Ignored when streaming is enabled. + * Ignored when using the PartEvent streaming support. */ private String fileStorageDirectory; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveMultipartAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveMultipartAutoConfigurationTests.java index d5925346e13..ae3d0099ce2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveMultipartAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveMultipartAutoConfigurationTests.java @@ -84,17 +84,21 @@ class ReactiveMultipartAutoConfigurationTests { void shouldConfigureMultipartPropertiesForPartEventReader() { this.contextRunner .withPropertyValues("spring.webflux.multipart.max-in-memory-size=1GB", - "spring.webflux.multipart.max-headers-size=16KB", "spring.webflux.multipart.headers-charset:UTF_16") + "spring.webflux.multipart.max-headers-size=16KB", + "spring.webflux.multipart.max-disk-usage-per-part=3GB", "spring.webflux.multipart.max-parts=7", + "spring.webflux.multipart.headers-charset:UTF_16") .run((context) -> { CodecCustomizer customizer = context.getBean(CodecCustomizer.class); DefaultServerCodecConfigurer configurer = new DefaultServerCodecConfigurer(); customizer.customize(configurer); PartEventHttpMessageReader partReader = getPartEventReader(configurer); + assertThat(partReader).hasFieldOrPropertyWithValue("maxParts", 7); assertThat(partReader).hasFieldOrPropertyWithValue("maxHeadersSize", Math.toIntExact(DataSize.ofKilobytes(16).toBytes())); assertThat(partReader).hasFieldOrPropertyWithValue("headersCharset", StandardCharsets.UTF_16); assertThat(partReader).hasFieldOrPropertyWithValue("maxInMemorySize", Math.toIntExact(DataSize.ofGigabytes(1).toBytes())); + assertThat(partReader).hasFieldOrPropertyWithValue("maxPartSize", DataSize.ofGigabytes(3).toBytes()); }); }