From 1833aafc3f7e22a4e418eb2776a6722335e617cb Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 1 May 2024 15:41:26 +0200 Subject: [PATCH] Ignore non-String keys in PropertiesPropertySource.getPropertyNames() Closes gh-32742 (cherry picked from commit 610626aec69dd8a932dcc2cbee5642d2dcccc3bc) --- .../core/env/PropertiesPropertySource.java | 4 ++-- .../core/env/StandardEnvironmentTests.java | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/env/PropertiesPropertySource.java b/spring-core/src/main/java/org/springframework/core/env/PropertiesPropertySource.java index d09c3683510..9741c581928 100644 --- a/spring-core/src/main/java/org/springframework/core/env/PropertiesPropertySource.java +++ b/spring-core/src/main/java/org/springframework/core/env/PropertiesPropertySource.java @@ -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 { @Override public String[] getPropertyNames() { synchronized (this.source) { - return super.getPropertyNames(); + return ((Map) this.source).keySet().stream().filter(k -> k instanceof String).toArray(String[]::new); } } diff --git a/spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java b/spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java index 80f12bea560..6bd889e5121 100644 --- a/spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java +++ b/spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java @@ -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; 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 { // 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 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 { EnvironmentTestUtils.getModifiableSystemEnvironment().remove(DISALLOWED_PROPERTY_NAME); } + @Nested class GetActiveProfiles { @@ -456,6 +466,7 @@ class StandardEnvironmentTests { } } + @Nested class AcceptsProfilesTests { @@ -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 { assertThat(environment.matchesProfiles("p2 & (foo | p1)")).isTrue(); assertThat(environment.matchesProfiles("foo", "(p2 & p1)")).isTrue(); } - } }