Skip to content

Commit d9670e1

Browse files
committed
Fix YAML load ordering problems. See https://jira.spring.io/browse/SPR-12471
1 parent 5a40fa8 commit d9670e1

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

grails-bootstrap/src/main/groovy/grails/config/ConfigMap.groovy

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ class ConfigMap implements Map<String, Object>, Cloneable {
1515
public ConfigMap() {
1616
rootConfig = this
1717
path = []
18-
delegateMap = [:]
18+
delegateMap = new LinkedHashMap<>()
1919
}
2020

2121
public ConfigMap(ConfigMap rootConfig, List<String> path) {
2222
super()
2323
this.rootConfig = rootConfig
2424
this.path = path
25-
delegateMap = [:]
25+
delegateMap = new LinkedHashMap<>()
2626
}
2727

2828
public ConfigMap clone() {

grails-core/src/main/groovy/org/grails/config/PropertySourcesConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected void initializeFromPropertySources(PropertySources propertySources) {
7171
}
7272

7373
private void mergeEnumerablePropertySource(EnumerablePropertySource enumerablePropertySource) {
74-
Map map = new HashMap();
74+
Map map = new LinkedHashMap();
7575
for(String propertyName : enumerablePropertySource.getPropertyNames()) {
7676
map.put(propertyName, enumerablePropertySource.getProperty(propertyName));
7777
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2014 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.grails.config.yaml
17+
18+
import groovy.transform.CompileStatic
19+
import org.springframework.beans.factory.config.YamlProcessor
20+
import org.springframework.boot.env.PropertySourceLoader
21+
import org.springframework.boot.yaml.SpringProfileDocumentMatcher
22+
import org.springframework.core.Ordered
23+
import org.springframework.core.annotation.Order
24+
import org.springframework.core.env.MapPropertySource
25+
import org.springframework.core.env.PropertySource
26+
import org.springframework.core.io.Resource
27+
import org.springframework.util.ClassUtils
28+
29+
/**
30+
* @author Graeme Rocher
31+
* @since 3.0
32+
*/
33+
@CompileStatic
34+
@Order(Ordered.HIGHEST_PRECEDENCE)
35+
class YamlPropertySourceLoader extends YamlProcessor implements PropertySourceLoader {
36+
@Override
37+
String[] getFileExtensions() {
38+
['yml', 'yaml'] as String[]
39+
}
40+
41+
@Override
42+
PropertySource<?> load(String name, Resource resource, String profile) throws IOException {
43+
if (ClassUtils.isPresent("org.yaml.snakeyaml.Yaml", null)) {
44+
if (profile == null) {
45+
matchDefault = true
46+
setDocumentMatchers(new SpringProfileDocumentMatcher())
47+
}
48+
else {
49+
matchDefault = false;
50+
setDocumentMatchers(new SpringProfileDocumentMatcher(profile))
51+
}
52+
resources = [resource] as Resource[]
53+
def propertySource = new LinkedHashMap<>()
54+
process { Properties properties, Map<String, Object> map ->
55+
propertySource.putAll(properties)
56+
propertySource.putAll(map)
57+
}
58+
if (!propertySource.isEmpty()) {
59+
return new MapPropertySource(name, propertySource);
60+
}
61+
}
62+
return null
63+
}
64+
65+
66+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# PropertySource Loaders
22
org.springframework.boot.env.PropertySourceLoader=\
3-
org.grails.core.cfg.GroovyConfigPropertySourceLoader
3+
org.grails.core.cfg.GroovyConfigPropertySourceLoader,\
4+
org.grails.config.yaml.YamlPropertySourceLoader

0 commit comments

Comments
 (0)