Skip to content

Commit

Permalink
Correct order of fallbacks and overrides (#1786)
Browse files Browse the repository at this point in the history
  • Loading branch information
mproch committed Jun 17, 2021
1 parent 9894fd6 commit f700068
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
Expand Up @@ -71,11 +71,8 @@ abstract class ModelConfigLoader extends Serializable {
and have baseUrl taken from application config
*/
val configFallbackFromModel = ConfigFactory.parseResources(classLoader, modelConfigResource)
inputConfig
.withFallback(configFallbackFromModel)
//this is for reference.conf resources from model jar, we don't want to load application.conf etc.
.withFallback(ConfigFactoryExt.loadEmptyConfigWithOverrides(classLoader))
.resolve()
//We want to respect overrides (like system properties) and standard fallbacks (like reference.conf)
ConfigFactory.load(classLoader, inputConfig.withFallback(configFallbackFromModel)).resolve()
}

//only for testing
Expand Down
Expand Up @@ -28,12 +28,9 @@ object ConfigFactoryExt {
load(locations, classLoader)
}

def loadEmptyConfigWithOverrides(classLoader: ClassLoader): Config = ConfigFactory.load(classLoader, ConfigFactory.empty())

def load(resources: List[URI], classLoader: ClassLoader): Config =
resources
ConfigFactory.load(classLoader, resources
.map(ConfigFactoryExt.parseUri)
.reverse
.foldLeft(ConfigFactory.empty())(_.withFallback(_))
.withFallback(loadEmptyConfigWithOverrides(classLoader))
.foldLeft(ConfigFactory.empty())(_.withFallback(_)))
}
Expand Up @@ -5,10 +5,27 @@ import org.scalatest.{FunSuite, Matchers}

import java.net.URI
import java.nio.file.Files
import java.util.UUID
import scala.jdk.CollectionConverters.mapAsJavaMapConverter

class ConfigFactoryExtSpec extends FunSuite with Matchers {

//The same mechanism is used with config.override_with_env_var
test("should preserve config overrides") {
val randomPropertyName = UUID.randomUUID().toString

val conf1 = writeToTemp(Map(randomPropertyName -> "default"))

val result = try {
System.setProperty(randomPropertyName, "I win!")
ConfigFactoryExt.load(conf1.toString, getClass.getClassLoader)
} finally {
System.getProperties.remove(randomPropertyName)
}

result.getString(randomPropertyName) shouldBe "I win!"
}

test("loads in correct order") {

val conf1 = writeToTemp(Map("f1" -> "default", "f2" ->"not so default", "akka.http.server.request-timeout" -> "300s"))
Expand Down

0 comments on commit f700068

Please sign in to comment.