Configuration properties are not EnvironmentAware if loaded as MapPropertySource
- Dominant language
- Groovy
- Stars
- 2.9k
- Forks
- 975
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 92
Description
- [x] Steps to reproduce provided
- [x] Stacktrace (if present) provided
- [x] Example that reproduces the problem uploaded to Github
- [x] Full description of the issue provided (see below)
It looks like the issue is happening because of [`initializeFromPropertySources`](https://github.com/grails/grails-core/blob/master/grails-core/src/main/groovy/org/grails/config/PropertySourcesConfig.java#L81) where the `MapPropertySource` added as `EnvironmentAwarePropertySource` but it is overridden due to [order of `propertySources `](https://github.com/grails/grails-core/blob/master/grails-core/src/main/groovy/org/grails/config/PropertySourcesConfig.java#L95).
### Steps to Reproduce
1. Create a new Grails 3.3.8 application.
2. Create a unit test as follows:
```
class DemoSpec extends Specification {
void "test dynamic configurations"() {
given:
MutablePropertySources mutablePropertySources = new MutablePropertySources()
mutablePropertySources.addFirst(new MapPropertySource('TestConfig', [
'dataSource.dbCreate' : '',
'dataSource.url' : 'jdbc:h2:mem:testDb',
'dataSource.username' : 'sa',
'dataSource.password' : '',
'dataSource.driverClassName' : Driver.name,
'environments.other.dataSource.url': 'jdbc:h2:mem:otherDb',
]))
ConfigMap config
when:
System.setProperty(Environment.KEY, Environment.DEVELOPMENT.name)
config = new PropertySourcesConfig(mutablePropertySources)
then:
Environment.current.name == 'development'
config.getProperty('dataSource.url') == 'jdbc:h2:mem:testDb'
when:
System.setProperty(Environment.KEY, 'other')
Environment.reset()
config = new PropertySourcesConfig(config.getPropertySources())
then:
Environment.current.name == 'other'
config.getProperty('dataSource.url') == 'jdbc:h2:mem:otherDb'
}
}
```
### Expected Behaviour
The test should pass and the `dataSource.url` config property value should be based on the current environment.
### Actual Behaviour
The value of `dataSource.url` is always `'jdbc:h2:mem:testDb'`.
### Environment Information
- **Operating System**: macOS
- **Grails Version:** 3.3.7
- **JDK Version:** 1.8
### Example Application
- https://github.com/puneetbehl/bugs-dynamic-grails-config
Contributor guide
Research direction
Start in grails-core/grails-core/src/main/groovy/org/grails/config/PropertySourcesConfig.groovy, especially initializeFromPropertySources and the propertySources ordering described in the issue. Run the supplied DemoSpec reproduction and inspect how the MapPropertySource is retained across environment changes. Done means the test passes and dataSource.url reflects the current environment instead of remaining jdbc:h2:mem:testDb.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100