Merge pull request #2979 from cainj13/localeNpeFix
make locale retrieval null-safe
This commit is contained in:
commit
fa312fb3db
2 changed files with 38 additions and 13 deletions
|
@ -135,6 +135,7 @@ public class LocaleHelper {
|
||||||
|
|
||||||
private static Locale findLocale(Set<String> supportedLocales, String... localeStrings) {
|
private static Locale findLocale(Set<String> supportedLocales, String... localeStrings) {
|
||||||
for (String localeString : localeStrings) {
|
for (String localeString : localeStrings) {
|
||||||
|
if (localeString != null) {
|
||||||
Locale result = null;
|
Locale result = null;
|
||||||
Locale search = Locale.forLanguageTag(localeString);
|
Locale search = Locale.forLanguageTag(localeString);
|
||||||
for (String languageTag : supportedLocales) {
|
for (String languageTag : supportedLocales) {
|
||||||
|
@ -152,6 +153,7 @@ public class LocaleHelper {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
package org.keycloak.services.util;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.core.IsNull.nullValue;
|
||||||
|
|
||||||
|
public class LocaleHelperTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotExceptionOnNullLocaleAttributeItem() throws Exception {
|
||||||
|
final Method method = LocaleHelper.class.getDeclaredMethod("findLocale", Set.class, String[].class);
|
||||||
|
method.setAccessible(true);
|
||||||
|
Locale foundLocale = (Locale) method.invoke(null, Stream.of("en", "es", "fr").collect(Collectors.toSet()), new String[]{null});
|
||||||
|
assertThat(foundLocale, nullValue());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue