Skip to content

Commit 2a987f5

Browse files
committed
Merge branch '7.0.x' into 7.1.x
2 parents 5ae8e87 + 95f6389 commit 2a987f5

15 files changed

Lines changed: 1848 additions & 119 deletions

File tree

‎grails-bootstrap/src/main/groovy/org/grails/config/NavigableMap.groovy‎

Lines changed: 392 additions & 2 deletions
Large diffs are not rendered by default.

‎grails-bootstrap/src/test/groovy/grails/config/ConfigMapSpec.groovy‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,69 @@ test.another = true
208208
!cloned.is(configMap)
209209
cloned == configMap
210210
}
211+
212+
def "an entry of a map written by map subscripts is flattened once, by its bracketed key, #description"() {
213+
given:
214+
NavigableMap configMap = new NavigableMap()
215+
merges.each { Map merged -> configMap.merge(merged, true) }
216+
217+
when:
218+
NavigableMap flattened = from.call(configMap)
219+
220+
then:
221+
flattened.toFlatConfig().keySet() == keys as Set
222+
flattened.toProperties().stringPropertyNames() == keys as Set
223+
224+
where:
225+
description | merges | from | keys
226+
'after a dotted key in the same map' | [['app.routes.default': 'z'], ['app.routes[/api/**]': 'x']] | { NavigableMap map -> map } | ['app.routes.default', 'app.routes[/api/**]']
227+
'after another subscript' | [['app.tenants[acme].routes[/api/**]': 'x']] | { NavigableMap map -> map } | ['app.tenants[acme].routes[/api/**]']
228+
'from a nested map' | [['app.routes[/api/**]': 'x']] | { NavigableMap map -> map.navigate('app') } | ['routes[/api/**]']
229+
'from a copy' | [['app.routes[/api/**]': 'x']] | { NavigableMap map -> map.clone() } | ['app.routes[/api/**]']
230+
}
231+
232+
def "an entry written in brackets is flattened once when every field is held in brackets, #description"() {
233+
given:
234+
NavigableMap configMap = new NavigableMap()
235+
configMap.merge(merged, true)
236+
237+
expect:
238+
configMap.toFlatConfig().keySet() == keys as Set
239+
configMap.toProperties().stringPropertyNames() == keys as Set
240+
241+
where:
242+
description | merged | keys
243+
'a map written in brackets' | ['app.routes[api]': [target: 'http://localhost', enabled: 'true']] | ['app.routes[api].target', 'app.routes[api].enabled']
244+
'lists written below a bracket' | ['grails.cors.mappings[/api/**].allowedOrigins[0]': 'https://a', 'grails.cors.mappings[/api/**].allowedMethods[0]': 'GET'] | ['grails.cors.mappings[/api/**].allowedOrigins', 'grails.cors.mappings[/api/**].allowedOrigins[0]', 'grails.cors.mappings[/api/**].allowedMethods', 'grails.cors.mappings[/api/**].allowedMethods[0]']
245+
}
246+
247+
def "a dotted name is flattened as before, unless every bracketed name it spells holds the same value, #description"() {
248+
given:
249+
NavigableMap configMap = new NavigableMap()
250+
251+
when:
252+
writes.each { Closure write -> write.call(configMap) }
253+
254+
then:
255+
configMap.toProperties() == exported
256+
configMap.toFlatConfig() == exported
257+
258+
where:
259+
description | exported | writes
260+
'a field merged after a map written in brackets' | ['routes.api.target': 'http://localhost', 'routes[api].enabled': 'true'] | [{ NavigableMap m -> m.merge(['routes[api]': [target: 'http://localhost'], 'routes[api].enabled': 'true'], true) }]
261+
'a dotted key merged after a null in brackets' | ['app.routes.k': 'new'] | [{ NavigableMap m -> m.merge(['app.routes[k]': null], true) }, { NavigableMap m -> m.merge(['app.routes.k': 'new'], true) }]
262+
'a dotted key merged after a value in brackets' | ['app.routes.k': 'new', 'app.routes[k]': 'old'] | [{ NavigableMap m -> m.merge(['app.routes[k]': 'old'], true) }, { NavigableMap m -> m.merge(['app.routes.k': 'new'], true) }]
263+
'a dotted key put after a key in brackets' | ['app.routes.k': 'new', 'app.routes[k]': 'old'] | [{ NavigableMap m -> m.merge(['app.routes[k]': 'old'], true) }, { NavigableMap m -> m.put('app.routes.k', 'new') }]
264+
'a key in brackets merged after a dotted key' | ['app.routes[k]': 'new'] | [{ NavigableMap m -> m.merge(['app.routes.k': 'old'], true) }, { NavigableMap m -> m.merge(['app.routes[k]': 'new'], true) }]
265+
'a map in brackets merged after a dotted key below it' | ['a[b].c': 'new'] | [{ NavigableMap m -> m.merge(['a.b.c': 'old'], true) }, { NavigableMap m -> m.merge(['a[b].c': 'new'], true) }]
266+
'a dotted key merged after two keys in brackets that differ' | ['a.b.c': 'new', 'a.b[c]': 'old', 'a[b.c]': 'new'] | [{ NavigableMap m -> m.merge(['a[b.c]': 'new'], true) }, { NavigableMap m -> m.merge(['a.b[c]': 'old'], true) }, { NavigableMap m -> m.merge(['a.b.c': 'new'], true) }]
267+
'a key with a dot in brackets' | ['a[b.c]': 'x'] | [{ NavigableMap m -> m.merge(['a[b.c]': 'x'], true) }]
268+
'a key with a dot in brackets beside the nested path it spells' | ['a.b.c': 'x', 'a[b.c]': 'x'] | [{ NavigableMap m -> m.merge(['a.b.c': 'x'], true) }, { NavigableMap m -> m.merge(['a[b.c]': 'x'], true) }]
269+
'a key with a dot in brackets merged after a dotted key put' | ['a.b.c': 'x', 'a[b.c]': 'x'] | [{ NavigableMap m -> m.put('a.b.c', 'x') }, { NavigableMap m -> m.merge(['a[b.c]': 'x'], true) }]
270+
'a dotted key put after a key with a dot in brackets' | ['a.b.c': 'x', 'a[b.c]': 'x'] | [{ NavigableMap m -> m.merge(['a[b.c]': 'x'], true) }, { NavigableMap m -> m.put('a.b.c', 'x') }]
271+
'a key with a dot in brackets whose first part holds a scalar' | ['routes[/v1]': 'one', 'routes[/v1.0/**]': 'two'] | [{ NavigableMap m -> m.merge(['routes[/v1]': 'one'], true) }, { NavigableMap m -> m.merge(['routes[/v1.0/**]': 'two'], true) }]
272+
'a key with a dot in brackets whose first part holds a map' | ['routes./v1.0/**': 'two', 'routes[/v1.0/**]': 'two'] | [{ NavigableMap m -> m.merge(['routes./v1.0/**': 'two'], true) }, { NavigableMap m -> m.merge(['routes[/v1.0/**]': 'two'], true) }]
273+
'a key with a dot in brackets whose dotted start holds a map' | ['a.b.c': 'x', 'a[b.c]': 'x'] | [{ NavigableMap m -> m.put('a.b', [c: 'x']) }, { NavigableMap m -> m.merge(['a[b.c]': 'x'], true) }]
274+
'a map put at the dotted start of a key with a dot in brackets' | ['a.b.c': 'x', 'a[b.c]': 'x'] | [{ NavigableMap m -> m.merge(['a[b.c]': 'x'], true) }, { NavigableMap m -> m.put('a.b', [c: 'x']) }]
275+
}
211276
}

