Skip to content

Commit 8189e82

Browse files
committed
Allow nested profile-specific resolution
Effectively revert commit 0da0d2d so that the `resolveProfileSpecific` method of `ConfigDataLocationResolver` is again called when resolving imports declared in a profile-specific file. Fixes gh-26960
1 parent 5dc1065 commit 8189e82

File tree

7 files changed

+44
-50
lines changed

7 files changed

+44
-50
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributors.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,9 @@ ConfigDataEnvironmentContributors withProcessedImports(ConfigDataImporter import
117117
result, contributor, activationContext);
118118
ConfigDataLoaderContext loaderContext = new ContributorDataLoaderContext(this);
119119
List<ConfigDataLocation> imports = contributor.getImports();
120-
boolean resolveProfileSpecific = !contributor.isFromProfileSpecificImport();
121120
this.logger.trace(LogMessage.format("Processing imports %s", imports));
122121
Map<ConfigDataResolutionResult, ConfigData> imported = importer.resolveAndLoad(activationContext,
123-
locationResolverContext, loaderContext, imports, resolveProfileSpecific);
122+
locationResolverContext, loaderContext, imports);
124123
this.logger.trace(LogMessage.of(() -> getImportedMessage(imported.keySet())));
125124
ConfigDataEnvironmentContributor contributorAndChildren = contributor.withChildren(importPhase,
126125
asContributors(imported));

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataImporter.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,14 @@ class ConfigDataImporter {
7575
* @param locationResolverContext the location resolver context
7676
* @param loaderContext the loader context
7777
* @param locations the locations to resolve
78-
* @param resolveProfileSpecific if profile specific resolution should be attempted
7978
* @return a map of the loaded locations and data
8079
*/
8180
Map<ConfigDataResolutionResult, ConfigData> resolveAndLoad(ConfigDataActivationContext activationContext,
8281
ConfigDataLocationResolverContext locationResolverContext, ConfigDataLoaderContext loaderContext,
83-
List<ConfigDataLocation> locations, boolean resolveProfileSpecific) {
82+
List<ConfigDataLocation> locations) {
8483
try {
8584
Profiles profiles = (activationContext != null) ? activationContext.getProfiles() : null;
86-
List<ConfigDataResolutionResult> resolved = resolve(locationResolverContext, profiles, locations,
87-
resolveProfileSpecific);
85+
List<ConfigDataResolutionResult> resolved = resolve(locationResolverContext, profiles, locations);
8886
return load(loaderContext, resolved);
8987
}
9088
catch (IOException ex) {
@@ -93,18 +91,18 @@ Map<ConfigDataResolutionResult, ConfigData> resolveAndLoad(ConfigDataActivationC
9391
}
9492

9593
private List<ConfigDataResolutionResult> resolve(ConfigDataLocationResolverContext locationResolverContext,
96-
Profiles profiles, List<ConfigDataLocation> locations, boolean resolveProfileSpecific) {
94+
Profiles profiles, List<ConfigDataLocation> locations) {
9795
List<ConfigDataResolutionResult> resolved = new ArrayList<>(locations.size());
9896
for (ConfigDataLocation location : locations) {
99-
resolved.addAll(resolve(locationResolverContext, profiles, location, resolveProfileSpecific));
97+
resolved.addAll(resolve(locationResolverContext, profiles, location));
10098
}
10199
return Collections.unmodifiableList(resolved);
102100
}
103101

104102
private List<ConfigDataResolutionResult> resolve(ConfigDataLocationResolverContext locationResolverContext,
105-
Profiles profiles, ConfigDataLocation location, boolean resolveProfileSpecific) {
103+
Profiles profiles, ConfigDataLocation location) {
106104
try {
107-
return this.resolvers.resolve(locationResolverContext, location, profiles, resolveProfileSpecific);
105+
return this.resolvers.resolve(locationResolverContext, location, profiles);
108106
}
109107
catch (ConfigDataNotFoundException ex) {
110108
handle(ex, location, null);

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataLocationResolvers.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,22 @@ private List<ConfigDataLocationResolver<?>> reorder(List<ConfigDataLocationResol
9898
}
9999

100100
List<ConfigDataResolutionResult> resolve(ConfigDataLocationResolverContext context, ConfigDataLocation location,
101-
Profiles profiles, boolean resolveProfileSpecific) {
101+
Profiles profiles) {
102102
if (location == null) {
103103
return Collections.emptyList();
104104
}
105105
for (ConfigDataLocationResolver<?> resolver : getResolvers()) {
106106
if (resolver.isResolvable(context, location)) {
107-
return resolve(resolver, context, location, profiles, resolveProfileSpecific);
107+
return resolve(resolver, context, location, profiles);
108108
}
109109
}
110110
throw new UnsupportedConfigDataLocationException(location);
111111
}
112112

113113
private List<ConfigDataResolutionResult> resolve(ConfigDataLocationResolver<?> resolver,
114-
ConfigDataLocationResolverContext context, ConfigDataLocation location, Profiles profiles,
115-
boolean resolveProfileSpecific) {
114+
ConfigDataLocationResolverContext context, ConfigDataLocation location, Profiles profiles) {
116115
List<ConfigDataResolutionResult> resolved = resolve(location, false, () -> resolver.resolve(context, location));
117-
if (profiles == null || !resolveProfileSpecific) {
116+
if (profiles == null) {
118117
return resolved;
119118
}
120119
List<ConfigDataResolutionResult> profileSpecific = resolve(location, true,

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributorsTests.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import static org.assertj.core.api.Assertions.assertThat;
4444
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4545
import static org.mockito.ArgumentMatchers.any;
46-
import static org.mockito.ArgumentMatchers.anyBoolean;
4746
import static org.mockito.ArgumentMatchers.eq;
4847
import static org.mockito.BDDMockito.given;
4948
import static org.mockito.Mockito.mock;
@@ -120,7 +119,7 @@ void withProcessedImportsResolvesAndLoads() {
120119
Map<ConfigDataResolutionResult, ConfigData> imported = new LinkedHashMap<>();
121120
imported.put(new ConfigDataResolutionResult(LOCATION_1, new TestConfigDataResource("a"), false),
122121
new ConfigData(Arrays.asList(propertySource)));
123-
given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(locations), anyBoolean()))
122+
given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(locations)))
124123
.willReturn(imported);
125124
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofInitialImport(LOCATION_1);
126125
ConfigDataEnvironmentContributors contributors = new ConfigDataEnvironmentContributors(this.logFactory,
@@ -143,14 +142,14 @@ void withProcessedImportsResolvesAndLoadsChainedImports() {
143142
Map<ConfigDataResolutionResult, ConfigData> initialImported = new LinkedHashMap<>();
144143
initialImported.put(new ConfigDataResolutionResult(LOCATION_1, new TestConfigDataResource("a"), false),
145144
new ConfigData(Arrays.asList(initialPropertySource)));
146-
given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(initialLocations),
147-
anyBoolean())).willReturn(initialImported);
145+
given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(initialLocations)))
146+
.willReturn(initialImported);
148147
List<ConfigDataLocation> secondLocations = Arrays.asList(LOCATION_2);
149148
MockPropertySource secondPropertySource = new MockPropertySource();
150149
Map<ConfigDataResolutionResult, ConfigData> secondImported = new LinkedHashMap<>();
151150
secondImported.put(new ConfigDataResolutionResult(LOCATION_2, new TestConfigDataResource("b"), false),
152151
new ConfigData(Arrays.asList(secondPropertySource)));
153-
given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(secondLocations), anyBoolean()))
152+
given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(secondLocations)))
154153
.willReturn(secondImported);
155154
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofInitialImport(LOCATION_1);
156155
ConfigDataEnvironmentContributors contributors = new ConfigDataEnvironmentContributors(this.logFactory,
@@ -177,13 +176,13 @@ void withProcessedImportsProvidesLocationResolverContextWithAccessToBinder() {
177176
Map<ConfigDataResolutionResult, ConfigData> imported = new LinkedHashMap<>();
178177
imported.put(new ConfigDataResolutionResult(LOCATION_1, new TestConfigDataResource("a'"), false),
179178
new ConfigData(Arrays.asList(propertySource)));
180-
given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(locations), anyBoolean()))
179+
given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(locations)))
181180
.willReturn(imported);
182181
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofInitialImport(LOCATION_1);
183182
ConfigDataEnvironmentContributors contributors = new ConfigDataEnvironmentContributors(this.logFactory,
184183
this.bootstrapContext, Arrays.asList(existingContributor, contributor));
185184
contributors.withProcessedImports(this.importer, this.activationContext);
186-
verify(this.importer).resolveAndLoad(any(), this.locationResolverContext.capture(), any(), any(), anyBoolean());
185+
verify(this.importer).resolveAndLoad(any(), this.locationResolverContext.capture(), any(), any());
187186
ConfigDataLocationResolverContext context = this.locationResolverContext.getValue();
188187
assertThat(context.getBinder().bind("test", String.class).get()).isEqualTo("springboot");
189188
}
@@ -197,21 +196,20 @@ void withProcessedImportsProvidesLocationResolverContextWithAccessToParent() {
197196
Map<ConfigDataResolutionResult, ConfigData> initialImported = new LinkedHashMap<>();
198197
initialImported.put(new ConfigDataResolutionResult(LOCATION_1, new TestConfigDataResource("a"), false),
199198
new ConfigData(Arrays.asList(initialPropertySource)));
200-
given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(initialLocations),
201-
anyBoolean())).willReturn(initialImported);
199+
given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(initialLocations)))
200+
.willReturn(initialImported);
202201
List<ConfigDataLocation> secondLocations = Arrays.asList(LOCATION_2);
203202
MockPropertySource secondPropertySource = new MockPropertySource();
204203
Map<ConfigDataResolutionResult, ConfigData> secondImported = new LinkedHashMap<>();
205204
secondImported.put(new ConfigDataResolutionResult(LOCATION_2, new TestConfigDataResource("b"), false),
206205
new ConfigData(Arrays.asList(secondPropertySource)));
207-
given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(secondLocations), anyBoolean()))
206+
given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(secondLocations)))
208207
.willReturn(secondImported);
209208
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofInitialImport(LOCATION_1);
210209
ConfigDataEnvironmentContributors contributors = new ConfigDataEnvironmentContributors(this.logFactory,
211210
this.bootstrapContext, Arrays.asList(contributor));
212211
contributors.withProcessedImports(this.importer, this.activationContext);
213-
verify(this.importer).resolveAndLoad(any(), this.locationResolverContext.capture(), any(), eq(secondLocations),
214-
anyBoolean());
212+
verify(this.importer).resolveAndLoad(any(), this.locationResolverContext.capture(), any(), eq(secondLocations));
215213
ConfigDataLocationResolverContext context = this.locationResolverContext.getValue();
216214
assertThat(context.getParent()).hasToString("a");
217215
}
@@ -228,13 +226,13 @@ void withProcessedImportsProvidesLocationResolverContextWithAccessToBootstrapReg
228226
Map<ConfigDataResolutionResult, ConfigData> imported = new LinkedHashMap<>();
229227
imported.put(new ConfigDataResolutionResult(LOCATION_1, new TestConfigDataResource("a'"), false),
230228
new ConfigData(Arrays.asList(propertySource)));
231-
given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(locations), anyBoolean()))
229+
given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(locations)))
232230
.willReturn(imported);
233231
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofInitialImport(LOCATION_1);
234232
ConfigDataEnvironmentContributors contributors = new ConfigDataEnvironmentContributors(this.logFactory,
235233
this.bootstrapContext, Arrays.asList(existingContributor, contributor));
236234
contributors.withProcessedImports(this.importer, this.activationContext);
237-
verify(this.importer).resolveAndLoad(any(), this.locationResolverContext.capture(), any(), any(), anyBoolean());
235+
verify(this.importer).resolveAndLoad(any(), this.locationResolverContext.capture(), any(), any());
238236
ConfigDataLocationResolverContext context = this.locationResolverContext.getValue();
239237
assertThat(context.getBootstrapContext()).isSameAs(this.bootstrapContext);
240238
}
@@ -251,13 +249,13 @@ void withProcessedImportsProvidesLoaderContextWithAccessToBootstrapRegistry() {
251249
Map<ConfigDataResolutionResult, ConfigData> imported = new LinkedHashMap<>();
252250
imported.put(new ConfigDataResolutionResult(LOCATION_1, new TestConfigDataResource("a'"), false),
253251
new ConfigData(Arrays.asList(propertySource)));
254-
given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(locations), anyBoolean()))
252+
given(this.importer.resolveAndLoad(eq(this.activationContext), any(), any(), eq(locations)))
255253
.willReturn(imported);
256254
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofInitialImport(LOCATION_1);
257255
ConfigDataEnvironmentContributors contributors = new ConfigDataEnvironmentContributors(this.logFactory,
258256
this.bootstrapContext, Arrays.asList(existingContributor, contributor));
259257
contributors.withProcessedImports(this.importer, this.activationContext);
260-
verify(this.importer).resolveAndLoad(any(), any(), this.loaderContext.capture(), any(), anyBoolean());
258+
verify(this.importer).resolveAndLoad(any(), any(), this.loaderContext.capture(), any());
261259
ConfigDataLoaderContext context = this.loaderContext.getValue();
262260
assertThat(context.getBootstrapContext()).isSameAs(this.bootstrapContext);
263261
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -750,26 +750,26 @@ void runWhenHasProfileSpecificFileWithActiveOnProfileProperty() {
750750
assertThat(environment.getProperty("test2")).isEqualTo("test2");
751751
}
752752

