Ignore non-String keys in PropertiesPropertySource.getPropertyNames()
Closes gh-32742
(cherry picked from commit 610626aec6)
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2019 the original author or authors.
|
* Copyright 2002-2024 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.
|
||||||
@@ -48,7 +48,7 @@ public class PropertiesPropertySource extends MapPropertySource {
|
|||||||
@Override
|
@Override
|
||||||
public String[] getPropertyNames() {
|
public String[] getPropertyNames() {
|
||||||
synchronized (this.source) {
|
synchronized (this.source) {
|
||||||
return super.getPropertyNames();
|
return ((Map<?, ?>) this.source).keySet().stream().filter(k -> k instanceof String).toArray(String[]::new);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2023 the original author or authors.
|
* Copyright 2002-2024 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.
|
||||||
@@ -18,7 +18,10 @@ package org.springframework.core.env;
|
|||||||
|
|
||||||
import java.security.AccessControlException;
|
import java.security.AccessControlException;
|
||||||
import java.security.Permission;
|
import java.security.Permission;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Nested;
|
import org.junit.jupiter.api.Nested;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -307,6 +310,12 @@ class StandardEnvironmentTests {
|
|||||||
// non-string keys and values work fine... until the security manager is introduced below
|
// non-string keys and values work fine... until the security manager is introduced below
|
||||||
assertThat(systemProperties.get(STRING_PROPERTY_NAME)).isEqualTo(NON_STRING_PROPERTY_VALUE);
|
assertThat(systemProperties.get(STRING_PROPERTY_NAME)).isEqualTo(NON_STRING_PROPERTY_VALUE);
|
||||||
assertThat(systemProperties.get(NON_STRING_PROPERTY_NAME)).isEqualTo(STRING_PROPERTY_VALUE);
|
assertThat(systemProperties.get(NON_STRING_PROPERTY_NAME)).isEqualTo(STRING_PROPERTY_VALUE);
|
||||||
|
|
||||||
|
PropertiesPropertySource systemPropertySource = (PropertiesPropertySource)
|
||||||
|
environment.getPropertySources().get(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||||
|
Set<String> expectedKeys = new HashSet<>(System.getProperties().stringPropertyNames());
|
||||||
|
expectedKeys.add(STRING_PROPERTY_NAME); // filtered out by stringPropertyNames due to non-String value
|
||||||
|
assertThat(new HashSet<>(Arrays.asList(systemPropertySource.getPropertyNames()))).isEqualTo(expectedKeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
SecurityManager securityManager = new SecurityManager() {
|
SecurityManager securityManager = new SecurityManager() {
|
||||||
@@ -407,6 +416,7 @@ class StandardEnvironmentTests {
|
|||||||
EnvironmentTestUtils.getModifiableSystemEnvironment().remove(DISALLOWED_PROPERTY_NAME);
|
EnvironmentTestUtils.getModifiableSystemEnvironment().remove(DISALLOWED_PROPERTY_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
class GetActiveProfiles {
|
class GetActiveProfiles {
|
||||||
|
|
||||||
@@ -456,6 +466,7 @@ class StandardEnvironmentTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
class AcceptsProfilesTests {
|
class AcceptsProfilesTests {
|
||||||
|
|
||||||
@@ -538,9 +549,9 @@ class StandardEnvironmentTests {
|
|||||||
environment.addActiveProfile("p2");
|
environment.addActiveProfile("p2");
|
||||||
assertThat(environment.acceptsProfiles(Profiles.of("p1 & p2"))).isTrue();
|
assertThat(environment.acceptsProfiles(Profiles.of("p1 & p2"))).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
class MatchesProfilesTests {
|
class MatchesProfilesTests {
|
||||||
|
|
||||||
@@ -650,7 +661,6 @@ class StandardEnvironmentTests {
|
|||||||
assertThat(environment.matchesProfiles("p2 & (foo | p1)")).isTrue();
|
assertThat(environment.matchesProfiles("p2 & (foo | p1)")).isTrue();
|
||||||
assertThat(environment.matchesProfiles("foo", "(p2 & p1)")).isTrue();
|
assertThat(environment.matchesProfiles("foo", "(p2 & p1)")).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user