config
config copied to clipboard
Q: Replace in parent not possible?
Hi,
I am using typesafe.config:1.3.1 in specific way:
final ConfigResolveOptions resolveOptions = ConfigResolveOptions.defaults()
.setAllowUnresolved(true)
.setUseSystemEnvironment(false);
final Config child = ConfigFactory.parseString("child : ${parent} {var1 = ${var3}}");
final Config parent = ConfigFactory.parseString("parent {var1 = somevar1, var2 = ${var4}}");
final Config substitution = ConfigFactory.parseString("var3 = somevar3, var4 = somevar4");
final Config resolved = child.resolveWith(parent, resolveOptions)
.resolveWith(substitution, resolveOptions);
I expect the resolved should be: child {var1 = somevar3, var2 = somevar4}
But I got:
Exception in thread "main" com.typesafe.config.ConfigException$BugOrBroken: replace in parent not possible ConfigDelayedMerge("somevar1",${var3}) with Unquoted("somevar1") in ResolveSource(root=SimpleConfigObject({"var3":"somevar3","var4":"somevar4"}), pathFromRoot=null)
Is it the expected behavior?
Thanks
BugOrBroken exception is essentially an assertion failure; this one looks like it's from https://github.com/typesafehub/config/blob/f6680a5dad51d992139d45a84fad734f1778bf50/config/src/main/java/com/typesafe/config/impl/ResolveSource.java#L244-L247
I think it's probably the same issue described here: https://github.com/typesafehub/config/issues/332
Could you suggest some workaround for this?
https://github.com/typesafehub/config/issues/332#issuecomment-127078621 is all I really know about it; I don't have this code loaded in my head right now, it's been a couple years. One possible fix could be to put the Config used in resolveWith inside the config being resolved somehow, instead of having them completely separate. It might help even if it's inside at some useless path.
Okay.
Thank you.
I ran into this issue as well and found that the workaround @havocp recommended will work.
If you put the source config in the tree at a temporary path, I imagine you could remove it again after the resolveWith even.
I found the following workaround helped in my case, to resolve against system parameters:
File confFile = new File("...");
Config config = ConfigFactory.parseFile(confFile);
Config system = ConfigFactory.systemProperties();
Config finale = config.withFallback(system).resolve();
In my case this made the following config work as I expect and fallback to explicit defaults as well as interpolate system properties if present:
app {
component {
hostname = localhost ; default
hostname = ${?some.system.property.hostname} ; override if present
}
}