Fix ConfigurationPropertySources parent attach
Update `ConfigurationPropertySources` so that the underlying sources are checked when attaching. Prior to this commit, in a parent/child setup the `ConfigurationPropertySources` adapter may already present on the parent. This means the attaching is skipped but the managed sources are incorrect. Fixes gh-12013
This commit is contained in:
parent
7ab587a067
commit
d6858ae162
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 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.
|
||||
|
@ -73,7 +73,12 @@ public final class ConfigurationPropertySources {
|
|||
Assert.isInstanceOf(ConfigurableEnvironment.class, environment);
|
||||
MutablePropertySources sources = ((ConfigurableEnvironment) environment)
|
||||
.getPropertySources();
|
||||
if (!sources.contains(ATTACHED_PROPERTY_SOURCE_NAME)) {
|
||||
PropertySource<?> attached = sources.get(ATTACHED_PROPERTY_SOURCE_NAME);
|
||||
if (attached != null && attached.getSource() != sources) {
|
||||
sources.remove(ATTACHED_PROPERTY_SOURCE_NAME);
|
||||
attached = null;
|
||||
}
|
||||
if (attached == null) {
|
||||
sources.addFirst(new ConfigurationPropertySourcesPropertySource(
|
||||
ATTACHED_PROPERTY_SOURCE_NAME,
|
||||
new SpringConfigurationPropertySources(sources)));
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 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.
|
||||
|
@ -56,6 +56,19 @@ public class ConfigurationPropertySourcesTests {
|
|||
assertThat(resolver.getProperty("server.port")).isEqualTo("1234");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void attachShouldReAttachInMergedSetup() {
|
||||
ConfigurableEnvironment parent = new StandardEnvironment();
|
||||
ConfigurationPropertySources.attach(parent);
|
||||
parent.getProperty("my.example-property");
|
||||
ConfigurableEnvironment child = new StandardEnvironment();
|
||||
child.merge(parent);
|
||||
child.getPropertySources().addLast(new MapPropertySource("config",
|
||||
Collections.singletonMap("my.example_property", "1234")));
|
||||
ConfigurationPropertySources.attach(child);
|
||||
assertThat(child.getProperty("my.example-property")).isEqualTo("1234");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getWhenNotAttachedShouldReturnAdapted() {
|
||||
ConfigurableEnvironment environment = new StandardEnvironment();
|
||||
|
|
Loading…
Reference in New Issue