Browse Source

Ignore non-String keys in PropertiesPropertySource.getPropertyNames()

Closes gh-32742

(cherry picked from commit 610626aec6)
pull/32754/head
Juergen Hoeller 2 years ago
parent
commit
1833aafc3f
  1. 4
      spring-core/src/main/java/org/springframework/core/env/PropertiesPropertySource.java
  2. 16
      spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java

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

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -48,7 +48,7 @@ public class PropertiesPropertySource extends MapPropertySource { @@ -48,7 +48,7 @@ public class PropertiesPropertySource extends MapPropertySource {
@Override
public String[] getPropertyNames() {
synchronized (this.source) {
return super.getPropertyNames();
return ((Map<?, ?>) this.source).keySet().stream().filter(k -> k instanceof String).toArray(String[]::new);
}
}

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

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -18,7 +18,10 @@ package org.springframework.core.env; @@ -18,7 +18,10 @@ package org.springframework.core.env;
import java.security.AccessControlException;
import java.security.Permission;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@ -307,6 +310,12 @@ class StandardEnvironmentTests { @@ -307,6 +310,12 @@ class StandardEnvironmentTests {
// 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(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() {
@ -407,6 +416,7 @@ class StandardEnvironmentTests { @@ -407,6 +416,7 @@ class StandardEnvironmentTests {
EnvironmentTestUtils.getModifiableSystemEnvironment().remove(DISALLOWED_PROPERTY_NAME);
}
@Nested
class GetActiveProfiles {
@ -456,6 +466,7 @@ class StandardEnvironmentTests { @@ -456,6 +466,7 @@ class StandardEnvironmentTests {
}
}
@Nested
class AcceptsProfilesTests {
@ -538,9 +549,9 @@ class StandardEnvironmentTests { @@ -538,9 +549,9 @@ class StandardEnvironmentTests {
environment.addActiveProfile("p2");
assertThat(environment.acceptsProfiles(Profiles.of("p1 & p2"))).isTrue();
}
}
@Nested
class MatchesProfilesTests {
@ -650,7 +661,6 @@ class StandardEnvironmentTests { @@ -650,7 +661,6 @@ class StandardEnvironmentTests {
assertThat(environment.matchesProfiles("p2 & (foo | p1)")).isTrue();
assertThat(environment.matchesProfiles("foo", "(p2 & p1)")).isTrue();
}
}
}

Loading…
Cancel
Save