Skip to content
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 add pr… #2474

Closed
wants to merge 1 commit into from

Conversation

shivam-sehgal
Copy link
Contributor

…op in the object

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

  • The user calls jsonwrite.name() for the first time in an empty document or when the beginobject method has not been called.
  • The user calls jsonwrite.name() when all opened objects are closed.

In both of these invalid cases, the stack size should be 1 as per the current implementation
In the first case, we 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 requirement

Checklist

  • New code follows the Google Java Style Guide
  • If necessary, new public API validates arguments, for example rejects null
  • New public API has Javadoc
    • Javadoc uses @since $next-version$
      ($next-version$ is a special placeholder which is automatically replaced during release)
  • If necessary, new unit tests have been added
    • Assertions in unit tests use Truth, see existing tests
    • No JUnit 3 features are used (such as extending class TestCase)
    • If this pull request fixes a bug, a new test was added for a situation which failed previously and is now fixed
  • mvn clean verify javadoc:jar passes without errors

@google-cla
Copy link

google-cla bot commented Aug 21, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JsonWriter.name does not throw exception when not inside JSON object
1 participant