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

In MockMvc tests all filters end up with empty-string filterName, breaking OncePerRequestFilter #31474

Closed
jeffbswope opened this issue Oct 22, 2023 · 1 comment
Assignees
Labels
in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@jeffbswope
Copy link

jeffbswope commented Oct 22, 2023

As discovered in spring-boot:3.2.0-RC1 (previous milestone unaffected) changes to populate init parameters and dispatch types in MockMvc auto-configuration have led to all filters (or those without explicit registration?) ending up with empty-string as their getFilterName. This ultimately breaks the OncePerRequestFilter behavior because all filters use .FILTERED as their request attribute so only the first one runs.

The stack here I believe is something like:

org.springframework.boot.test.autoconfigure.web.servlet.SpringBootMockMvcBuilderCustomizer#addFilter
org.springframework.test.web.servlet.setup.AbstractMockMvcBuilder#addFilter(jakarta.servlet.Filter, java.util.Map<java.lang.String,java.lang.String>, java.util.EnumSet<jakarta.servlet.DispatcherType>, java.lang.String...)

So we end up with MockMvcFilterDecorator even though we aren't doing any filtering/otherwise -- this is change from last milestone.

Then in MockMvcFilterDecorator#initIfRequired we create a MockFilterConfig which always has an empty-string filterName.

@wilkinsona had suggestions over here about different approaches to harmonizing this between framework and boot:
spring-projects/spring-boot#37835 (comment)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 22, 2023
@snicoll snicoll added in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 23, 2023
@snicoll snicoll added this to the 6.1.0-RC2 milestone Oct 23, 2023
@edwardsre
Copy link

edwardsre commented Oct 23, 2023

We are experiencing this regression also. I have been able to work around it temporarily by overriding getFilterName() in our once per request filter InvocationContextFilter.

  @Override
  protected String getFilterName() {
    return Optional.ofNullable(super.getFilterName())
      .filter(StringUtils::hasText)
      .orElseGet(InvocationContextFilter.class::getSimpleName);
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

5 participants