753-
@Test // gh-26752
754-
void runWhenHasProfileSpecificImportWithImportDoesNotImportSecondProfileSpecificFile() {
753+
@Test // gh-26960
754+
void runWhenHasProfileSpecificImportWithImportImportsSecondProfileSpecificFile() {
755755
ConfigurableApplicationContext context = this.application
756756
.run("--spring.config.name=application-profile-specific-import-with-import");
757757
ConfigurableEnvironment environment = context.getEnvironment();
758758
assertThat(environment.containsProperty("application-profile-specific-import-with-import")).isTrue();
759759
assertThat(environment.containsProperty("application-profile-specific-import-with-import-p1")).isTrue();
760760
assertThat(environment.containsProperty("application-profile-specific-import-with-import-p2")).isFalse();
761761
assertThat(environment.containsProperty("application-profile-specific-import-with-import-import")).isTrue();
762-
assertThat(environment.containsProperty("application-profile-specific-import-with-import-import-p1")).isFalse();
763-
assertThat(environment.containsProperty("application-profile-specific-import-with-import-import-p2")).isFalse();
762+
assertThat(environment.containsProperty("application-profile-specific-import-with-import-import-p1")).isTrue();
763+
assertThat(environment.containsProperty("application-profile-specific-import-with-import-import-p2")).isTrue();
764764
}
765765

766-
@Test // gh-26753
767-
void runWhenHasProfileSpecificImportWithCustomImportDoesNotResolveProfileSpecific() {
766+
@Test // gh-26960
767+
void runWhenHasProfileSpecificImportWithCustomImportResolvesProfileSpecific() {
768768
ConfigurableApplicationContext context = this.application
769769
.run("--spring.config.name=application-profile-specific-import-with-custom-import");
770770
ConfigurableEnvironment environment = context.getEnvironment();
771771
assertThat(environment.containsProperty("test:boot")).isTrue();
772-
assertThat(environment.containsProperty("test:boot:ps")).isFalse();
772+
assertThat(environment.containsProperty("test:boot:ps")).isTrue();
773773
}
774774

775775
@Test // gh-26593

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataImporterTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@ void loadImportsResolvesAndLoadsLocations() throws Exception {
8080
TestResource resource2 = new TestResource("r2");
8181
ConfigData configData1 = new ConfigData(Collections.singleton(new MockPropertySource()));
8282
ConfigData configData2 = new ConfigData(Collections.singleton(new MockPropertySource()));
83-
given(this.resolvers.resolve(this.locationResolverContext, location1, this.profiles, true))
83+
given(this.resolvers.resolve(this.locationResolverContext, location1, this.profiles))
8484
.willReturn(Collections.singletonList(new ConfigDataResolutionResult(location1, resource1, false)));
85-
given(this.resolvers.resolve(this.locationResolverContext, location2, this.profiles, true))
85+
given(this.resolvers.resolve(this.locationResolverContext, location2, this.profiles))
8686
.willReturn(Collections.singletonList(new ConfigDataResolutionResult(location2, resource2, false)));
8787
given(this.loaders.load(this.loaderContext, resource1)).willReturn(configData1);
8888
given(this.loaders.load(this.loaderContext, resource2)).willReturn(configData2);
8989
ConfigDataImporter importer = new ConfigDataImporter(this.logFactory, ConfigDataNotFoundAction.FAIL,
9090
this.resolvers, this.loaders);
9191
Collection<ConfigData> loaded = importer.resolveAndLoad(this.activationContext, this.locationResolverContext,
92-
this.loaderContext, Arrays.asList(location1, location2), true).values();
92+
this.loaderContext, Arrays.asList(location1, location2)).values();
9393
assertThat(loaded).containsExactly(configData2, configData1);
9494
}
9595

@@ -106,21 +106,21 @@ void loadImportsWhenAlreadyImportedLocationSkipsLoad() throws Exception {
106106
ConfigData configData1 = new ConfigData(Collections.singleton(new MockPropertySource()));
107107
ConfigData configData2 = new ConfigData(Collections.singleton(new MockPropertySource()));
108108
ConfigData configData3 = new ConfigData(Collections.singleton(new MockPropertySource()));
109-
given(this.resolvers.resolve(this.locationResolverContext, location1, this.profiles, true))
109+
given(this.resolvers.resolve(this.locationResolverContext, location1, this.profiles))
110110
.willReturn(Collections.singletonList(new ConfigDataResolutionResult(location1, resource1, false)));
111-
given(this.resolvers.resolve(this.locationResolverContext, location2, this.profiles, true))
111+
given(this.resolvers.resolve(this.locationResolverContext, location2, this.profiles))
112112
.willReturn(Collections.singletonList(new ConfigDataResolutionResult(location2, resource2, false)));
113-
given(this.resolvers.resolve(this.locationResolverContext, location3, this.profiles, true))
113+
given(this.resolvers.resolve(this.locationResolverContext, location3, this.profiles))
114114
.willReturn(Collections.singletonList(new ConfigDataResolutionResult(location3, resource3, false)));
115115
given(this.loaders.load(this.loaderContext, resource1)).willReturn(configData1);
116116
given(this.loaders.load(this.loaderContext, resource2)).willReturn(configData2);
117117
given(this.loaders.load(this.loaderContext, resource3)).willReturn(configData3);
118118
ConfigDataImporter importer = new ConfigDataImporter(this.logFactory, ConfigDataNotFoundAction.FAIL,
119119
this.resolvers, this.loaders);
120120
Collection<ConfigData> loaded1and2 = importer.resolveAndLoad(this.activationContext,
121-
this.locationResolverContext, this.loaderContext, locations1and2, true).values();
121+
this.locationResolverContext, this.loaderContext, locations1and2).values();
122122
Collection<ConfigData> loaded2and3 = importer.resolveAndLoad(this.activationContext,
123-
this.locationResolverContext, this.loaderContext, locations2and3, true).values();
123+
this.locationResolverContext, this.loaderContext, locations2and3).values();
124124
assertThat(loaded1and2).containsExactly(configData2, configData1);
125125
assertThat(loaded2and3).containsExactly(configData3);
126126
}

0 commit comments

Comments
 (0)