Skip to content

Commit

Permalink
fix endless parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
a10zn8 committed Mar 1, 2024
1 parent 10cbb99 commit 3c475c6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,29 @@ abstract class ResponseParser<T> {
break
}
val token = parser.nextToken()
if (token == JsonToken.START_OBJECT) {
inited = true
count++
continue
}
if (token == JsonToken.END_OBJECT) {
count--
continue
}
if (token == JsonToken.VALUE_NULL) {
// error is just null
return null
when (token) {
null -> {
return null
}
JsonToken.START_OBJECT -> {
inited = true
count++
continue
}
JsonToken.END_OBJECT -> {
count--
continue
}
JsonToken.VALUE_NULL -> {
return null
}
JsonToken.VALUE_STRING -> {
if (!inited) {
message = parser.valueAsString
break
}
}
else -> {}
}
val field = parser.currentName()
if (field == "code" && token == JsonToken.VALUE_NUMBER_INT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,16 @@ class ResponseRpcParserSpec extends Specification {
!act.hasResult()
}

def "Parse non object error"() {
setup:
def json = '{"jsonrpc": "2.0", "id": 1, "error": "plain error"}'
when:
def act = parser.parse(json.getBytes())
then:
act.error != null
act.error.message == "plain error"
act.hasError()
!act.hasResult()
}

}

0 comments on commit 3c475c6

Please sign in to comment.