Browse Source

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 5 years ago
parent
commit
ba5b36d733
  1. 9
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySources.java
  2. 13
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java
  3. 11
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySourcesTests.java

9
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.Iterator;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Random;
import java.util.function.Function; import java.util.function.Function;
import org.springframework.boot.origin.OriginLookup; import org.springframework.boot.origin.OriginLookup;
@ -132,16 +131,10 @@ class SpringConfigurationPropertySources implements Iterable<ConfigurationProper
} }
private boolean isIgnored(PropertySource<?> candidate) { private boolean isIgnored(PropertySource<?> candidate) {
return (isRandomPropertySource(candidate) || candidate instanceof StubPropertySource return (candidate instanceof StubPropertySource
|| candidate instanceof ConfigurationPropertySourcesPropertySource); || candidate instanceof ConfigurationPropertySourcesPropertySource);
} }
private boolean isRandomPropertySource(PropertySource<?> candidate) {
Object source = candidate.getSource();
return (source instanceof Random) || (source instanceof PropertySource<?>
&& ((PropertySource<?>) source).getSource() instanceof Random);
}
} }
} }

13
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.PeriodFormat;
import org.springframework.boot.convert.PeriodStyle; import org.springframework.boot.convert.PeriodStyle;
import org.springframework.boot.convert.PeriodUnit; 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.CapturedOutput;
import org.springframework.boot.testsupport.system.OutputCaptureExtension; import org.springframework.boot.testsupport.system.OutputCaptureExtension;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -1061,6 +1062,18 @@ class ConfigurationPropertiesTests {
assertThat(bean.getA()).isEqualTo("baz"); 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 @Test
void boundPropertiesShouldBeRecorded() { void boundPropertiesShouldBeRecorded() {
load(NestedConfiguration.class, "name=foo", "nested.name=bar"); load(NestedConfiguration.class, "name=foo", "nested.name=bar");

11
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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"); assertThat(configurationSources.iterator().next().getConfigurationProperty(name).getValue()).isEqualTo("s2");
} }
@Test @Test // gh-21659
void shouldNotAdaptRandomePropertySource() { void shouldAdaptRandomePropertySource() {
MutablePropertySources sources = new MutablePropertySources(); MutablePropertySources sources = new MutablePropertySources();
sources.addFirst(new RandomValuePropertySource()); sources.addFirst(new RandomValuePropertySource());
sources.addFirst(new MapPropertySource("test", Collections.singletonMap("a", "b")));
Iterator<ConfigurationPropertySource> iterator = new SpringConfigurationPropertySources(sources).iterator(); Iterator<ConfigurationPropertySource> iterator = new SpringConfigurationPropertySources(sources).iterator();
ConfigurationPropertyName name = ConfigurationPropertyName.of("a"); ConfigurationPropertyName name = ConfigurationPropertyName.of("random.int");
assertThat(iterator.next().getConfigurationProperty(name).getValue()).isEqualTo("b"); assertThat(iterator.next().getConfigurationProperty(name).getValue()).isNotNull();
assertThat(iterator.hasNext()).isFalse(); assertThat(iterator.hasNext()).isFalse();
} }

Loading…
Cancel
Save