Browse Source

Merge branch '3.4.x'

Closes gh-45549
pull/45554/head
Phillip Webb 9 months ago
parent
commit
e0a23e5e38
  1. 16
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributor.java
  2. 26
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/TestApplicationEnvironment.java
  3. 16
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java

16
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributor.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 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.
@ -29,6 +29,7 @@ import java.util.stream.StreamSupport; @@ -29,6 +29,7 @@ import java.util.stream.StreamSupport;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.bind.PlaceholdersResolver;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.origin.OriginLookup;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
@ -414,7 +415,7 @@ class ConfigDataEnvironmentContributor implements Iterable<ConfigDataEnvironment @@ -414,7 +415,7 @@ class ConfigDataEnvironmentContributor implements Iterable<ConfigDataEnvironment
static ConfigDataEnvironmentContributor ofExisting(PropertySource<?> propertySource,
ConversionService conversionService) {
return new ConfigDataEnvironmentContributor(Kind.EXISTING, null, null, false, propertySource,
ConfigurationPropertySource.from(propertySource), null, null, null, conversionService);
asConfigurationPropertySource(propertySource), null, null, null, conversionService);
}
/**
@ -434,9 +435,16 @@ class ConfigDataEnvironmentContributor implements Iterable<ConfigDataEnvironment @@ -434,9 +435,16 @@ class ConfigDataEnvironmentContributor implements Iterable<ConfigDataEnvironment
ConversionService conversionService) {
PropertySource<?> propertySource = configData.getPropertySources().get(propertySourceIndex);
ConfigData.Options options = configData.getOptions(propertySource);
ConfigurationPropertySource configurationPropertySource = ConfigurationPropertySource.from(propertySource);
return new ConfigDataEnvironmentContributor(Kind.UNBOUND_IMPORT, location, resource, profileSpecific,
propertySource, configurationPropertySource, null, options, null, conversionService);
propertySource, asConfigurationPropertySource(propertySource), null, options, null, conversionService);
}
private static ConfigurationPropertySource asConfigurationPropertySource(PropertySource<?> propertySource) {
ConfigurationPropertySource configurationPropertySource = ConfigurationPropertySource.from(propertySource);
if (configurationPropertySource != null && propertySource instanceof OriginLookup<?> originLookup) {
configurationPropertySource = configurationPropertySource.withPrefix(originLookup.getPrefix());
}
return configurationPropertySource;
}
/**

26
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/TestApplicationEnvironment.java

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
/*
* Copyright 2012-2025 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot;
/**
* Public version {@link ApplicationEnvironment} for tests to use.
*
* @author Phillip Webb
*/
public class TestApplicationEnvironment extends ApplicationEnvironment {
}

16
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java

@ -38,6 +38,7 @@ import org.junit.jupiter.api.Test; @@ -38,6 +38,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.TestApplicationEnvironment;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.context.properties.bind.BindContext;
import org.springframework.boot.context.properties.bind.BindException;
@ -461,6 +462,21 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests { @@ -461,6 +462,21 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests {
assertThat(context.getEnvironment().getProperty("my.property")).isEqualTo("fromotherpropertiesfile");
}
@Test // gh-45387
void runWhenProfileActivatedViaSystemEnvironmentVariableWithPrefix() {
this.application.setEnvironmentPrefix("example.prefix");
this.application.setEnvironment(new TestApplicationEnvironment() {
@Override
public Map<String, Object> getSystemEnvironment() {
return Map.of("EXAMPLE_PREFIX_SPRING_PROFILES_ACTIVE", "other,dev");
}
});
ConfigurableApplicationContext context = this.application.run();
assertThat(context.getEnvironment().getActiveProfiles()).contains("dev", "other");
}
@Test
@WithResource(name = "application.yaml", content = """
---

Loading…
Cancel
Save