Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/main/java/graphql/scalars/object/ObjectScalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public Object parseLiteral(Value<?> input, CoercedVariables variables, GraphQLCo
"Expected AST type 'Value' but was '" + typeName(input) + "'."
);
}
if (input instanceof NullValue) {
return null;
}
if (input instanceof FloatValue) {
return ((FloatValue) input).getValue();
}
Expand Down Expand Up @@ -91,12 +94,7 @@ public Object parseLiteral(Value<?> input, CoercedVariables variables, GraphQLCo
List<ObjectField> values = ((ObjectValue) input).getObjectFields();
Map<String, Object> parsedValues = new LinkedHashMap<>();
values.forEach(fld -> {
Object parsedValue;
if (fld.getValue() instanceof NullValue) { // Nested NullValue inside ObjectValue
parsedValue = null;
} else {
parsedValue = parseLiteral(fld.getValue(), variables, graphQLContext, locale);
}
Object parsedValue = parseLiteral(fld.getValue(), variables, graphQLContext, locale);
parsedValues.put(fld.getName(), parsedValue);
});
return parsedValues;
Expand Down
67 changes: 67 additions & 0 deletions src/test/groovy/graphql/scalars/object/JsonScalarTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package graphql.scalars.object

import graphql.ExecutionInput
import graphql.GraphQL
import graphql.scalars.ExtendedScalars
import graphql.schema.idl.RuntimeWiring
import graphql.schema.idl.SchemaGenerator
import graphql.schema.idl.SchemaParser
import graphql.schema.idl.TypeRuntimeWiring
import spock.lang.Specification
import spock.lang.Unroll

class JsonScalarTest extends Specification {

GraphQL graphQL

def setup() {
def typeRegistry = new SchemaParser().parse('''
scalar JSON
type Query {
echo(value: JSON): JSON
}
''')
def wiring = RuntimeWiring.newRuntimeWiring()
.scalar(ExtendedScalars.Json)
.type(TypeRuntimeWiring.newTypeWiring("Query")
.dataFetcher("echo", { env -> env.getArgument("value") }))
.build()
def schema = new SchemaGenerator().makeExecutableSchema(typeRegistry, wiring)
graphQL = GraphQL.newGraphQL(schema).build()
}

@Unroll
def "accepts null values in JSON literal #literal"() {
when:
def result = graphQL.execute("{ echo(value: $literal) }")

then:
result.errors.empty
result.getData() == [echo: expected]

where:
literal | expected
'[null]' | [null]
'[1, null, 2]' | [1, null, 2]
'[null, 1, null]' | [null, 1, null]
'[[null]]' | [[null]]
'{items: [null, 1], value: null}' | [items: [null, 1], value: null]
'[{value: null}]' | [[value: null]]
'null' | null
}

def "accepts null list elements in JSON variables"() {
given:
def value = [items: [1, null, [null]], value: null]

when:
def result = graphQL.execute(ExecutionInput.newExecutionInput()
.query('query($value: JSON) { echo(value: $value) }')
.variables([value: value])
.build())

then:
result.errors.empty
result.getData() == [echo: value]
}
}
11 changes: 11 additions & 0 deletions src/test/groovy/graphql/scalars/object/ObjectScalarTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ObjectScalarTest extends AbstractScalarTest {
result == expectedResult
where:
input | expectedResult
mkNullValue() | null
mkStringValue("s") | "s"
mkFloatValue("99.9") | new BigDecimal("99.9")
mkIntValue(666) | 666
Expand All @@ -46,6 +47,12 @@ class ObjectScalarTest extends AbstractScalarTest {
mkArrayValue([
mkStringValue("s"), mkIntValue(666)
] as List<Value>) | ["s", 666]
mkArrayValue([
mkNullValue(), mkIntValue(1), mkNullValue()
] as List<Value>) | [null, 1, null]
mkArrayValue([
mkArrayValue([mkNullValue()] as List<Value>)
] as List<Value>) | [[null]]
}

@Unroll
Expand All @@ -69,6 +76,10 @@ class ObjectScalarTest extends AbstractScalarTest {
mkObjectValue([
field1: mkNullValue()
] as Map<String, Value>) | [field1: null] // Nested NullValue inside ObjectValue

mkObjectValue([
items: mkArrayValue([mkIntValue(1), mkNullValue()] as List<Value>)
] as Map<String, Value>) | [items: [1, null]]
}

@Unroll
Expand Down
Loading