Skip to content

Commit 1833aaf

Browse files
committed
Ignore non-String keys in PropertiesPropertySource.getPropertyNames()
Closes gh-32742 (cherry picked from commit 610626a)
1 parent b8bc780 commit 1833aaf

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

spring-core/src/main/java/org/springframework/core/env/PropertiesPropertySource.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -48,7 +48,7 @@ protected PropertiesPropertySource(String name, Map<String, Object> source) {
4848
@Override
4949
public String[] getPropertyNames() {
5050
synchronized (this.source) {
51-
return super.getPropertyNames();
51+
return ((Map<?, ?>) this.source).keySet().stream().filter(k -> k instanceof String).toArray(String[]::new);
5252
}
5353
}
5454

spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -18,7 +18,10 @@
1818

1919
import java.security.AccessControlException;
2020
import java.security.Permission;
21+
import java.util.Arrays;
22+
import java.util.HashSet;
2123
import java.util.Map;
24+
import java.util.Set;
2225

2326
import org.junit.jupiter.api.Nested;
2427
import org.junit.jupiter.api.Test;
@@ -307,6 +310,12 @@ void getSystemProperties_withAndWithoutSecurityManager() {
307310
// non-string keys and values work fine... until the security manager is introduced below
308311
assertThat(systemProperties.get(STRING_PROPERTY_NAME)).isEqualTo(NON_STRING_PROPERTY_VALUE);
309312
assertThat(systemProperties.get(NON_STRING_PROPERTY_NAME)).isEqualTo(STRING_PROPERTY_VALUE);
313+
314+
PropertiesPropertySource systemPropertySource = (PropertiesPropertySource)
315+
environment.getPropertySources().get(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
316+
Set<String> expectedKeys = new HashSet<>(System.getProperties().stringPropertyNames());
317+
expectedKeys.add(STRING_PROPERTY_NAME); // filtered out by stringPropertyNames due to non-String value
318+
assertThat(new HashSet<>(Arrays.asList(systemPropertySource.getPropertyNames()))).isEqualTo(expectedKeys);
310319
}
311320

312321
SecurityManager securityManager = new SecurityManager() {
@@ -407,6 +416,7 @@ public void checkPermission(Permission perm) {
407416
EnvironmentTestUtils.getModifiableSystemEnvironment().remove(DISALLOWED_PROPERTY_NAME);
408417
}
409418

419+
410420
@Nested
411421
class GetActiveProfiles {
412422

@@ -456,6 +466,7 @@ void fromSystemProperties_withMultipleProfiles_withWhitespace() {
456466
}
457467
}
458468

469+
459470
@Nested
460471
class AcceptsProfilesTests {
461472

@@ -538,9 +549,9 @@ void withProfileExpression() {
538549
environment.addActiveProfile("p2");
539550
assertThat(environment.acceptsProfiles(Profiles.of("p1 & p2"))).isTrue();
540551
}
541-
542552
}
543553

554+
544555
@Nested
545556
class MatchesProfilesTests {
546557

@@ -650,7 +661,6 @@ void withProfileExpressions() {
650661
assertThat(environment.matchesProfiles("p2 & (foo | p1)")).isTrue();
651662
assertThat(environment.matchesProfiles("foo", "(p2 & p1)")).isTrue();
652663
}
653-
654664
}
655665

656666
}

0 commit comments

Comments
 (0)