Skip to content

Commit 23afabc

Browse files
committed
Allow override of ConfigData.Options
Update `ConfigDataEnvironmentUpdateListener` to allow Spring Cloud to override the actual ConfigData.Options that will be used for any specific contributor. Closes gh-42932
1 parent 7b28021 commit 23afabc

File tree

9 files changed

+123
-50
lines changed

9 files changed

+123
-50
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ public Options with(Option option) {
239239
}
240240

241241
private Options copy(Consumer<EnumSet<Option>> processor) {
242-
EnumSet<Option> options = EnumSet.copyOf(this.options);
242+
EnumSet<Option> options = (!this.options.isEmpty()) ? EnumSet.copyOf(this.options)
243+
: EnumSet.noneOf(Option.class);
243244
processor.accept(options);
244245
return new Options(options);
245246
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -188,7 +188,7 @@ private ConfigDataEnvironmentContributors createContributors(Binder binder) {
188188
protected ConfigDataEnvironmentContributors createContributors(
189189
List<ConfigDataEnvironmentContributor> contributors) {
190190
return new ConfigDataEnvironmentContributors(this.logFactory, this.bootstrapContext, contributors,
191-
this.environment.getConversionService());
191+
this.environment.getConversionService(), this.environmentUpdateListener);
192192
}
193193

194194
ConfigDataEnvironmentContributors getContributors() {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,13 +428,15 @@ static ConfigDataEnvironmentContributor ofExisting(PropertySource<?> propertySou
428428
* @param configData the config data
429429
* @param propertySourceIndex the index of the property source that should be used
430430
* @param conversionService the conversion service to use
431+
* @param environmentUpdateListener the environment update listener
431432
* @return a new {@link ConfigDataEnvironmentContributor} instance
432433
*/
433434
static ConfigDataEnvironmentContributor ofUnboundImport(ConfigDataLocation location, ConfigDataResource resource,
434435
boolean profileSpecific, ConfigData configData, int propertySourceIndex,
435-
ConversionService conversionService) {
436+
ConversionService conversionService, ConfigDataEnvironmentUpdateListener environmentUpdateListener) {
436437
PropertySource<?> propertySource = configData.getPropertySources().get(propertySourceIndex);
437438
ConfigData.Options options = configData.getOptions(propertySource);
439+
options = environmentUpdateListener.onConfigDataOptions(configData, propertySource, options);
438440
return new ConfigDataEnvironmentContributor(Kind.UNBOUND_IMPORT, location, resource, profileSpecific,
439441
propertySource, asConfigurationPropertySource(propertySource), null, options, null, conversionService);
440442
}

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,27 +62,34 @@ class ConfigDataEnvironmentContributors implements Iterable<ConfigDataEnvironmen
6262

6363
private final ConversionService conversionService;
6464

65+
private final ConfigDataEnvironmentUpdateListener environmentUpdateListener;
66+
6567
/**
6668
* Create a new {@link ConfigDataEnvironmentContributors} instance.
6769
* @param logFactory the log factory
6870
* @param bootstrapContext the bootstrap context
6971
* @param contributors the initial set of contributors
7072
* @param conversionService the conversion service to use
73+
* @param environmentUpdateListener the environment update listener
7174
*/
7275
ConfigDataEnvironmentContributors(DeferredLogFactory logFactory, ConfigurableBootstrapContext bootstrapContext,
73-
List<ConfigDataEnvironmentContributor> contributors, ConversionService conversionService) {
76+
List<ConfigDataEnvironmentContributor> contributors, ConversionService conversionService,
77+
ConfigDataEnvironmentUpdateListener environmentUpdateListener) {
7478
this.logger = logFactory.getLog(getClass());
7579
this.bootstrapContext = bootstrapContext;
7680
this.root = ConfigDataEnvironmentContributor.of(contributors, conversionService);
7781
this.conversionService = conversionService;
82+
this.environmentUpdateListener = environmentUpdateListener;
7883
}
7984

8085
private ConfigDataEnvironmentContributors(Log logger, ConfigurableBootstrapContext bootstrapContext,
81-
ConfigDataEnvironmentContributor root, ConversionService conversionService) {
86+
ConfigDataEnvironmentContributor root, ConversionService conversionService,
87+
ConfigDataEnvironmentUpdateListener environmentUpdateListener) {
8288
this.logger = logger;
8389
this.bootstrapContext = bootstrapContext;
8490
this.root = root;
8591
this.conversionService = conversionService;
92+
this.environmentUpdateListener = environmentUpdateListener;
8693
}
8794

8895
/**
@@ -110,7 +117,8 @@ ConfigDataEnvironmentContributors withProcessedImports(ConfigDataImporter import
110117
if (contributor.getKind() == Kind.UNBOUND_IMPORT) {
111118
ConfigDataEnvironmentContributor bound = contributor.withBoundProperties(result, activationContext);
112119
result = new ConfigDataEnvironmentContributors(this.logger, this.bootstrapContext,
113-
result.getRoot().withReplacement(contributor, bound), this.conversionService);
120+
result.getRoot().withReplacement(contributor, bound), this.conversionService,
121+
this.environmentUpdateListener);
114122
continue;
115123
}
116124
ConfigDataLocationResolverContext locationResolverContext = new ContributorConfigDataLocationResolverContext(
@@ -124,7 +132,8 @@ ConfigDataEnvironmentContributors withProcessedImports(ConfigDataImporter import
124132
ConfigDataEnvironmentContributor contributorAndChildren = contributor.withChildren(importPhase,
125133
asContributors(imported));
126134
result = new ConfigDataEnvironmentContributors(this.logger, this.bootstrapContext,
127-
result.getRoot().withReplacement(contributor, contributorAndChildren), this.conversionService);
135+
result.getRoot().withReplacement(contributor, contributorAndChildren), this.conversionService,
136+
this.environmentUpdateListener);
128137
processed++;
129138
}
130139
}
@@ -173,7 +182,7 @@ private List<ConfigDataEnvironmentContributor> asContributors(
173182
else {
174183
for (int i = data.getPropertySources().size() - 1; i >= 0; i--) {
175184
contributors.add(ConfigDataEnvironmentContributor.ofUnboundImport(location, resource,
176-
profileSpecific, data, i, this.conversionService));
185+
profileSpecific, data, i, this.conversionService, this.environmentUpdateListener));
177186
}
178187
}
179188
});

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.util.EventListener;
2020

21+
import org.springframework.boot.context.config.ConfigData.Options;
2122
import org.springframework.core.env.Environment;
2223
import org.springframework.core.env.PropertySource;
2324

@@ -53,4 +54,18 @@ default void onPropertySourceAdded(PropertySource<?> propertySource, ConfigDataL
5354
default void onSetProfiles(Profiles profiles) {
5455
}
5556

57+
/**
58+
* Called when config data options are obtained for a particular property source.
59+
* @param configData the config data
60+
* @param propertySource the property source
61+
* @param options the options as provided by
62+
* {@link ConfigData#getOptions(PropertySource)}
63+
* @return the actual options that should be used
64+
* @since 3.5.1
65+
*/
66+
default ConfigData.Options onConfigDataOptions(ConfigData configData, PropertySource<?> propertySource,
67+
Options options) {
68+
return options;
69+
}
70+
5671
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -91,7 +91,7 @@ void getLocationReturnsLocation() {
9191
ConfigData configData = new ConfigData(Collections.singleton(new MockPropertySource()));
9292
ConfigDataResource resource = mock(ConfigDataResource.class);
9393
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofUnboundImport(TEST_LOCATION,
94-
resource, false, configData, 0, this.conversionService);
94+
resource, false, configData, 0, this.conversionService, ConfigDataEnvironmentUpdateListener.NONE);
9595
assertThat(contributor.getResource()).isSameAs(resource);
9696
}
9797

@@ -109,7 +109,7 @@ void getConfigurationPropertySourceReturnsAdaptedPropertySource() {
109109
propertySource.setProperty("spring", "boot");
110110
ConfigData configData = new ConfigData(Collections.singleton(propertySource));
111111
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofUnboundImport(null, null,
112-
false, configData, 0, this.conversionService);
112+
false, configData, 0, this.conversionService, ConfigDataEnvironmentUpdateListener.NONE);
113113
assertThat(contributor.getConfigurationPropertySource()
114114
.getConfigurationProperty(ConfigurationPropertyName.of("spring"))
115115
.getValue()).isEqualTo("boot");
@@ -321,7 +321,7 @@ void ofUnboundImportCreatesImportedContributor() {
321321
propertySource.setProperty("spring.config.import", "test");
322322
ConfigData configData = new ConfigData(Collections.singleton(propertySource));
323323
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofUnboundImport(TEST_LOCATION,
324-
resource, false, configData, 0, this.conversionService);
324+
resource, false, configData, 0, this.conversionService, ConfigDataEnvironmentUpdateListener.NONE);
325325
assertThat(contributor.getKind()).isEqualTo(Kind.UNBOUND_IMPORT);
326326
assertThat(contributor.getResource()).isSameAs(resource);
327327
assertThat(contributor.getImports()).isEmpty();
@@ -368,7 +368,7 @@ void withBoundPropertiesWhenIgnoringImportsAndNothingBound() {
368368
TestResource resource = new TestResource("a");
369369
ConfigData configData = new ConfigData(Collections.singleton(new MockPropertySource()), Option.IGNORE_IMPORTS);
370370
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofUnboundImport(TEST_LOCATION,
371-
resource, false, configData, 0, this.conversionService);
371+
resource, false, configData, 0, this.conversionService, ConfigDataEnvironmentUpdateListener.NONE);
372372
ConfigDataEnvironmentContributor bound = contributor.withBoundProperties(Collections.singleton(contributor),
373373
null);
374374
assertThat(bound).isNotNull();
@@ -382,7 +382,8 @@ private ConfigDataEnvironmentContributor createBoundContributor(String location)
382382
private ConfigDataEnvironmentContributor createBoundContributor(ConfigDataResource resource, ConfigData configData,
383383
int propertySourceIndex) {
384384
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofUnboundImport(TEST_LOCATION,
385-
resource, false, configData, propertySourceIndex, this.conversionService);
385+
resource, false, configData, propertySourceIndex, this.conversionService,
386+
ConfigDataEnvironmentUpdateListener.NONE);
386387
return contributor.withBoundProperties(Collections.singleton(contributor), null);
387388
}
388389

0 commit comments

Comments
 (0)