Skip to content

Commit bafdae6

Browse files
committed
Merge pull request #49791 from husseinvr97
* gh-49623-default-html-escape-property: Polish "Add configuration for HTML escaping configuration with WebFlux" Add configuration for HTML escaping configuration with WebFlux Closes gh-49791
2 parents 6c66555 + c747c9c commit bafdae6

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

module/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/HttpHandlerAutoConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@ static class AnnotationConfig {
6464
@Bean
6565
HttpHandler httpHandler(ObjectProvider<WebFluxProperties> propsProvider,
6666
ObjectProvider<WebHttpHandlerBuilderCustomizer> handlerBuilderCustomizers) {
67+
WebFluxProperties properties = propsProvider.getIfAvailable();
6768
WebHttpHandlerBuilder handlerBuilder = WebHttpHandlerBuilder.applicationContext(this.applicationContext);
69+
if (properties != null) {
70+
handlerBuilder.defaultHtmlEscape(properties.getDefaultHtmlEscape());
71+
}
6872
handlerBuilderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(handlerBuilder));
6973
HttpHandler httpHandler = handlerBuilder.build();
70-
WebFluxProperties properties = propsProvider.getIfAvailable();
7174
if (properties != null && StringUtils.hasText(properties.getBasePath())) {
7275
Map<String, HttpHandler> handlersMap = Collections.singletonMap(properties.getBasePath(), httpHandler);
7376
return new ContextPathCompositeHandler(handlersMap);

module/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/WebFluxProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public class WebFluxProperties {
5858
*/
5959
private String webjarsPathPattern = "/webjars/**";
6060

61+
/**
62+
* Whether default HTML escaping is enabled for the web application.
63+
*/
64+
private @Nullable Boolean defaultHtmlEscape;
65+
6166
public @Nullable String getBasePath() {
6267
return this.basePath;
6368
}
@@ -110,6 +115,14 @@ public void setWebjarsPathPattern(String webjarsPathPattern) {
110115
this.webjarsPathPattern = webjarsPathPattern;
111116
}
112117

118+
public @Nullable Boolean getDefaultHtmlEscape() {
119+
return this.defaultHtmlEscape;
120+
}
121+
122+
public void setDefaultHtmlEscape(@Nullable Boolean defaultHtmlEscape) {
123+
this.defaultHtmlEscape = defaultHtmlEscape;
124+
}
125+
113126
public static class Format {
114127

115128
/**

module/spring-boot-webflux/src/test/java/org/springframework/boot/webflux/autoconfigure/HttpHandlerAutoConfigurationTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import org.assertj.core.api.InstanceOfAssertFactories;
2020
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.ValueSource;
2123
import reactor.core.publisher.Mono;
2224

2325
import org.springframework.boot.autoconfigure.AutoConfigurations;
@@ -35,6 +37,7 @@
3537
import org.springframework.web.reactive.function.server.RouterFunction;
3638
import org.springframework.web.reactive.function.server.ServerResponse;
3739
import org.springframework.web.server.WebHandler;
40+
import org.springframework.web.server.adapter.HttpWebHandlerAdapter;
3841

3942
import static org.assertj.core.api.Assertions.assertThat;
4043
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
@@ -100,6 +103,29 @@ void shouldConfigureBasePathCompositeHandler() {
100103
});
101104
}
102105

106+
@ParameterizedTest
107+
@ValueSource(booleans = { true, false })
108+
void shouldConfigureDefaultHtmlEscape(boolean enabled) {
109+
this.contextRunner.withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class))
110+
.withPropertyValues("spring.webflux.default-html-escape=" + enabled)
111+
.run((context) -> {
112+
assertThat(context).hasSingleBean(HttpHandler.class);
113+
assertThat(context.getBean(HttpHandler.class)).isInstanceOfSatisfying(HttpWebHandlerAdapter.class,
114+
(adapter) -> assertThat(adapter.getDefaultHtmlEscape()).isEqualTo(enabled));
115+
});
116+
}
117+
118+
@Test
119+
void shouldNotConfigureDefaultHtmlEscaperWithoutWebFluxAutoConfiguration() {
120+
this.contextRunner.withUserConfiguration(CustomWebHandler.class)
121+
.withPropertyValues("spring.webflux.default-html-escape=true")
122+
.run((context) -> {
123+
assertThat(context).hasSingleBean(HttpHandler.class);
124+
assertThat(context.getBean(HttpHandler.class)).isInstanceOfSatisfying(HttpWebHandlerAdapter.class,
125+
(adapter) -> assertThat(adapter.getDefaultHtmlEscape()).isNull());
126+
});
127+
}
128+
103129
@Configuration(proxyBeanMethods = false)
104130
static class CustomHttpHandler {
105131

0 commit comments

Comments
 (0)