Skip to content

Commit 94a9748

Browse files
eiselemsbclozel
authored andcommitted
Fix 'spring.resources.cache.period' for WebMvc
Before this change it got overwritten by forwarding an empty CacheControl to Spring. Spring itself sets CacheSeconds already correctly in absence (=null) of a CacheControl. Also: * Fixes bug in WebMvcAutoConfigurationTests.cachePeriod which prevented it to assert anything See gh-16488 Closes gh-16730
1 parent 0b4934d commit 94a9748

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@ public CacheControl toHttpCacheControl() {
476476
.staleIfError(duration.getSeconds(), TimeUnit.SECONDS));
477477
map.from(this::getSMaxAge).whenNonNull().to((duration) -> control
478478
.sMaxAge(duration.getSeconds(), TimeUnit.SECONDS));
479+
// check if cacheControl remained untouched
480+
if (control.getHeaderValue() == null) {
481+
return null;
482+
}
479483
return control;
480484
}
481485

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void customStaticLocationsAreNormalizedToEndWithTrailingSlash() {
7676
public void emptyCacheControl() {
7777
CacheControl cacheControl = this.properties.getCache().getCachecontrol()
7878
.toHttpCacheControl();
79-
assertThat(cacheControl.getHeaderValue()).isNull();
79+
assertThat(cacheControl).isNull();
8080
}
8181

8282
@Test

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -803,13 +803,12 @@ private void assertCachePeriod(AssertableWebApplicationContext context) {
803803
Map<String, Object> handlerMap = getHandlerMap(
804804
context.getBean("resourceHandlerMapping", HandlerMapping.class));
805805
assertThat(handlerMap).hasSize(2);
806-
for (Object handler : handlerMap.keySet()) {
806+
for (Object handler : handlerMap.values()) {
807807
if (handler instanceof ResourceHttpRequestHandler) {
808808
assertThat(((ResourceHttpRequestHandler) handler).getCacheSeconds())
809-
.isEqualTo(-1);
809+
.isEqualTo(5);
810810
assertThat(((ResourceHttpRequestHandler) handler).getCacheControl())
811-
.isEqualToComparingFieldByField(
812-
CacheControl.maxAge(5, TimeUnit.SECONDS));
811+
.isNull();
813812
}
814813
}
815814
}

0 commit comments

Comments
 (0)