Skip to content

Commit

Permalink
Fix ordering of properties and yaml files
Browse files Browse the repository at this point in the history
Fixes gh-24719
  • Loading branch information
mbhave committed Jan 13, 2021
1 parent 9a48423 commit ceff47a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
Expand Up @@ -16,9 +16,11 @@

package org.springframework.boot.context.config;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -171,11 +173,22 @@ private Set<StandardConfigDataReference> getReferencesForDirectory(ConfigDataLoc
String directory, String profile) {
Set<StandardConfigDataReference> references = new LinkedHashSet<>();
for (String name : this.configNames) {
for (PropertySourceLoader propertySourceLoader : this.propertySourceLoaders) {
for (String extension : propertySourceLoader.getFileExtensions()) {
StandardConfigDataReference reference = new StandardConfigDataReference(configDataLocation,
directory, directory + name, profile, extension, propertySourceLoader);
references.add(reference);
Deque<StandardConfigDataReference> referencesForName = getReferencesForConfigName(name, configDataLocation,
directory, profile);
references.addAll(referencesForName);
}
return references;
}

private Deque<StandardConfigDataReference> getReferencesForConfigName(String name,
ConfigDataLocation configDataLocation, String directory, String profile) {
Deque<StandardConfigDataReference> references = new ArrayDeque<>();
for (PropertySourceLoader propertySourceLoader : this.propertySourceLoaders) {
for (String extension : propertySourceLoader.getFileExtensions()) {
StandardConfigDataReference reference = new StandardConfigDataReference(configDataLocation, directory,
directory + name, profile, extension, propertySourceLoader);
if (!references.contains(reference)) {
references.addFirst(reference);
}
}
}
Expand Down
Expand Up @@ -135,6 +135,13 @@ void runLoadsFileWithCustomName() {
assertThat(property).isEqualTo("frompropertiesfile");
}

@Test
void runWhenPropertiesAndYamlShouldPreferProperties() {
ConfigurableApplicationContext context = this.application.run();
String property = context.getEnvironment().getProperty("duplicate");
assertThat(property).isEqualTo("properties");
}

@Test
void runWhenMultipleCustomNamesLoadsEachName() {
ConfigurableApplicationContext context = this.application
Expand Down
Expand Up @@ -3,3 +3,4 @@ value: 1234
my.property: fromapplicationproperties
sample.app.test.prop: *
my.placeholder: ${my.fallback}
duplicate=properties
@@ -0,0 +1 @@
duplicate: yaml

0 comments on commit ceff47a

Please sign in to comment.