Restore support for binding random properties

Revert 0588e989af so that `@ConfigurationProperties` can again be
bound using values from the `RandomValuePropertySource`.

Fixes gh-26201
pull/26253/head
Phillip Webb 4 years ago
parent 5e8f383947
commit ba5b36d733

@ -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<ConfigurationProper
}
private boolean isIgnored(PropertySource<?> 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);
}
}
}

@ -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<String, Object> 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");

@ -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<ConfigurationPropertySource> 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();
}

Loading…
Cancel
Save