From 392cc65ff3004689e03b219d9b00979072f9c9d0 Mon Sep 17 00:00:00 2001 From: shivam-sehgal Date: Tue, 22 Aug 2023 22:42:31 +0530 Subject: [PATCH] Changes to ensure if no object is opened user won't be able to write property name (#2475) * Changes to ensure if no object is opened user won't be able to add prop in the object * review points * spelling correction --- .../com/google/gson/stream/JsonWriter.java | 3 ++ .../google/gson/stream/JsonWriterTest.java | 49 ++++++++++++++++--- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/gson/src/main/java/com/google/gson/stream/JsonWriter.java b/gson/src/main/java/com/google/gson/stream/JsonWriter.java index 07848b06..bb285bd0 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonWriter.java +++ b/gson/src/main/java/com/google/gson/stream/JsonWriter.java @@ -498,6 +498,9 @@ public class JsonWriter implements Closeable, Flushable { if (stackSize == 0) { throw new IllegalStateException("JsonWriter is closed."); } + if (stackSize == 1 && (peek() == EMPTY_DOCUMENT || peek() == NONEMPTY_DOCUMENT)) { + throw new IllegalStateException("Please begin an object before this."); + } deferredName = name; return this; } diff --git a/gson/src/test/java/com/google/gson/stream/JsonWriterTest.java b/gson/src/test/java/com/google/gson/stream/JsonWriterTest.java index ae4194d7..80bf15f6 100644 --- a/gson/src/test/java/com/google/gson/stream/JsonWriterTest.java +++ b/gson/src/test/java/com/google/gson/stream/JsonWriterTest.java @@ -28,6 +28,8 @@ import java.io.StringWriter; import java.math.BigDecimal; import java.math.BigInteger; import org.junit.Test; +import java.util.Arrays; +import java.util.List; @SuppressWarnings("resource") public final class JsonWriterTest { @@ -114,13 +116,17 @@ public final class JsonWriterTest { public void testInvalidTopLevelTypes() throws IOException { StringWriter stringWriter = new StringWriter(); JsonWriter jsonWriter = new JsonWriter(stringWriter); - jsonWriter.name("hello"); // TODO: This should throw, see https://github.com/google/gson/issues/2407 - try { - jsonWriter.value("world"); - fail(); - } catch (IllegalStateException expected) { - assertThat(expected).hasMessageThat().isEqualTo("Nesting problem."); - } + assertThrows(IllegalStateException.class, () -> jsonWriter.name("hello")); + } + + @Test + public void closeAllObjectsAndTryToAddElements() throws IOException { + JsonWriter jsonWriterForNameAddition = getJsonWriterWithObjects(); + assertThrows(IllegalStateException.class, () -> jsonWriterForNameAddition.name("this_throw_exception_as_all_objects_are_closed")); + jsonWriterForNameAddition.close(); + JsonWriter jsonWriterForValueAddition = getJsonWriterWithObjects(); + assertThrows(IllegalStateException.class, () -> jsonWriterForValueAddition.value("this_throw_exception_as_only_one_top_level_entry")); + jsonWriterForValueAddition.close(); } @Test @@ -973,4 +979,33 @@ public final class JsonWriterTest { + "}"; assertThat(stringWriter.toString()).isEqualTo(expected); } + + /** + * This method wites a json object and return a jsonwriter object + * that we can use for the testing purpose + * @return JsonWriter Object with nested object and an array + */ + private JsonWriter getJsonWriterWithObjects() throws IOException { + StringWriter stringWriter = new StringWriter(); + JsonWriter jsonWriter = new JsonWriter(stringWriter); + jsonWriter.beginObject(); + jsonWriter.name("a").value(20); + jsonWriter.name("age").value(30); + + // Start the nested "address" object + jsonWriter.name("address").beginObject(); + jsonWriter.name("city").value("New York"); + jsonWriter.name("country").value("USA"); + jsonWriter.endObject(); // End the nested "address" object + jsonWriter.name("random_prop").value(78); + // Add an array of phone numbers (list of numbers) + List phoneNumbers = Arrays.asList(1234567890, 98989, 9909); + jsonWriter.name("phoneNumbers").beginArray(); + for (Integer phoneNumber : phoneNumbers) { + jsonWriter.value(phoneNumber); + } + jsonWriter.endArray(); // End the array + jsonWriter.endObject(); // End the outer object + return jsonWriter; + } }