Repository: urlaubsverwaltung/urlaubsverwaltung
Pull request #4437 (2023-12-26 11:28:33)

avoid adding infoBannerText as flash attribute
<!--

Thanks for contributing to the Urlaubsverwaltung.
Please review the following notes before submitting you pull request.

Please look for other issues or pull requests which already work on this topic. Is somebody already on it? Do you need to synchronize?

# Security Vulnerabilities

🛑 STOP! 🛑 If your contribution fixes a security vulnerability, please do not submit it.
Instead, please open a report of a security vulnerability via
[Report a security vulnerability](https://github.com/urlaubsverwaltung/urlaubsverwaltung/security/advisories/new)


# Describing Your Changes

If, having reviewed the notes above, you're ready to submit your pull request, please
provide a brief description of the proposed changes.

If they:
 🐞 fix a bug, please describe the broken behaviour and how the changes fix it.
    Please label with 'type: bug' and 'status: new'
    
 🎁 make an enhancement, please describe the new functionality and why you believe it's useful.
    Please label with 'type: enhancement' and 'status: new'
 
If your pull request relates to any existing issues,
please reference them by using the issue number prefixed with #.

-->



Patch:
diff --git a/src/main/java/org/synyx/urlaubsverwaltung/infobanner/InfoBannerConfigProperties.java b/src/main/java/org/synyx/urlaubsverwaltung/infobanner/InfoBannerConfigProperties.java
index 9b487ece1..2239b4c4c 100644
--- a/src/main/java/org/synyx/urlaubsverwaltung/infobanner/InfoBannerConfigProperties.java
+++ b/src/main/java/org/synyx/urlaubsverwaltung/infobanner/InfoBannerConfigProperties.java
@@ -1,6 +1,5 @@
 package org.synyx.urlaubsverwaltung.infobanner;
 
-import jakarta.validation.Valid;
 import jakarta.validation.constraints.NotEmpty;
 import jakarta.validation.constraints.NotNull;
 import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -8,40 +7,8 @@ import org.springframework.validation.annotation.Validated;
 
 @Validated
 @ConfigurationProperties(prefix = "uv.info-banner")
-public class InfoBannerConfigProperties {
+public record InfoBannerConfigProperties(boolean enabled, @NotNull Text text) {
 
-    private boolean enabled;
-
-    @NotNull
-    @Valid
-    private Text text;
-
-    public boolean isEnabled() {
-        return enabled;
-    }
-
-    public void setEnabled(boolean enabled) {
-        this.enabled = enabled;
-    }
-
-    public Text getText() {
-        return text;
-    }
-
-    public void setText(Text text) {
-        this.text = text;
-    }
-
-    public static class Text {
-        @NotEmpty
-        private String de;
-
-        public String getDe() {
-            return de;
-        }
-
-        public void setDe(String de) {
-            this.de = de;
-        }
+    public record Text(@NotEmpty String de) {
     }
 }
diff --git a/src/main/java/org/synyx/urlaubsverwaltung/infobanner/InfoBannerControllerAdvice.java b/src/main/java/org/synyx/urlaubsverwaltung/infobanner/InfoBannerControllerAdvice.java
index e5bf46c1f..b3da12fb2 100644
--- a/src/main/java/org/synyx/urlaubsverwaltung/infobanner/InfoBannerControllerAdvice.java
+++ b/src/main/java/org/synyx/urlaubsverwaltung/infobanner/InfoBannerControllerAdvice.java
@@ -18,8 +18,13 @@ class InfoBannerControllerAdvice implements HandlerInterceptor {
     @Override
     public void postHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler, @Nullable ModelAndView modelAndView) throws Exception {
 
-        if (modelAndView != null && modelAndView.hasView()) {
-            modelAndView.getModelMap().addAttribute("infoBannerText", properties.getText().getDe());
+        if (modelAndView != null && modelAndView.hasView() && !redirectOrForward(modelAndView)) {
+            modelAndView.getModelMap().addAttribute("infoBannerText", properties.text().de());
         }
     }
+
+    private static boolean redirectOrForward(ModelAndView modelAndView) {
+        final String viewName = modelAndView.getViewName();
+        return viewName != null && (viewName.startsWith("redirect") || viewName.startsWith("forward"));
+    }
 }


Test patch:
diff --git a/src/test/java/org/synyx/urlaubsverwaltung/infobanner/InfoBannerControllerAdviceTest.java b/src/test/java/org/synyx/urlaubsverwaltung/infobanner/InfoBannerControllerAdviceTest.java
index f74baa5ea..03cd1e0e2 100644
--- a/src/test/java/org/synyx/urlaubsverwaltung/infobanner/InfoBannerControllerAdviceTest.java
+++ b/src/test/java/org/synyx/urlaubsverwaltung/infobanner/InfoBannerControllerAdviceTest.java
@@ -2,6 +2,8 @@ package org.synyx.urlaubsverwaltung.infobanner;
 
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 import org.springframework.web.servlet.ModelAndView;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -14,11 +16,8 @@ class InfoBannerControllerAdviceTest {
     @BeforeEach
     void setUp() {
 
-        final InfoBannerConfigProperties.Text text = new InfoBannerConfigProperties.Text();
-        text.setDe("info text");
-
-        final InfoBannerConfigProperties properties = new InfoBannerConfigProperties();
-        properties.setText(text);
+        final InfoBannerConfigProperties.Text infoText = new InfoBannerConfigProperties.Text("info text");
+        final InfoBannerConfigProperties properties = new InfoBannerConfigProperties(true, infoText);
 
         sut = new InfoBannerControllerAdvice(properties);
     }
@@ -36,6 +35,17 @@ class InfoBannerControllerAdviceTest {
         assertThat(modelAndView.getModel()).doesNotContainEntry("infoBannerText", "info text");
     }
 
+    @ParameterizedTest
+    @ValueSource(strings = {"redirect", "forward"})
+    void ensureModelAttributeIsNotSetWhenViewNameStartsWith(String prefix) throws Exception {
+
+        final ModelAndView modelAndView = new ModelAndView();
+        modelAndView.setViewName(prefix);
+
+        sut.postHandle(null, null, null, modelAndView);
+        assertThat(modelAndView.getModel()).doesNotContainEntry("infoBannerText", "info text");
+    }
+
     @Test
     void ensureModelAttribute() throws Exception {
         final ModelAndView modelAndView = new ModelAndView("any-viewname");