Skip to content

Commit ca353d6

Browse files
committed
Use noNullElements
Collection#contains(null) does not work for all collection types Closes gh-10703
1 parent a763382 commit ca353d6

File tree

6 files changed

+61
-9
lines changed

6 files changed

+61
-9
lines changed

web/src/main/java/org/springframework/security/web/server/util/matcher/MediaTypeServerWebExchangeMatcher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,7 +66,7 @@ public MediaTypeServerWebExchangeMatcher(MediaType... matchingMediaTypes) {
6666
*/
6767
public MediaTypeServerWebExchangeMatcher(Collection<MediaType> matchingMediaTypes) {
6868
Assert.notEmpty(matchingMediaTypes, "matchingMediaTypes cannot be null");
69-
Assert.isTrue(!matchingMediaTypes.contains(null),
69+
Assert.noNullElements(matchingMediaTypes,
7070
() -> "matchingMediaTypes cannot contain null. Got " + matchingMediaTypes);
7171
this.matchingMediaTypes = matchingMediaTypes;
7272
}

web/src/main/java/org/springframework/security/web/util/matcher/AndRequestMatcher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,7 +45,7 @@ public final class AndRequestMatcher implements RequestMatcher {
4545
*/
4646
public AndRequestMatcher(List<RequestMatcher> requestMatchers) {
4747
Assert.notEmpty(requestMatchers, "requestMatchers must contain a value");
48-
Assert.isTrue(!requestMatchers.contains(null), "requestMatchers cannot contain null values");
48+
Assert.noNullElements(requestMatchers, "requestMatchers cannot contain null values");
4949
this.requestMatchers = requestMatchers;
5050
}
5151

web/src/main/java/org/springframework/security/web/util/matcher/OrRequestMatcher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,7 +40,7 @@ public final class OrRequestMatcher implements RequestMatcher {
4040
*/
4141
public OrRequestMatcher(List<RequestMatcher> requestMatchers) {
4242
Assert.notEmpty(requestMatchers, "requestMatchers must contain a value");
43-
Assert.isTrue(!requestMatchers.contains(null), "requestMatchers cannot contain null values");
43+
Assert.noNullElements(requestMatchers, "requestMatchers cannot contain null values");
4444
this.requestMatchers = requestMatchers;
4545
}
4646

web/src/test/java/org/springframework/security/web/server/util/matcher/MediaTypeServerWebExchangeMatcherTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.security.web.server.util.matcher;
1818

19+
import java.util.ArrayList;
20+
import java.util.Arrays;
1921
import java.util.Collections;
2022
import java.util.List;
2123

@@ -43,6 +45,21 @@ public void constructorMediaTypeArrayWhenNullThenThrowsIllegalArgumentException(
4345
assertThatIllegalArgumentException().isThrownBy(() -> new MediaTypeServerWebExchangeMatcher(types));
4446
}
4547

48+
// gh-10703
49+
@Test
50+
public void constructorListOfDoesNotThrowNullPointerException() {
51+
List<MediaType> mediaTypes = new ArrayList<MediaType>(Arrays.asList(MediaType.ALL)) {
52+
@Override
53+
public boolean contains(Object o) {
54+
if (o == null) {
55+
throw new NullPointerException();
56+
}
57+
return super.contains(o);
58+
}
59+
};
60+
new MediaTypeServerWebExchangeMatcher(mediaTypes);
61+
}
62+
4663
@Test
4764
public void constructorMediaTypeArrayWhenContainsNullThenThrowsIllegalArgumentException() {
4865
MediaType[] types = { null };

web/src/test/java/org/springframework/security/web/util/matcher/AndRequestMatcherTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.security.web.util.matcher;
1818

19+
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.Collections;
2122
import java.util.List;
@@ -55,6 +56,22 @@ public void constructorNullArray() {
5556
assertThatNullPointerException().isThrownBy(() -> new AndRequestMatcher((RequestMatcher[]) null));
5657
}
5758

59+
// gh-10703
60+
@Test
61+
public void constructorListOfDoesNotThrowNullPointer() {
62+
List<RequestMatcher> requestMatchers = new ArrayList<RequestMatcher>(
63+
Arrays.asList(AnyRequestMatcher.INSTANCE)) {
64+
@Override
65+
public boolean contains(Object o) {
66+
if (o == null) {
67+
throw new NullPointerException();
68+
}
69+
return super.contains(o);
70+
}
71+
};
72+
new AndRequestMatcher(requestMatchers);
73+
}
74+
5875
@Test
5976
public void constructorArrayContainsNull() {
6077
assertThatIllegalArgumentException().isThrownBy(() -> new AndRequestMatcher((RequestMatcher) null));

web/src/test/java/org/springframework/security/web/util/matcher/OrRequestMatcherTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.security.web.util.matcher;
1818

19+
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.Collections;
2122
import java.util.List;
@@ -55,6 +56,23 @@ public void constructorNullArray() {
5556
assertThatNullPointerException().isThrownBy(() -> new OrRequestMatcher((RequestMatcher[]) null));
5657
}
5758

59+
// gh-10703
60+
@Test
61+
public void constructorListOfDoesNotThrowNullPointer() {
62+
// emulate List.of for pre-JDK 9 builds
63+
List<RequestMatcher> requestMatchers = new ArrayList<RequestMatcher>(
64+
Arrays.asList(AnyRequestMatcher.INSTANCE)) {
65+
@Override
66+
public boolean contains(Object o) {
67+
if (o == null) {
68+
throw new NullPointerException();
69+
}
70+
return super.contains(o);
71+
}
72+
};
73+
new OrRequestMatcher(requestMatchers);
74+
}
75+
5876
@Test
5977
public void constructorArrayContainsNull() {
6078
assertThatIllegalArgumentException().isThrownBy(() -> new OrRequestMatcher((RequestMatcher) null));

0 commit comments

Comments
 (0)