From 94a9748ce0764cc52271385402e43d93c2232fa8 Mon Sep 17 00:00:00 2001 From: Marcus Eisele Date: Mon, 6 May 2019 21:17:28 +0200 Subject: [PATCH] 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 --- .../boot/autoconfigure/web/ResourceProperties.java | 4 ++++ .../boot/autoconfigure/web/ResourcePropertiesTests.java | 2 +- .../web/servlet/WebMvcAutoConfigurationTests.java | 7 +++---- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java index ade901404b..8f27f33f5f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java @@ -476,6 +476,10 @@ public class ResourceProperties { .staleIfError(duration.getSeconds(), TimeUnit.SECONDS)); map.from(this::getSMaxAge).whenNonNull().to((duration) -> control .sMaxAge(duration.getSeconds(), TimeUnit.SECONDS)); + // check if cacheControl remained untouched + if (control.getHeaderValue() == null) { + return null; + } return control; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ResourcePropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ResourcePropertiesTests.java index 9ec932a9f4..292dd7480e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ResourcePropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ResourcePropertiesTests.java @@ -76,7 +76,7 @@ public class ResourcePropertiesTests { public void emptyCacheControl() { CacheControl cacheControl = this.properties.getCache().getCachecontrol() .toHttpCacheControl(); - assertThat(cacheControl.getHeaderValue()).isNull(); + assertThat(cacheControl).isNull(); } @Test diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java index 7a346ad1e9..294fede283 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java @@ -803,13 +803,12 @@ public class WebMvcAutoConfigurationTests { Map handlerMap = getHandlerMap( context.getBean("resourceHandlerMapping", HandlerMapping.class)); assertThat(handlerMap).hasSize(2); - for (Object handler : handlerMap.keySet()) { + for (Object handler : handlerMap.values()) { if (handler instanceof ResourceHttpRequestHandler) { assertThat(((ResourceHttpRequestHandler) handler).getCacheSeconds()) - .isEqualTo(-1); + .isEqualTo(5); assertThat(((ResourceHttpRequestHandler) handler).getCacheControl()) - .isEqualToComparingFieldByField( - CacheControl.maxAge(5, TimeUnit.SECONDS)); + .isNull(); } } }