Skip to content

Commit

Permalink
fix input object value coercion bug, fixes #49
Browse files Browse the repository at this point in the history
This commit fixes input object value coercion,
a crash in validator, or to be precise in `nullableType`
of `ast.nim`. This commit also fixes empty input object parser
in `common_parser.nim`
  • Loading branch information
jangko committed Apr 27, 2021
1 parent 8c0fa5d commit 3643fc2
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 17 deletions.
2 changes: 1 addition & 1 deletion graphql/common/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ proc nullableType*(node: Node): bool =
case node[0].kind
of nkListType:
return true
of nkSym:
of nkSym, nkNamedType:
return false
else:
unreachable()
Expand Down
3 changes: 2 additions & 1 deletion graphql/common_parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@ proc objectField(q: var Parser, isConst: bool, field: var Node) =
proc objectVal(q: var Parser, isConst: bool, input: var Node) =
expect tokLCurly
input = newNode(nkInput)
if pfJsonCompatibility in q.flags and currToken == tokRCurly:
if currToken == tokRCurly:
# input object value can have empty fields
nextToken()
return
repeatUntil(q.conf.maxFields, tokRCurly):
Expand Down
112 changes: 98 additions & 14 deletions tests/execution/engine.toml
Original file line number Diff line number Diff line change
Expand Up @@ -650,18 +650,102 @@ fragment bird on Bird {
}
"""
result = """
{
"creatures":[
{
"__typename":"Tree",
"name":"redwood",
"age":100
},
{
"__typename":"Bird",
"name":"parrot",
"color":"RED"
}
]
}
{
"creatures":[
{
"__typename":"Tree",
"name":"redwood",
"age":100
},
{
"__typename":"Bird",
"name":"parrot",
"color":"RED"
}
]
}
"""

[[units]]
name = "graphql-spec#793 case #1"
opName = "A"
code = """
type Query {
example(inputObject: ExampleInputObject! = {}): Int
}
input ExampleInputObject {
number: Int! = 3
}
query A {
example
}
"""
result = """
{
"example":3
}
"""

[[units]]
name = "graphql-spec#793 case #2"
opName = "B"
code = """
type Query {
example(inputObject: ExampleInputObject! = {}): Int
}
input ExampleInputObject {
number: Int! = 3
}
query B {
example(inputObject: {})
}
"""
result = """
{
"example":3
}
"""

[[units]]
name = "graphql-spec#793 case #3"
opName = "C"
code = """
type Query {
example(inputObject: ExampleInputObject! = {}): Int
}
input ExampleInputObject {
number: Int! = 3
}
query C {
example(inputObject: { number: 3 })
}
"""
result = """
{
"example":3
}
"""

[[units]]
name = "graphql-spec#793 case #4"
opName = "D"
code = """
type Query {
example(inputObject: ExampleInputObject! = {}): Int
}
input ExampleInputObject {
number: Int! = 3
}
query D($inputObject: ExampleInputObject! = {}) {
example(inputObject: $inputObject)
}
"""
result = """
{
"example":3
}
"""
8 changes: 7 additions & 1 deletion tests/test_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ proc querySearchImpl(ud: RootRef, params: Args, parent: Node): RespResult {.apiP
list.add respMap(tc.names[tnStarship])
ok(list)

proc queryExampleImpl(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
let obj = params[0].val
let number = obj[0][1]
ok(number)

const queryProtos = {
"name": queryNameImpl,
"color": queryColorImpl,
Expand All @@ -89,7 +94,8 @@ const queryProtos = {
"echoArg": queryEchoArgImpl,
"checkFruit": queryCheckFruit,
"creatures": queryCreatures,
"search": querySearchImpl
"search": querySearchImpl,
"example": queryExampleImpl
}

proc creatureNameImpl(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
Expand Down

0 comments on commit 3643fc2

Please sign in to comment.