Skip to content

Accept empty filter dispatcher types in auto-configurations #22138

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

Closed
wants to merge 1 commit into from

Conversation

dreis2211
Copy link
Contributor

Hi,

this PR fixes #22128 by using

  • Collectors.toCollection(() -> EnumSet.noneOf(DispatcherType.class)) rather than
  • Collectors.collectingAndThen(Collectors.toSet(), EnumSet::copyOf)

to prevent exceptions when the underlying collection is empty.

Let me know what you think.
Cheers,
Christoph

Prior to this commit, the usage of EnumSet.copyOf resulted in exceptions when
the underlying collection was empty.

Closes spring-projectsgh-22128
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 27, 2020
Copy link
Member

@wilkinsona wilkinsona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much, @dreis2211. The approach looks good to me. I left a couple of comments about possibly using AssertJ’s extracting. Please let us know if you agree. If you do, we can tweak it when we merge or you could update the PR if you prefer.

DelegatingFilterProxyRegistrationBean bean = context.getBean("securityFilterChainRegistration",
DelegatingFilterProxyRegistrationBean.class);
@SuppressWarnings("unchecked")
EnumSet<DispatcherType> dispatcherTypes = (EnumSet<DispatcherType>) ReflectionTestUtils
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could use AssertJ’s extracting here and we prefer to do so where possible.

this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class)
.withPropertyValues("spring.session.servlet.filter-dispatcher-types=").run((context) -> {
FilterRegistrationBean<?> registration = context.getBean(FilterRegistrationBean.class);
Object dispatcherTypes = ReflectionTestUtils.getField(registration, "dispatcherTypes");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could use AssertJ’s extracting too.

@wilkinsona wilkinsona added for: merge-with-amendments Needs some changes when we merge type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 27, 2020
@wilkinsona wilkinsona added this to the 2.2.x milestone Jun 27, 2020
@dreis2211
Copy link
Contributor Author

I was undecided on that one actually. I wanted to stay consistent inside the specific tests, but I can change it of course.

@wilkinsona wilkinsona removed the for: merge-with-amendments Needs some changes when we merge label Jun 27, 2020
@wilkinsona
Copy link
Member

The dangers of reviewing a diff in isolation. Ok, let's keep it consistent within the test classes for now. We can do a separate pass that replaces the use of ReflectionTestUtils. hasFieldOrPropertyWithValue would probably be a better fit than my earlier suggestion of extracting anyway.

@dreis2211
Copy link
Contributor Author

dreis2211 commented Jun 27, 2020

Yeah, I think doing this in a separate pass is probably better. On top of that I think that neither hasFieldOrPropertyWithValue nor extracting work really great here. The former because it doesn't really play well with the empty case and the latter because it doesn't really work great with Sets (I only know of extracting().asList() at least and a quick test didn't reveal any other cool shortcut).

@wilkinsona wilkinsona self-assigned this Jun 30, 2020
wilkinsona pushed a commit that referenced this pull request Jun 30, 2020
Prior to this commit, the usage of EnumSet.copyOf resulted in exceptions when
the underlying collection was empty.

See gh-22138
@wilkinsona
Copy link
Member

Thanks very much once again, @dreis2211.

@wilkinsona wilkinsona modified the milestones: 2.2.x, 2.2.9 Jun 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants