From d6858ae16239b0108ae68569b3b204eb26f67e36 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 12 Feb 2018 15:03:35 -0800 Subject: [PATCH] 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 --- .../source/ConfigurationPropertySources.java | 9 +++++++-- .../source/ConfigurationPropertySourcesTests.java | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySources.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySources.java index 4e57523359..002c9e084d 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySources.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySources.java @@ -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))); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesTests.java index 02f7b48538..1feaaaf079 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesTests.java @@ -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();