-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes to ensure if no object is opened user won't be able to write property name #2475
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this looks great! I have some minor comments. I'm also going to try running this against all of Google's internal tests, though I don't expect that will find any problems.
@@ -498,6 +498,10 @@ public JsonWriter name(String name) throws IOException { | |||
if (stackSize == 0) { | |||
throw new IllegalStateException("JsonWriter is closed."); | |||
} | |||
// As currently in any valid case name function cannot be called if stack top has these 2 statuses | |||
if (stackSize == 1 && ( peek() == EMPTY_DOCUMENT || peek() == NONEMPTY_DOCUMENT )) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nit: there shouldn't be a space after (
or before )
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my bad, corrected that, thanks for pointing out
jsonWriter.endArray(); // End the array | ||
jsonWriter.endObject(); // End the outer object | ||
assertThrows(IllegalStateException.class, () -> jsonWriter.name("this_throw_exception_as_all_objects_are_closed")); | ||
assertThrows(IllegalStateException.class, () -> jsonWriter.value("this_throw_exception_as_only_one_top_level_entry")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little uneasy at the assumption that the previous statement hasn't changed the state of jsonWriter
. I think it might be better to move the preceding setup into a private method (that maybe returns a JsonWriter
) and then use that method in two different test methods, one for .name
and one for .value
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree with you regarding status change and the possible impact of it, created a private method to return jsonwritter as you recommended, and running both tests with 2 separate objects
} catch (IllegalStateException expected) { | ||
assertThat(expected).hasMessageThat().isEqualTo("Nesting problem."); | ||
assertThrows(IllegalStateException.class, () -> jsonWriter.name("hello")); | ||
//removed nested exception test on jsonwriter.writevalue as it seems currently valid case per code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you are right about removing that test, but now that you have recorded that, can you remove this comment? It will be confusing for future readers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense, reading this comment will be odd for users as code is already gone, thanks for pointing it out, removed this comment
@@ -498,6 +498,10 @@ public JsonWriter name(String name) throws IOException { | |||
if (stackSize == 0) { | |||
throw new IllegalStateException("JsonWriter is closed."); | |||
} | |||
// As currently in any valid case name function cannot be called if stack top has these 2 statuses |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this comment a bit confusing. I think it isn't really necessary anyway? It doesn't really tell the reader anything that they can't already see from the following statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it! removed this unecessary comment
Thank you for reviewing the code! I'll look forward to the test results. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing this! I ran it against all of Google's internal tests and did not see any problems.
Thanks for the approval :) |
Thanks @shivam-sehgal for your work on this! Unfortunately I think my original issue made it look like this only affected the top level (which is addressed by your pull request), but it actually also affects trying to use I have created #2476 to (hopefully) fully resolve this. It reverts some of your test changes because handling of multiple top-level values was already covered by multiple separate test methods, and while it is generally good that your method |
…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
Purpose
Currently, even if the no object is opened or begin object is not called or all open objects are ended using jsonwriter.endObject() Still user is able to add json object property name, which shouldn't be happening
Closes #2407
Description
Currently when the Jsonwriter name method is called no check is there to identify if it's an empty document or all open objects are closed and stack size reaches 1 and the top element is NON_EMPTY_DOC basically the two case
In both of these invalid cases, the stack size should be 1 as per the current implementation
In the first case, stack will be having one status only which will be EMPTY_DOCUMENT
In the second case, all other statuses will be popped from the stack and only one status NONEMPTY_DOCUMENT will be present in the stack
So added a check in the name method for these 2 special cases
Also added and changed the unit test as per the requirement
Checklist
null
@since $next-version$
(
$next-version$
is a special placeholder which is automatically replaced during release)TestCase
)mvn clean verify javadoc:jar
passes without errors