Skip to content

Commit 610626a

Browse files
committed
Ignore non-String keys in PropertiesPropertySource.getPropertyNames()
Closes gh-32742
1 parent d8afe7a commit 610626a

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

Diff for: 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

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
package org.springframework.core.env;
1818

19+
import java.util.HashSet;
1920
import java.util.Map;
21+
import java.util.Set;
2022

2123
import org.junit.jupiter.api.Nested;
2224
import org.junit.jupiter.api.Test;
@@ -300,6 +302,12 @@ void getSystemProperties() {
300302
assertThat(systemProperties.get(DISALLOWED_PROPERTY_NAME)).isEqualTo(DISALLOWED_PROPERTY_VALUE);
301303
assertThat(systemProperties.get(STRING_PROPERTY_NAME)).isEqualTo(NON_STRING_PROPERTY_VALUE);
302304
assertThat(systemProperties.get(NON_STRING_PROPERTY_NAME)).isEqualTo(STRING_PROPERTY_VALUE);
305+
306+
PropertiesPropertySource systemPropertySource = (PropertiesPropertySource)
307+
environment.getPropertySources().get(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
308+
Set<String> expectedKeys = new HashSet<>(System.getProperties().stringPropertyNames());
309+
expectedKeys.add(STRING_PROPERTY_NAME); // filtered out by stringPropertyNames due to non-String value
310+
assertThat(Set.of(systemPropertySource.getPropertyNames())).isEqualTo(expectedKeys);
303311
}
304312
finally {
305313
System.clearProperty(ALLOWED_PROPERTY_NAME);
@@ -316,6 +324,7 @@ void getSystemEnvironment() {
316324
assertThat(System.getenv()).isSameAs(systemEnvironment);
317325
}
318326

327+
319328
@Nested
320329
class GetActiveProfiles {
321330

@@ -365,6 +374,7 @@ void fromSystemProperties_withMultipleProfiles_withWhitespace() {
365374
}
366375
}
367376

377+
368378
@Nested
369379
class AcceptsProfilesTests {
370380

@@ -447,9 +457,9 @@ void withProfileExpression() {
447457
environment.addActiveProfile("p2");
448458
assertThat(environment.acceptsProfiles(Profiles.of("p1 & p2"))).isTrue();
449459
}
450-
451460
}
452461

462+
453463
@Nested
454464
class MatchesProfilesTests {
455465

@@ -549,7 +559,6 @@ void withProfileExpressions() {
549559
assertThat(environment.matchesProfiles("p2 & (foo | p1)")).isTrue();
550560
assertThat(environment.matchesProfiles("foo", "(p2 & p1)")).isTrue();
551561
}
552-
553562
}
554563

555564
}

0 commit comments

Comments
 (0)