diff --git a/config/src/main/java/com/typesafe/config/impl/ConfigNodeObject.java b/config/src/main/java/com/typesafe/config/impl/ConfigNodeObject.java index cbb98562..17c5b291 100644 --- a/config/src/main/java/com/typesafe/config/impl/ConfigNodeObject.java +++ b/config/src/main/java/com/typesafe/config/impl/ConfigNodeObject.java @@ -69,7 +69,15 @@ final class ConfigNodeObject extends ConfigNodeComplexValue { } } else if (key.equals(desiredPath)) { seenNonMatching = true; - childrenCopy.set(i, node.replaceValue(value)); + AbstractConfigNodeValue indentedValue; + AbstractConfigNode before = i - 1 > 0 ? childrenCopy.get(i - 1) : null; + if (value instanceof ConfigNodeComplexValue && + before instanceof ConfigNodeSingleToken && + Tokens.isIgnoredWhitespace(((ConfigNodeSingleToken) before).token())) + indentedValue = ((ConfigNodeComplexValue) value).indentText(before); + else + indentedValue = value; + childrenCopy.set(i, node.replaceValue(indentedValue)); valueCopy = null; } else if (desiredPath.startsWith(key)) { seenNonMatching = true; diff --git a/config/src/test/scala/com/typesafe/config/impl/ConfigDocumentTest.scala b/config/src/test/scala/com/typesafe/config/impl/ConfigDocumentTest.scala index 05d2d964..b4a571ba 100644 --- a/config/src/test/scala/com/typesafe/config/impl/ConfigDocumentTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/ConfigDocumentTest.scala @@ -413,4 +413,17 @@ class ConfigDocumentTest extends TestUtils { assertEquals("a { b {\n c: d\n}, e : f }", configDocument.setValue("a.e", "f").render()) } + @Test + def configDocumentIndentationReplacingWithMultiLineValue { + var origText = "a {\n b {\n c : 22\n }\n}" + var configDocument = ConfigDocumentFactory.parseString(origText) + + assertEquals("a {\n b {\n c : {\n d:e\n }\n }\n}", configDocument.setValue("a.b.c", "{\n d:e\n}").render()) + + origText = "a {\n b {\n f : 10\n c : 22\n }\n}" + configDocument = ConfigDocumentFactory.parseString(origText) + + assertEquals("a {\n b {\n f : 10\n c : {\n d:e\n }\n }\n}", configDocument.setValue("a.b.c", "{\n d:e\n}").render()) + } + }