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

BsonReaderDecoder throws BsonInvalidOperationException when skipping an object or array value #371

Closed
rohitkumbhar opened this issue Feb 15, 2023 · 0 comments

Comments

@rohitkumbhar
Copy link
Contributor

Expected Behavior

BsonReaderDecoder should be able to skip any non-primitive value

Actual Behaviour

BsonReaderDecoder gets an exception when trying to skip an object or array value

readStartDocument can only be called when State is VALUE, not when State is END_OF_DOCUMENT.
org.bson.BsonInvalidOperationException: readStartDocument can only be called when State is VALUE, not when State is END_OF_DOCUMENT.
	at app//org.bson.AbstractBsonReader.throwInvalidState(AbstractBsonReader.java:668)
	at app//org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:686)
	at app//org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:721)
	at app//org.bson.AbstractBsonReader.readStartDocument(AbstractBsonReader.java:449)
	at app//io.micronaut.serde.bson.BsonReaderDecoder.nextToken(BsonReaderDecoder.java:99)
	at app//io.micronaut.serde.support.AbstractStreamDecoder.skipValue(AbstractStreamDecoder.java:844)
	at io.micronaut.serde.bson.BsonReaderDecoderJsonSpec.skip unknown object decode(BsonReaderDecoderJsonSpec.groovy:150)

Steps To Reproduce

Failing test:

 def 'skip unknown object decode'() {
        given:
        // data has a key "nested" with an object as a value 
        def data = '{ "name": "Foo", "nested": {"key": "value"}}'
        when:
        def w = createDecoder(data).decodeObject()
        then:

        while (true) {
            def key = w.decodeKey()
            if (key == "nested") {
                // Attempt to skip nested
                w.skipValue()
                break
            } else {
                w.decodeString()
            }
        }
    }

The skipValue() call causes the exception above.
NOTE: If I change the value type of the nested key to a String, the skipValue() call does not throw an exception

Environment Information

No response

Example Application

No response

Version

micronaut-serde-bson 1.3.3

graemerocher pushed a commit that referenced this issue Feb 15, 2023
The value of `currentToken` is used to read the next token. The MongoDB BSON reader consumes the full value and sets the internal state to TYPE.
This change sets the `currentToken` to `null` so the next token can be read correctly
rohitkumbhar added a commit to rohitkumbhar/micronaut-serialization that referenced this issue Feb 28, 2023
…pping a value (micronaut-projects#372)

The value of `currentToken` is used to read the next token. The MongoDB BSON reader consumes the full value and sets the internal state to TYPE.
This change sets the `currentToken` to `null` so the next token can be read correctly

(cherry picked from commit d874b7a)
graemerocher pushed a commit that referenced this issue Mar 1, 2023
… (#388)

The value of `currentToken` is used to read the next token. The MongoDB BSON reader consumes the full value and sets the internal state to TYPE.
This change sets the `currentToken` to `null` so the next token can be read correctly

(cherry picked from commit d874b7a)
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

No branches or pull requests

2 participants