Skip to content

Commit 1b00d38

Browse files
committed
Only apply non-null Groovy Template properties
Previously, properties were always applied even when they had a null value. When using the property defaults, this would leave the GroovyMarkupConfigurer bean with null values for autoIndentString, locale, and newLineString. The null values could cause problems at runtime, for example an NPE when Groovy Templates tries to call toString on the null newLineString. Closes gh-47139
1 parent 3ca81e3 commit 1b00d38

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private boolean isUsingGroovyAllJar() {
115115
GroovyMarkupConfigurer groovyMarkupConfigurer(ObjectProvider<MarkupTemplateEngine> templateEngine,
116116
Environment environment) {
117117
GroovyMarkupConfigurer configurer = new GroovyMarkupConfigurer();
118-
PropertyMapper map = PropertyMapper.get();
118+
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
119119
map.from(this.properties::isAutoEscape).to(configurer::setAutoEscape);
120120
map.from(this.properties::isAutoIndent).to(configurer::setAutoIndent);
121121
map.from(this.properties::getAutoIndentString).to(configurer::setAutoIndentString);

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfigurationTests.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Map;
2626

2727
import groovy.text.markup.BaseTemplate;
28+
import groovy.text.markup.DelegatingIndentWriter;
2829
import groovy.text.markup.MarkupTemplateEngine;
2930
import groovy.text.markup.TemplateConfiguration;
3031
import jakarta.servlet.http.HttpServletRequest;
@@ -159,9 +160,16 @@ void customSuffix() throws Exception {
159160
assertThat(result).contains("suffixed");
160161
}
161162

163+
@Test
164+
void defaultResourceLoaderPath() throws Exception {
165+
registerAndRefreshContext();
166+
assertThat(this.context.getBean(GroovyMarkupConfigurer.class).getResourceLoaderPath())
167+
.isEqualTo(GroovyTemplateProperties.DEFAULT_RESOURCE_LOADER_PATH);
168+
}
169+
162170
@Test
163171
@WithResource(name = "custom-templates/custom.tpl", content = "yield \"custom\"")
164-
void customTemplateLoaderPath() throws Exception {
172+
void customResourceLoaderPath() throws Exception {
165173
registerAndRefreshContext("spring.groovy.template.resource-loader-path:classpath:/custom-templates/");
166174
MockHttpServletResponse response = render("custom");
167175
String result = response.getContentAsString();
@@ -206,6 +214,13 @@ void enableAutoIndent() {
206214
assertThat(this.context.getBean(GroovyMarkupConfigurer.class).isAutoIndent()).isTrue();
207215
}
208216

217+
@Test
218+
void defaultAutoIndentString() {
219+
registerAndRefreshContext();
220+
assertThat(this.context.getBean(GroovyMarkupConfigurer.class).getAutoIndentString())
221+
.isEqualTo(DelegatingIndentWriter.SPACES);
222+
}
223+
209224
@Test
210225
void customAutoIndentString() {
211226
registerAndRefreshContext("spring.groovy.template.auto-indent-string:\\t");
@@ -218,13 +233,26 @@ void enableAutoNewLine() {
218233
assertThat(this.context.getBean(GroovyMarkupConfigurer.class).isAutoNewLine()).isTrue();
219234
}
220235

236+
@Test
237+
void defaultBaseTemplateClass() {
238+
registerAndRefreshContext();
239+
assertThat(this.context.getBean(GroovyMarkupConfigurer.class).getBaseTemplateClass())
240+
.isEqualTo(BaseTemplate.class);
241+
}
242+
221243
@Test
222244
void customBaseTemplateClass() {
223245
registerAndRefreshContext("spring.groovy.template.base-template-class:" + CustomBaseTemplate.class.getName());
224246
assertThat(this.context.getBean(GroovyMarkupConfigurer.class).getBaseTemplateClass())
225247
.isEqualTo(CustomBaseTemplate.class);
226248
}
227249

250+
@Test
251+
void defaultDeclarationEncoding() {
252+
registerAndRefreshContext();
253+
assertThat(this.context.getBean(GroovyMarkupConfigurer.class).getDeclarationEncoding()).isNull();
254+
}
255+
228256
@Test
229257
void customDeclarationEncoding() {
230258
registerAndRefreshContext("spring.groovy.template.declaration-encoding:UTF-8");
@@ -237,12 +265,25 @@ void enableExpandEmptyElements() {
237265
assertThat(this.context.getBean(GroovyMarkupConfigurer.class).isExpandEmptyElements()).isTrue();
238266
}
239267

268+
@Test
269+
void defaultLocale() {
270+
registerAndRefreshContext();
271+
assertThat(this.context.getBean(GroovyMarkupConfigurer.class).getLocale()).isEqualTo(Locale.getDefault());
272+
}
273+
240274
@Test
241275
void customLocale() {
242276
registerAndRefreshContext("spring.groovy.template.locale:en_US");
243277
assertThat(this.context.getBean(GroovyMarkupConfigurer.class).getLocale()).isEqualTo(Locale.US);
244278
}
245279

280+
@Test
281+
void defaultNewLineString() {
282+
registerAndRefreshContext();
283+
assertThat(this.context.getBean(GroovyMarkupConfigurer.class).getNewLineString())
284+
.isEqualTo(System.lineSeparator());
285+
}
286+
246287
@Test
247288
void customNewLineString() {
248289
registerAndRefreshContext("spring.groovy.template.new-line-string:\\r\\n");

0 commit comments

Comments
 (0)