Use noNullElements
Collection#contains(null) does not work for all collection types Closes gh-10703
This commit is contained in:
parent
3c45d46bd7
commit
aaaf7d3523
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2017 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -66,7 +66,7 @@ public class MediaTypeServerWebExchangeMatcher implements ServerWebExchangeMatch
|
||||||
*/
|
*/
|
||||||
public MediaTypeServerWebExchangeMatcher(Collection<MediaType> matchingMediaTypes) {
|
public MediaTypeServerWebExchangeMatcher(Collection<MediaType> matchingMediaTypes) {
|
||||||
Assert.notEmpty(matchingMediaTypes, "matchingMediaTypes cannot be null");
|
Assert.notEmpty(matchingMediaTypes, "matchingMediaTypes cannot be null");
|
||||||
Assert.isTrue(!matchingMediaTypes.contains(null),
|
Assert.noNullElements(matchingMediaTypes,
|
||||||
() -> "matchingMediaTypes cannot contain null. Got " + matchingMediaTypes);
|
() -> "matchingMediaTypes cannot contain null. Got " + matchingMediaTypes);
|
||||||
this.matchingMediaTypes = matchingMediaTypes;
|
this.matchingMediaTypes = matchingMediaTypes;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2016 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -45,7 +45,7 @@ public final class AndRequestMatcher implements RequestMatcher {
|
||||||
*/
|
*/
|
||||||
public AndRequestMatcher(List<RequestMatcher> requestMatchers) {
|
public AndRequestMatcher(List<RequestMatcher> requestMatchers) {
|
||||||
Assert.notEmpty(requestMatchers, "requestMatchers must contain a value");
|
Assert.notEmpty(requestMatchers, "requestMatchers must contain a value");
|
||||||
Assert.isTrue(!requestMatchers.contains(null), "requestMatchers cannot contain null values");
|
Assert.noNullElements(requestMatchers, "requestMatchers cannot contain null values");
|
||||||
this.requestMatchers = requestMatchers;
|
this.requestMatchers = requestMatchers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2016 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -40,7 +40,7 @@ public final class OrRequestMatcher implements RequestMatcher {
|
||||||
*/
|
*/
|
||||||
public OrRequestMatcher(List<RequestMatcher> requestMatchers) {
|
public OrRequestMatcher(List<RequestMatcher> requestMatchers) {
|
||||||
Assert.notEmpty(requestMatchers, "requestMatchers must contain a value");
|
Assert.notEmpty(requestMatchers, "requestMatchers must contain a value");
|
||||||
Assert.isTrue(!requestMatchers.contains(null), "requestMatchers cannot contain null values");
|
Assert.noNullElements(requestMatchers, "requestMatchers cannot contain null values");
|
||||||
this.requestMatchers = requestMatchers;
|
this.requestMatchers = requestMatchers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2017 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
package org.springframework.security.web.server.util.matcher;
|
package org.springframework.security.web.server.util.matcher;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -43,6 +45,21 @@ public class MediaTypeServerWebExchangeMatcherTests {
|
||||||
assertThatIllegalArgumentException().isThrownBy(() -> new MediaTypeServerWebExchangeMatcher(types));
|
assertThatIllegalArgumentException().isThrownBy(() -> new MediaTypeServerWebExchangeMatcher(types));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// gh-10703
|
||||||
|
@Test
|
||||||
|
public void constructorListOfDoesNotThrowNullPointerException() {
|
||||||
|
List<MediaType> mediaTypes = new ArrayList<MediaType>(Arrays.asList(MediaType.ALL)) {
|
||||||
|
@Override
|
||||||
|
public boolean contains(Object o) {
|
||||||
|
if (o == null) {
|
||||||
|
throw new NullPointerException();
|
||||||
|
}
|
||||||
|
return super.contains(o);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
new MediaTypeServerWebExchangeMatcher(mediaTypes);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void constructorMediaTypeArrayWhenContainsNullThenThrowsIllegalArgumentException() {
|
public void constructorMediaTypeArrayWhenContainsNullThenThrowsIllegalArgumentException() {
|
||||||
MediaType[] types = { null };
|
MediaType[] types = { null };
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2016 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.security.web.util.matcher;
|
package org.springframework.security.web.util.matcher;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -55,6 +56,22 @@ public class AndRequestMatcherTests {
|
||||||
assertThatNullPointerException().isThrownBy(() -> new AndRequestMatcher((RequestMatcher[]) null));
|
assertThatNullPointerException().isThrownBy(() -> new AndRequestMatcher((RequestMatcher[]) null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// gh-10703
|
||||||
|
@Test
|
||||||
|
public void constructorListOfDoesNotThrowNullPointer() {
|
||||||
|
List<RequestMatcher> requestMatchers = new ArrayList<RequestMatcher>(
|
||||||
|
Arrays.asList(AnyRequestMatcher.INSTANCE)) {
|
||||||
|
@Override
|
||||||
|
public boolean contains(Object o) {
|
||||||
|
if (o == null) {
|
||||||
|
throw new NullPointerException();
|
||||||
|
}
|
||||||
|
return super.contains(o);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
new AndRequestMatcher(requestMatchers);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void constructorArrayContainsNull() {
|
public void constructorArrayContainsNull() {
|
||||||
assertThatIllegalArgumentException().isThrownBy(() -> new AndRequestMatcher((RequestMatcher) null));
|
assertThatIllegalArgumentException().isThrownBy(() -> new AndRequestMatcher((RequestMatcher) null));
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2016 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.security.web.util.matcher;
|
package org.springframework.security.web.util.matcher;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -55,6 +56,23 @@ public class OrRequestMatcherTests {
|
||||||
assertThatNullPointerException().isThrownBy(() -> new OrRequestMatcher((RequestMatcher[]) null));
|
assertThatNullPointerException().isThrownBy(() -> new OrRequestMatcher((RequestMatcher[]) null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// gh-10703
|
||||||
|
@Test
|
||||||
|
public void constructorListOfDoesNotThrowNullPointer() {
|
||||||
|
// emulate List.of for pre-JDK 9 builds
|
||||||
|
List<RequestMatcher> requestMatchers = new ArrayList<RequestMatcher>(
|
||||||
|
Arrays.asList(AnyRequestMatcher.INSTANCE)) {
|
||||||
|
@Override
|
||||||
|
public boolean contains(Object o) {
|
||||||
|
if (o == null) {
|
||||||
|
throw new NullPointerException();
|
||||||
|
}
|
||||||
|
return super.contains(o);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
new OrRequestMatcher(requestMatchers);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void constructorArrayContainsNull() {
|
public void constructorArrayContainsNull() {
|
||||||
assertThatIllegalArgumentException().isThrownBy(() -> new OrRequestMatcher((RequestMatcher) null));
|
assertThatIllegalArgumentException().isThrownBy(() -> new OrRequestMatcher((RequestMatcher) null));
|
||||||
|
|
Loading…
Reference in New Issue