Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

htmx:redirect:/path and new HtmxDirectView("/path") have different behaviour regarding RedirectAttributes #144

Closed
wimdeblauwe opened this issue Nov 17, 2024 · 6 comments · Fixed by #146
Milestone

Comments

@wimdeblauwe
Copy link
Owner

I am writing a blog post on redirect attributes and I was testing this with the new 3.6.0 release. What I have discovered is that there is a difference when using htmx:redirect:/path versus new HtmxDirectView("/path"). The flash attributes are not present after the redirect in the first case, but they do work fine when using new HtmxDirectView("/path").

@wimdeblauwe
Copy link
Owner Author

I tried to investigate how to fix it, but I don't quite understand the code at HtmxResponseHandlerMethodArgumentResolver. Should supportsParameter also check for String.class? But what should happen inside resolveArgument when it is a String?

@xhaggi
Copy link
Collaborator

xhaggi commented Nov 19, 2024

There should be, or rather, there is no difference between string-based and view-based redirects. BTW the HtmxResponseHandlerMethodArgumentResolver is not involved in view resolution such as HtmxRedirectView. Instead, this is done by Spring's view resolver implementation, since HtmxRedirectView is a subclass of RedirectView. So we don't do anything special to get it to work. The only difference between the two is that HtmxRedirectView is created by HtmxViewResolver when you use string-based redirection.

To be really sure, I have created two new tests for this in #145.

@xhaggi
Copy link
Collaborator

xhaggi commented Nov 19, 2024

Mhhh you're right @wimdeblauwe. The tests in #145 fail. Didn't check the result before I added my comment 😄 Let me check what's going on here.

@xhaggi
Copy link
Collaborator

xhaggi commented Nov 19, 2024

Bad news. It won't work for string-based redirects because we need to set mavContainer.setRedirectModelScenario(true) on the ModelAndViewContainer, but in this case we do not have access to it, nor we are able to override anything. If a string-based return value is used, Spring's ViewNameMethodReturnValueHandler kicks in before any custom HandlerMethodReturnValueHandler is called, and you cannot change this order or override anything to change it (see RequestMappingHandlerAdapter.java#L770

Of course, this is anything but nice. So what should we do? Point this out in the README or remove support for string-based redirects.

@xhaggi
Copy link
Collaborator

xhaggi commented Nov 19, 2024

Another option would be to change the prefix from htmx:redirect:/path and htmx:location:/path to redirect:htmx:/path, redirect:htmx:location:/path. I have closed #145 in favor of #146, which fixes the problem.

@wimdeblauwe
Copy link
Owner Author

Thanks for investigating this. Changing the prefix seems the best option. I tried it in my example with the changes you did in the PR and it works fine. I'll do a 3.6.1 release with the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants