Skip to content

Commit 5db49f0

Browse files
shivam-sehgaltibor-universe
authored andcommitted
Changes to ensure if no object is opened user won't be able to write property name (google#2475)
* Changes to ensure if no object is opened user won't be able to add prop in the object * review points * spelling correction
1 parent 6c24a87 commit 5db49f0

2 files changed

Lines changed: 45 additions & 7 deletions

File tree

gson/src/main/java/com/google/gson/stream/JsonWriter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,9 @@ public JsonWriter name(String name) throws IOException {
498498
if (stackSize == 0) {
499499
throw new IllegalStateException("JsonWriter is closed.");
500500
}
501+
if (stackSize == 1 && (peek() == EMPTY_DOCUMENT || peek() == NONEMPTY_DOCUMENT)) {
502+
throw new IllegalStateException("Please begin an object before this.");
503+
}
501504
deferredName = name;
502505
return this;
503506
}

gson/src/test/java/com/google/gson/stream/JsonWriterTest.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.math.BigDecimal;
2929
import java.math.BigInteger;
3030
import org.junit.Test;
31+
import java.util.Arrays;
32+
import java.util.List;
3133

3234
@SuppressWarnings("resource")
3335
public final class JsonWriterTest {
@@ -114,13 +116,17 @@ public void testTopLevelValueTypes() throws IOException {
114116
public void testInvalidTopLevelTypes() throws IOException {
115117
StringWriter stringWriter = new StringWriter();
116118
JsonWriter jsonWriter = new JsonWriter(stringWriter);
117-
jsonWriter.name("hello"); // TODO: This should throw, see https://github.com/google/gson/issues/2407
118-
try {
119-
jsonWriter.value("world");
120-
fail();
121-
} catch (IllegalStateException expected) {
122-
assertThat(expected).hasMessageThat().isEqualTo("Nesting problem.");
123-
}
119+
assertThrows(IllegalStateException.class, () -> jsonWriter.name("hello"));
120+
}
121+
122+
@Test
123+
public void closeAllObjectsAndTryToAddElements() throws IOException {
124+
JsonWriter jsonWriterForNameAddition = getJsonWriterWithObjects();
125+
assertThrows(IllegalStateException.class, () -> jsonWriterForNameAddition.name("this_throw_exception_as_all_objects_are_closed"));
126+
jsonWriterForNameAddition.close();
127+
JsonWriter jsonWriterForValueAddition = getJsonWriterWithObjects();
128+
assertThrows(IllegalStateException.class, () -> jsonWriterForValueAddition.value("this_throw_exception_as_only_one_top_level_entry"));
129+
jsonWriterForValueAddition.close();
124130
}
125131

126132
@Test
@@ -973,4 +979,33 @@ public void testIndentOverwritesFormattingStyle() throws IOException {
973979
+ "}";
974980
assertThat(stringWriter.toString()).isEqualTo(expected);
975981
}
982+
983+
/**
984+
* This method wites a json object and return a jsonwriter object
985+
* that we can use for the testing purpose
986+
* @return JsonWriter Object with nested object and an array
987+
*/
988+
private JsonWriter getJsonWriterWithObjects() throws IOException {
989+
StringWriter stringWriter = new StringWriter();
990+
JsonWriter jsonWriter = new JsonWriter(stringWriter);
991+
jsonWriter.beginObject();
992+
jsonWriter.name("a").value(20);
993+
jsonWriter.name("age").value(30);
994+
995+
// Start the nested "address" object
996+
jsonWriter.name("address").beginObject();
997+
jsonWriter.name("city").value("New York");
998+
jsonWriter.name("country").value("USA");
999+
jsonWriter.endObject(); // End the nested "address" object
1000+
jsonWriter.name("random_prop").value(78);
1001+
// Add an array of phone numbers (list of numbers)
1002+
List<Integer> phoneNumbers = Arrays.asList(1234567890, 98989, 9909);
1003+
jsonWriter.name("phoneNumbers").beginArray();
1004+
for (Integer phoneNumber : phoneNumbers) {
1005+
jsonWriter.value(phoneNumber);
1006+
}
1007+
jsonWriter.endArray(); // End the array
1008+
jsonWriter.endObject(); // End the outer object
1009+
return jsonWriter;
1010+
}
9761011
}

0 commit comments

Comments
 (0)