From 919c46accc8d310d71c52f8086268a4022416b16 Mon Sep 17 00:00:00 2001 From: Preben Ingvaldsen Date: Tue, 30 Jun 2015 14:01:43 -0700 Subject: [PATCH] Fix index out of bounds error Fix an array index out of bounds error, which would occur if a complex value was inserted into an empty root in a ConfigDocument. --- .../java/com/typesafe/config/impl/ConfigNodeObject.java | 2 +- .../com/typesafe/config/impl/ConfigDocumentTest.scala | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) 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 39cb76f3..66c65f06 100644 --- a/config/src/main/java/com/typesafe/config/impl/ConfigNodeObject.java +++ b/config/src/main/java/com/typesafe/config/impl/ConfigNodeObject.java @@ -170,7 +170,7 @@ final class ConfigNodeObject extends ConfigNodeComplexValue { // If the value we're inserting is a complex value, we'll need to indent it for insertion AbstractConfigNodeValue indentedValue; - if (value instanceof ConfigNodeComplexValue) { + if (value instanceof ConfigNodeComplexValue && !indentation.isEmpty()) { indentedValue = ((ConfigNodeComplexValue) value).indentText(indentation.get(indentation.size() - 1)); } else { indentedValue = value; 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 aa7e2d4c..ae0a783e 100644 --- a/config/src/test/scala/com/typesafe/config/impl/ConfigDocumentTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/ConfigDocumentTest.scala @@ -436,6 +436,13 @@ class ConfigDocumentTest extends TestUtils { val configDocument = ConfigDocumentFactory.parseString(origText) assertEquals("a : 1", configDocument.withValueText("a", "1").render) + + val mapVal = ConfigValueFactory.fromAnyRef(Map("a" -> 1, "b" -> 2).asJava) + assertEquals("a : {\n # hardcoded value\n \"a\" : 1,\n # hardcoded value\n \"b\" : 2\n}", + configDocument.withValue("a", mapVal).render) + + val arrayVal = ConfigValueFactory.fromAnyRef(List(1, 2).asJava) + assertEquals("a : [\n # hardcoded value\n 1,\n # hardcoded value\n 2\n]", configDocument.withValue("a", arrayVal).render) } @Test