codeconsole opened a new pull request, #16798:
URL: https://github.com/apache/grails-core/pull/16798
A placeholder in an element of a list is now resolved by
`grailsApplication.config`, as Spring's `Environment` resolves it:
```yaml
app:
origins:
- https://app.example.com
- '${EXTRA_ORIGIN:https://admin.example.com}'
rows:
- url: '${EXTRA_ORIGIN:https://admin.example.com}'
db:
host: '${DB_HOST:localhost}'
```
| | before | now, as the `Environment` |
|---|---|---|
| `config.getProperty('app.origins[1]')` | `${EXTRA_ORIGIN:…}` |
`https://admin.example.com` |
| `config.getProperty('app.origins', List)` | `[https://app.example.com,
${EXTRA_ORIGIN:…}]` | `[https://app.example.com, https://admin.example.com]` |
| `config.getProperty('app.rows[0].url')` | `${EXTRA_ORIGIN:…}` |
`https://admin.example.com` |
| `config.getProperty('app.rows[0].db.host')` | `${DB_HOST:localhost}` |
`localhost` |
| `config.navigate('app', 'rows')[0].db.host` | `${DB_HOST:localhost}` |
`localhost` |
The config's merge resolved placeholders only in string values. A list is
stored as it is given, and a value under an indexed key, such as
`app.origins[1]` or `app.rows[0].url`, is written into its list or map without
that step, so any list from `application.yml` read through Grails' YAML loader,
or from `application.groovy`, kept its placeholders, by index and in the whole
list. A list from a property source read through Spring Boot's YAML loader was
already resolved.
The merge now resolves the strings in a list, through the maps and lists it
holds, and a value or map written under an indexed key:
- A list or map without a placeholder is kept as given. Any other is copied,
so the property source keeps what it holds and resolves again when the config
is refreshed.
- A dotted key is written twice, under the whole key and then in the nested
map. Both writes take one resolution, so the flat key, the nested entry and the
export hold the same value, even for `${random.uuid}`.
Requested in
https://github.com/apache/grails-core/pull/16732#discussion_r4416718752. The
configuration guide's YAML page shows a placeholder in a list element.
### Tests
`PropertySourcesConfigSpec`:
- Reads `application.yml` through Grails' YAML loader into a config and into
a `StandardEnvironment` with `ConfigurationPropertySources` attached, and
checks that the config answers as the environment does: a list of values by
index and whole, and a list of maps with a map and a list inside an element.
- A list held whole, as `application.groovy` gives it, of values, maps and
lists, is resolved, and the property source keeps it as written.
- A list without a placeholder is held as the same instance.
- A list element merged in a nested map, as a dotted key and as an indexed
key, reads the source once, and every read agrees.
The new features fail on 7.0.x. `grails-core` (524) and
`grails-web-url-mappings` (162) pass.
Outside the specs, every property name of four sources was compared with the
environment: Grails' YAML loader, Spring Boot's YAML loader, a list held whole,
and a `MapPropertySource`. The shapes were strings, maps in lists, nested
lists, and a map and a list inside an element, read through `getProperty`,
`toProperties`, `navigate` and `getProperty(name, List)`. No placeholder
remains after the change. On 7.0.x, Grails' YAML loader and a list held whole
left them in 15 reads.
### Merging up
- The main code merges cleanly with #16771 and #16772.
`PropertySourcesConfigSpec` conflicts with both, in its imports and in the
features added before `CountingPropertySource`; take both sides. Merged with
#16771, all 37 features of the spec and `GrailsCorsConfigurationSpec` pass, and
a dotted value under an index (`a.rows[0].db.host`) still reads the source once.
- On 9.0.x, #16732 adds a sentence to the upgrade guide's section on
configuration keys, and to the `grails-9-upgrade` skill, saying that `config`
does not resolve a placeholder in a list element. Drop it when this reaches
9.0.x.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]