wimdeblauwe / htmx-spring-boot

Spring Boot and Thymeleaf helpers for working with htmx
Apache License 2.0
436 stars 41 forks source link

adding multiple triggers causes exception #57

Closed yennor closed 1 month ago

yennor commented 12 months ago

If you add multiple triggers to a HtmxResponse, like:

        htmxResponse.addTrigger("hyperlinkChanged");
        htmxResponse.addTrigger("hideModal");

Spring will throw a IllegalArgumentException (Spring boot 3.1). As it seems a cr/lf is added instead of a coma to separate the different triggers:

java.lang.IllegalArgumentException: Invalid characters (CR/LF) in header HX-Trigger
    at org.springframework.util.Assert.isTrue(Assert.java:140) ~[spring-core-6.0.11.jar:6.0.11]
    at org.springframework.security.web.firewall.FirewalledResponse.validateCrlf(FirewalledResponse.java:76) ~[spring-security-web-6.1.2.jar:6.1.2]
    at org.springframework.security.web.firewall.FirewalledResponse.setHeader(FirewalledResponse.java:53) ~[spring-security-web-6.1.2.jar:6.1.2]
    at jakarta.servlet.http.HttpServletResponseWrapper.setHeader(HttpServletResponseWrapper.java:132) ~[tomcat-embed-core-10.1.11.jar:6.0]
    at jakarta.servlet.http.HttpServletResponseWrapper.setHeader(HttpServletResponseWrapper.java:132) ~[tomcat-embed-core-10.1.11.jar:6.0]
    at io.github.wimdeblauwe.hsbt.mvc.HtmxViewHandlerInterceptor.setTriggerHeader(HtmxViewHandlerInterceptor.java:120) ~[htmx-spring-boot-thymeleaf-2.1.0.jar:2.1.0]
    at io.github.wimdeblauwe.hsbt.mvc.HtmxViewHandlerInterceptor.addHxHeaders(HtmxViewHandlerInterceptor.java:85) ~[htmx-spring-boot-thymeleaf-2.1.0.jar:2.1.0]
    at io.github.wimdeblauwe.hsbt.mvc.HtmxViewHandlerInterceptor.postHandle(HtmxViewHandlerInterceptor.java:81) ~[htmx-spring-boot-thymeleaf-2.1.0.jar:2.1.0]

edit: i've realised I was one version behind. Just changed the stracktrace.

wimdeblauwe commented 11 months ago

I am unable to reproduce this. Could you put a sample project somewhere that shows the issue? Or paste the code of a test here that shows the issue?

wimdeblauwe commented 10 months ago

Since there has been no update, I am closing this issue. Feel free to re-open if you have more info, or even better a small reproducer.

yennor commented 1 month ago

I've just stumpled over the same issue again.

HtmxResponse.builder()
                .trigger("showToast", "Die Einträge wurden erfolgreich gelöscht.")

This time I went a bit deeper. The problem is the spring boot config option which I use in the development profile:

spring:
  jackson:
    serialization:
      indent-output: true

Since HtmxResponseHandlerMethodReturnValueHandler injects the default objectMapper to serialize the values to json, the json output will be formatted like that. Propably it would be better to create a own objectMapper to not depend on this or any other configuration changes the developper does to the default mapper.

checketts commented 1 month ago

Thanks for finding that! I hit the similar issue and with your input that explains the root cause perfectly

xhaggi commented 1 month ago

@yennor thanks for pointing us in the right direction. I have pushed a fix for it.

wimdeblauwe commented 1 month ago

Released as 3.4.1