From ba5b36d733f0668ae320d809abbc4b7de69286ca Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 26 Apr 2021 15:40:25 -0700 Subject: [PATCH] Restore support for binding random properties Revert 0588e989af93 so that `@ConfigurationProperties` can again be bound using values from the `RandomValuePropertySource`. Fixes gh-26201 --- .../source/SpringConfigurationPropertySources.java | 9 +-------- .../properties/ConfigurationPropertiesTests.java | 13 +++++++++++++ .../SpringConfigurationPropertySourcesTests.java | 11 +++++------ 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySources.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySources.java index 5ee1caca54d..c3202f8967a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySources.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySources.java @@ -21,7 +21,6 @@ import java.util.Deque; import java.util.Iterator; import java.util.Map; import java.util.NoSuchElementException; -import java.util.Random; import java.util.function.Function; import org.springframework.boot.origin.OriginLookup; @@ -132,16 +131,10 @@ class SpringConfigurationPropertySources implements Iterable candidate) { - return (isRandomPropertySource(candidate) || candidate instanceof StubPropertySource + return (candidate instanceof StubPropertySource || candidate instanceof ConfigurationPropertySourcesPropertySource); } - private boolean isRandomPropertySource(PropertySource candidate) { - Object source = candidate.getSource(); - return (source instanceof Random) || (source instanceof PropertySource - && ((PropertySource) source).getSource() instanceof Random); - } - } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java index bc500e3ef27..050c0e48266 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java @@ -65,6 +65,7 @@ import org.springframework.boot.convert.DurationUnit; import org.springframework.boot.convert.PeriodFormat; import org.springframework.boot.convert.PeriodStyle; import org.springframework.boot.convert.PeriodUnit; +import org.springframework.boot.env.RandomValuePropertySource; import org.springframework.boot.testsupport.system.CapturedOutput; import org.springframework.boot.testsupport.system.OutputCaptureExtension; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -1061,6 +1062,18 @@ class ConfigurationPropertiesTests { assertThat(bean.getA()).isEqualTo("baz"); } + @Test // gh-26201 + void loadWhenBoundToRandomPropertyPlaceholder() { + MutablePropertySources sources = this.context.getEnvironment().getPropertySources(); + sources.addFirst(new RandomValuePropertySource()); + Map source = new HashMap<>(); + source.put("com.example.bar", "${random.int}"); + sources.addLast(new MapPropertySource("test", source)); + load(SimplePrefixedProperties.class); + SimplePrefixedProperties bean = this.context.getBean(SimplePrefixedProperties.class); + assertThat(bean.getBar()).isNotNull().containsOnlyDigits(); + } + @Test void boundPropertiesShouldBeRecorded() { load(NestedConfiguration.class, "name=foo", "nested.name=bar"); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySourcesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySourcesTests.java index 260833f084c..ad47ad8bb9e 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySourcesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySourcesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 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. @@ -162,14 +162,13 @@ class SpringConfigurationPropertySourcesTests { assertThat(configurationSources.iterator().next().getConfigurationProperty(name).getValue()).isEqualTo("s2"); } - @Test - void shouldNotAdaptRandomePropertySource() { + @Test // gh-21659 + void shouldAdaptRandomePropertySource() { MutablePropertySources sources = new MutablePropertySources(); sources.addFirst(new RandomValuePropertySource()); - sources.addFirst(new MapPropertySource("test", Collections.singletonMap("a", "b"))); Iterator iterator = new SpringConfigurationPropertySources(sources).iterator(); - ConfigurationPropertyName name = ConfigurationPropertyName.of("a"); - assertThat(iterator.next().getConfigurationProperty(name).getValue()).isEqualTo("b"); + ConfigurationPropertyName name = ConfigurationPropertyName.of("random.int"); + assertThat(iterator.next().getConfigurationProperty(name).getValue()).isNotNull(); assertThat(iterator.hasNext()).isFalse(); }