‎grails-bootstrap/src/test/groovy/grails/config/NavigableMapSpec.groovy‎

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,69 @@ class NavigableMapSpec extends Specification {
115115
output['rabbitmq.connections']['foo'].password == 'guest'
116116
}
117117

118+
def "an indexed key with a property after the index builds the list in the nested map, written #description"() {
119+
given:
120+
NavigableMap output = new NavigableMap()
121+
122+
when:
123+
output.merge(input, true)
124+
125+
then: 'the nested map holds the list, so navigation reads it'
126+
output.navigate(path as String[]) == [[name: 'row', port: '8080']]
127+
128+
where:
129+
description | input | path
130+
'as a dotted key' | ['a.rows[0].name': 'row', 'a.rows[0].port': '8080'] | ['a', 'rows']
131+
'as a dotted key two deep' | ['a.b.rows[0].name': 'row', 'a.b.rows[0].port': '8080'] | ['a', 'b', 'rows']
132+
}
133+
134+
def "a map written by an indexed key with a property after the index keeps its entries when a dotted key is merged after it"() {
135+
given:
136+
NavigableMap output = new NavigableMap()
137+
138+
when:
139+
output.merge(['a.mappings[foo].name': 'x'], true)
140+
output.merge(['a.mappings.bar': 'y'], true)
141+
142+
then:
143+
output.navigate('a', 'mappings') == [foo: [name: 'x'], bar: 'y']
144+
}
145+
146+
def "a list replaced after an indexed key wrote it gets no old element back from a later indexed key, replaced #description"() {
147+
given:
148+
NavigableMap output = new NavigableMap()
149+
150+
when:
151+
output.merge(['a.rows[0].name': 'old'], true)
152+
output.merge(replacement, true)
153+
output.merge(['a.rows[0].port': '8080'], true)
154+
155+
then:
156+
output.navigate('a', 'rows') == [[name: 'new', port: '8080']]
157+
output.get('a.rows') == [[name: 'new', port: '8080']]
158+
159+
where:
160+
description | replacement
161+
'as a dotted key' | ['a.rows': [[name: 'new']]]
162+
'in a nested map' | [a: [rows: [[name: 'new']]]]
163+
}
164+
165+
def "an indexed key written into a map inside an element keeps the other entries of that map"() {
166+
given:
167+
NavigableMap output = new NavigableMap()
168+
169+
when:
170+
output.merge(['a.rows': [[db: [url: 'jdbc:new', user: 'me']]]], true)
171+
output.merge(['a.rows[0].db.port': '8080'], true)
172+
173+
then:
174+
output.navigate('a', 'rows')[0].db == [url: 'jdbc:new', user: 'me', port: '8080']
175+
176+
and: 'the flat keys of the element and of the map inside it hold the maps the list holds'
177+
output.get('a.rows[0]').is(output.navigate('a', 'rows')[0])
178+
output.get('a.rows[0].db').is(output.navigate('a', 'rows')[0].db)
179+
}
180+
118181
def "a dotted key inside a nested map is merged under the path of that map"() {
119182
given:
120183
NavigableMap output = new NavigableMap()
@@ -173,4 +236,136 @@ class NavigableMapSpec extends Specification {
173236
!output.containsKey('org.springframework')
174237
!output.keySet().contains('org.springframework')
175238
}
239+
240+
def "a dotted property after a map subscript is written into the nested map the subscript names, written #description"() {
241+
given:
242+
NavigableMap output = new NavigableMap()
243+
244+
when:
245+
output.merge(written, true)
246+
output.merge(['a[b].c.d': 'new'], true)
247+
248+
then: 'the nested map and its flat key hold the new value'
249+
output.navigate('a', 'b', 'c', 'd') == 'new'
250+
output.get('a.b.c.d') == 'new'
251+
252+
and: 'the flattened config holds it once, under the bracketed name, and the old value nowhere'
253+
output.toFlatConfig().get('a[b].c.d') == 'new'
254+
!output.toFlatConfig().containsKey('a.b.c.d')
255+
!output.toFlatConfig().containsValue('old')
256+
257+
where:
258+
description | written
259+
'after a dotted key' | ['a.b.c.d': 'old']
260+
'after a nested map' | [a: [b: [c: [d: 'old']]]]
261+
}
262+
263+
def "a field of a map key in brackets written again holds the last value written, #key, written #description"() {
264+
given:
265+
NavigableMap output = new NavigableMap()
266+
267+
when:
268+
output.merge([("a.mappings[${key}].maxAge".toString()): '10', ("a.mappings[${key}].allowCredentials".toString()): 'true'], true)
269+
output.merge([("a.mappings[${key}].maxAge".toString()): value], true)
270+
271+
then: 'the held map, the flat key of the map and the flat key of the field all hold the last value'
272+
output.navigate('a', 'mappings')[key].maxAge == value
273+
output.navigate('a', 'mappings')[key].allowCredentials == 'true'
274+
output.get('a.mappings')[key].maxAge == value
275+
output.get("a.mappings[${key}].maxAge".toString()) == value
276+
277+
and: 'the properties hold the last value under every key of the field, and none when it is null'
278+
output.toProperties().findAll { it.key.toString().endsWith('maxAge') }.values().toSet() == (value == null ? [] : [value]).toSet()
279+
280+
where:
281+
[key, description, value] << [['/api/**', '/api/v1.0/**'], [['as a value', '20'], ['as null', null]]].combinations().collect { String key, List row ->
282+
[key, row[0], row[1]]
283+
}
284+
}
285+
286+
def "a field of a map key in brackets written as null after the map was #description holds the null, #key"() {
287+
given:
288+
NavigableMap output = new NavigableMap()
289+
290+
when:
291+
output.merge([("a.mappings[${key}].maxAge".toString()): '10'], true)
292+
output.merge(replacement, true)
293+
output.merge([("a.mappings[${key}].maxAge".toString()): null, ("a.mappings[${key}].allowCredentials".toString()): 'true'], true)
294+
295+
then: 'the held map holds the null and the field written beside it, and not the value written before the replacement'
296+
output.navigate('a', 'mappings')[key] == [maxAge: null, allowCredentials: 'true']
297+
output.get('a.mappings')[key] == [maxAge: null, allowCredentials: 'true']
298+
output.get("a.mappings[${key}].maxAge".toString()) == null
299+
!output.toProperties().keySet().any { it.toString().endsWith('maxAge') }
300+
301+
where:
302+
[key, description, replacement] << [['/api/**', '/api/v1.0/**'], ['replaced with a value', 'cleared']].combinations().collect { String key, String description ->
303+
[key, description, description == 'cleared' ? ['a.mappings': null] : ['a.mappings': [(key): 'one']]]
304+
}
305+
}
306+
307+
def "a key with a second subscript right after the first, #key, builds no container in the nested map"() {
308+
given:
309+
NavigableMap output = new NavigableMap()
310+
311+
when:
312+
output.merge([(key): 'x'], true)
313+
314+
then: 'the nested map holds no container, and the flat key holds the value under the first subscript, as before'
315+
output.navigate('a', 'grid') == null
316+
output.get('a.grid')[first].values() as List == ['x']
317+
318+
where:
319+
key | first
320+
'a.grid[0][1].name' | 0
321+
'a.grid[k][1].name' | 'k'
322+
}
323+
324+
def "an indexed key with a property after the index writes a map over a value of a list that is not a map"() {
325+
given:
326+
NavigableMap output = new NavigableMap()
327+
328+
when:
329+
output.merge(['a.rows': ['one', 'two']], true)
330+
output.merge(['a.rows[0].name': 'first', 'a.rows[1].name': 'second'], true)
331+
332+
then:
333+
output.navigate('a', 'rows') == [[name: 'first'], [name: 'second']]
334+
output.navigate('a', 'rows')[1].name == 'second'
335+
}
336+
337+
def "an element rebuilt after the list was #description holds only the fields written since"() {
338+
given:
339+
NavigableMap output = new NavigableMap()
340+
341+
when:
342+
output.merge(first, true)
343+
output.merge(replacement, true)
344+
writes.each { Map write -> output.merge(write, true) }
345+
346+
then:
347+
output.navigate('a', 'rows') == expected
348+
output.get('a.rows') == expected
349+
350+
where:
351+
description | first | replacement | writes | expected
352+
'replaced with values' | ['a.rows[1].obsolete': 'old'] | ['a.rows': ['one', 'two']] | [['a.rows[0].name': 'first'], ['a.rows[1].name': 'second']] | [[name: 'first'], [name: 'second']]
353+
'replaced, then written' | ['a.rows[1].obsolete': 'old'] | ['a.rows': ['one', 'two']] | [['a.rows[1].name': 'second']] | ['one', [name: 'second']]
354+
'cleared' | ['a.rows[1].obsolete': 'old'] | ['a.rows': null] | [['a.rows[0].port': '8080'], ['a.rows[1].name': 'second']] | [[port: '8080'], [name: 'second']]
355+
'cleared, same element' | ['a.rows[0].name': 'old'] | ['a.rows': null] | [['a.rows[0].port': '8080']] | [[port: '8080']]
356+
}
357+
358+
def "a map written by subscript and then replaced holds only the fields written since in its entry"() {
359+
given:
360+
NavigableMap output = new NavigableMap()
361+
362+
when:
363+
output.merge(['a.routes[x].obsolete': 'old'], true)
364+
output.merge(['a.routes': [x: 'one']], true)
365+
output.merge(['a.routes[x].name': 'second'], true)
366+
367+
then:
368+
output.navigate('a', 'routes') == [x: [name: 'second']]
369+
output.get('a.routes') == [x: [name: 'second']]
370+
}
176371
}

‎grails-core/src/main/groovy/org/grails/config/CompositeConfig.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
* A {@link Config} composed of other Configs. A lookup answers from the first config, in order of precedence,
4242
* that has the key, and the {@link Map} views, {@link #flatten()} and {@link #toProperties()} resolve a key
4343
* shared by several configs the same way, so each key appears once with its highest-precedence value.
44+
* <p>
45+
* One case differs: {@link #toProperties()} combines the names each config exports, and a config leaves out the
46+
* dotted name that only repeats one of its own bracketed names; {@link #flatten()} combines the maps the configs
47+
* store, which never hold that dotted name. So when a higher config spells a property in brackets,
48+
* {@code app.routes[k]}, and a lower one dotted, {@code app.routes.k}, the dotted name keeps the lower value there,
49+
* while {@link #getProperty(String)} answers the higher one.
4450
*
4551
* @author Graeme Rocher
4652
* @since 3.0

0 commit comments

Comments
 (0)