Skip to content

Commit

Permalink
Issue micronaut-projects#371: Sets the currentToken to null after ski…
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
rohitkumbhar committed Feb 28, 2023
1 parent 333d7d1 commit 429de97
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ protected Number getBestNumber() {
@Override
protected void skipChildren() {
bsonReader.skipValue();
currentToken = null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.micronaut.serde.bson


import io.micronaut.serde.Decoder
import io.micronaut.serde.exceptions.SerdeException
import org.bson.json.JsonReader
Expand Down Expand Up @@ -117,4 +118,73 @@ class BsonReaderDecoderJsonSpec extends Specification {
f4: [56, [f5: 'bar']]
]
}

def 'skip unknown string'() {
given:
def data = '{ "name": "Foo", "nested": "hello"}'
when:
def w = createDecoder(data).decodeObject()
then:

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

def 'can skip non-scalar values'() {
given:
// data has a key "nested" with an object as a value
def data = '{ "stringKey": "stringValue", "objectKey": {"key": "value"}, "stringKey2": "stringValue2", "arrayKey": [1,2,3], "intKey": 1}'
when:
def w = createDecoder(data).decodeObject()
def propCount = 0
def map = [:]
while (propCount < 5) {
propCount++
def key = w.decodeKey()
if (key == "objectKey" || key == "arrayKey") {
w.skipValue()
} else {
def val = w.decodeString()
map.put(key, val)
}
}
then:
map.size() == 3
map["objectKey"] == null
map["arrayKey"] == null
map["intKey"] == "1"
}

def 'can skip scalar values'() {
given:
def data = '{ "stringKey": "stringValue", "objectKey": {"key": "value"}, "stringKey2": "stringValue2", "arrayKey": [1,2,3], "intKey": 1}'
when:
def w = createDecoder(data).decodeObject()
def propCount = 0
def map = [:]
while (propCount < 5) {
propCount++
def key = w.decodeKey()
if (key == "stringKey" || key == "intKey") {
w.skipValue()
} else {
def val = w.decodeArbitrary()
map.put(key, val)
}
}
then:
map.size() == 3
map["objectKey"] == ["key": "value"]
map["arrayKey"] == [1, 2, 3]
map["stringKey2"] == "stringValue2"
map.get("intKey") == null
map.get("stringKey") == null
}
}

0 comments on commit 429de97

Please sign in to comment.