Skip to content
Open
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
35 changes: 27 additions & 8 deletions src/main/java/graphql/execution/ValuesResolverConversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ static Object valueToInternalValueImpl(
locale);
}
if (inputValueWithState.isExternal()) {
return externalValueToInternalValueImpl(
return externalValueToInternalValueWithOneOfCheck(
inputInterceptor,
fieldVisibility,
inputType,
inputValueWithState.getValue(),
graphqlContext,
locale,
0, Integer.MAX_VALUE);
Integer.MAX_VALUE);
}
return assertShouldNeverHappen("unexpected value state " + inputValueWithState);
}
Expand Down Expand Up @@ -399,6 +399,8 @@ static CoercedVariables externalValueToInternalValueForVariables(
CoercedVariables.emptyVariables(),
graphqlContext,
locale);
// checked here like a supplied value, arguments that reference the variable do not check it again
ValuesResolverOneOfValidation.validateOneOfInputTypes(variableInputType, coercedDefaultValue, defaultValue, locale);
coercedValues.put(variableName, coercedDefaultValue);
} else if (isNonNull(variableType) && (!hasValue || value == null)) {
throw new NonNullableValueCoercedAsNullException(variableDefinition, variableType);
Expand Down Expand Up @@ -439,8 +441,27 @@ static Object externalValueToInternalValueImpl(
GraphQLContext graphqlContext,
Locale locale
) throws NonNullableValueCoercedAsNullException, CoercingParseValueException {
return externalValueToInternalValueImpl(inputInterceptor, fieldVisibility, graphQLType,
originalValue, graphqlContext, locale, 0, maxInputDepth(graphqlContext));
return externalValueToInternalValueWithOneOfCheck(inputInterceptor, fieldVisibility, graphQLType,
originalValue, graphqlContext, locale, maxInputDepth(graphqlContext));
}

/**
* Coerces the whole external value and then checks the @oneOf rules once over the result, rather than
* walking every nested input object again at each level above it
*/
private static Object externalValueToInternalValueWithOneOfCheck(
InputInterceptor inputInterceptor,
GraphqlFieldVisibility fieldVisibility,
GraphQLInputType graphQLType,
Object originalValue,
GraphQLContext graphqlContext,
Locale locale,
int maxDepth
) throws NonNullableValueCoercedAsNullException, CoercingParseValueException {
Object coercedValue = externalValueToInternalValueImpl(inputInterceptor, fieldVisibility, graphQLType,
originalValue, graphqlContext, locale, 0, maxDepth);
ValuesResolverOneOfValidation.validateOneOfInputTypes(graphQLType, coercedValue, null, locale);
return coercedValue;
}

private static Object externalValueToInternalValueImpl(
Expand Down Expand Up @@ -518,8 +539,6 @@ private static Object externalValueToInternalValueImpl(
locale,
depth + 1,
maxDepth);

ValuesResolverOneOfValidation.validateOneOfInputTypes(inputObjectType, coercedMap, null, locale);
return coercedMap;
} else {
throw CoercingParseValueException.newCoercingParseValueException()
Expand Down Expand Up @@ -928,14 +947,14 @@ static Object defaultValueToInternalValue(
}
if (defaultValue.isExternal()) {
// The schema already validated the default's depth; request overrides only bound supplied values.
return externalValueToInternalValueImpl(
return externalValueToInternalValueWithOneOfCheck(
inputInterceptor,
fieldVisibility,
type,
defaultValue.getValue(),
graphqlContext,
locale,
0, Integer.MAX_VALUE);
Integer.MAX_VALUE);
}
return assertShouldNeverHappen();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import graphql.language.ObjectField;
import graphql.language.ObjectValue;
import graphql.language.Value;
import graphql.language.VariableReference;
import graphql.schema.GraphQLInputObjectField;
import graphql.schema.GraphQLInputObjectType;
import graphql.schema.GraphQLInputType;
Expand All @@ -26,6 +27,11 @@ final class ValuesResolverOneOfValidation {

@SuppressWarnings("unchecked")
static void validateOneOfInputTypes(GraphQLType type, Object inputValue, Value<?> argumentValue, Locale locale) {
if (argumentValue instanceof VariableReference) {
// variable values were already checked when the variables were coerced, walking them again here
// would repeat that work for every field that references the variable
return;
}
GraphQLType unwrappedNonNullType = GraphQLTypeUtil.unwrapNonNull(type);

if (isList(unwrappedNonNullType)
Expand Down
129 changes: 80 additions & 49 deletions src/test/groovy/graphql/execution/ValuesResolverTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import graphql.language.VariableReference
import graphql.schema.CoercingParseValueException
import graphql.schema.DataFetcher
import spock.lang.Specification
import spock.lang.Timeout
import spock.lang.Unroll

import static graphql.ExecutionInput.newExecutionInput
Expand Down Expand Up @@ -122,6 +123,85 @@ class ValuesResolverTest extends Specification {
thrown(OneOfTooManyKeysException.class)
}

def "getVariableValues: invalid oneOf variable values are rejected when the variables are coerced - #testCase"() {
given:
def oneOfType = newInputObject()
.name("oneOfInputObject")
.withAppliedDirective(Directives.OneOfDirective.toAppliedDirective())
.field(newInputObjectField().name("a").type(GraphQLString))
.field(newInputObjectField().name("b").type(GraphQLInt))
.build()
def parentType = newInputObject()
.name("ParentInputObject")
.field(newInputObjectField().name("oneOfField").type(oneOfType))
.build()
def schema = TestUtil.schemaWithInputType(parentType)
VariableDefinition variableDefinition = new VariableDefinition("var", variableType)

when:
ValuesResolver.coerceVariableValues(schema, [variableDefinition], RawVariables.of([var: inputValue]), graphQLContext, locale)

then:
def e = thrown(expectedException)
e.message == expectedMessage

where:
testCase | variableType | inputValue | expectedException | expectedMessage
'{ a: "abc", b: 123 }' | new TypeName("oneOfInputObject") | [a: "abc", b: 123] | OneOfTooManyKeysException | "Exactly one key must be specified for OneOf type 'oneOfInputObject'."
'{}' | new TypeName("oneOfInputObject") | [:] | OneOfTooManyKeysException | "Exactly one key must be specified for OneOf type 'oneOfInputObject'."
'{ a: "xyz", b: 789 } non-null' | new NonNullType(new TypeName("oneOfInputObject")) | [a: "xyz", b: 789] | OneOfTooManyKeysException | "Exactly one key must be specified for OneOf type 'oneOfInputObject'."
'{ oneOfField: { a: "abc", b: 123 } }' | new TypeName("ParentInputObject") | [oneOfField: [a: "abc", b: 123]] | OneOfTooManyKeysException | "Exactly one key must be specified for OneOf type 'oneOfInputObject'."
'{ oneOfField: {} }' | new TypeName("ParentInputObject") | [oneOfField: [:]] | OneOfTooManyKeysException | "Exactly one key must be specified for OneOf type 'oneOfInputObject'."
'[{ a: "abc" }, { a: "xyz", b: 789 }]' | new ListType(new TypeName("oneOfInputObject")) | [[a: "abc"], [a: "xyz", b: 789]] | OneOfTooManyKeysException | "Exactly one key must be specified for OneOf type 'oneOfInputObject'."
'[{ a: "abc", b: 123 }]' | new ListType(new TypeName("oneOfInputObject")) | [[a: "abc", b: 123]] | OneOfTooManyKeysException | "Exactly one key must be specified for OneOf type 'oneOfInputObject'."
'[{ a: null }]' | new ListType(new TypeName("oneOfInputObject")) | [[a: null]] | OneOfNullValueException | "OneOf type field 'oneOfInputObject.a' must be non-null."
}

def "getVariableValues: an invalid oneOf variable default value is rejected when the variables are coerced"() {
given:
def oneOfType = newInputObject()
.name("oneOfInputObject")
.withAppliedDirective(Directives.OneOfDirective.toAppliedDirective())
.field(newInputObjectField().name("a").type(GraphQLString))
.field(newInputObjectField().name("b").type(GraphQLInt))
.build()
def schema = TestUtil.schemaWithInputType(oneOfType)
def defaultValue = ArrayValue.newArrayValue().value(buildObjectLiteral([a: NullValue.of()])).build()
VariableDefinition variableDefinition = new VariableDefinition("var", new ListType(new TypeName("oneOfInputObject")), defaultValue)

when:
ValuesResolver.coerceVariableValues(schema, [variableDefinition], RawVariables.emptyVariables(), graphQLContext, locale)

then:
def e = thrown(OneOfNullValueException)
e.message == "OneOf type field 'oneOfInputObject.a' must be non-null."
}

@Timeout(10)
def "a variable value is not walked again for every field that references it"() {
def sdl = '''
input Filter { and: [Filter!] eq: String }
type Item { f(filter: Filter): Int }
type Query { items: [Item] }
'''

DataFetcher items = { env -> (1..10_000).collect { [:] } }
DataFetcher f = { env -> 1 }
def graphQL = TestUtil.graphQL(sdl, [Query: [items: items], Item: [f: f]]).build()

// one 100,000 element filter, referenced by Item.f once per item
def filter = [and: (1..100_000).collect { [eq: "x"] }]

when:
def er = graphQL.execute(
newExecutionInput('query ($filter: Filter) { items { f(filter: $filter) } }').variables([filter: filter])
)

then:
er.errors.isEmpty()
er.data["items"].size() == 10_000
}

def "can validate inner input oneOf fields"() {
//
// a test from https://github.com/graphql-java/graphql-java/issues/3572
Expand Down Expand Up @@ -509,12 +589,6 @@ class ValuesResolverTest extends Specification {
a: VariableReference.of("a"),
b: VariableReference.of("v")
]) | CoercedVariables.of(["a": "abc"])

'$var {var : { a : "abc", b : 123}}' | VariableReference.of("var")
| CoercedVariables.of(["var": ["a": "abc", "b": 123]])

'$var {var : {}}' | VariableReference.of("var")
| CoercedVariables.of(["var": [:]])
}

def "getArgumentValues: invalid oneOf nested input because of duplicate keys - #testCase"() {
Expand Down Expand Up @@ -599,11 +673,6 @@ class ValuesResolverTest extends Specification {
b: VariableReference.of("v")
]
]) | CoercedVariables.of(["a": "abc"])
'$var {var : {oneOfField: { a : "abc", b : 123}}}' | VariableReference.of("var")
| CoercedVariables.of(["var": ["oneOfField": ["a": "abc", "b": 123]]])

'$var {var : {oneOfField: {} }}' | VariableReference.of("var")
| CoercedVariables.of(["var": ["oneOfField": [:]]])

}

Expand Down Expand Up @@ -745,16 +814,6 @@ class ValuesResolverTest extends Specification {
]).build()
| CoercedVariables.emptyVariables()

'[{ a: "abc" }, $var ] [{ a: "abc" }, { a: "xyz", b: 789 }]'
| ArrayValue.newArrayValue()
.values([
buildObjectLiteral([
a: StringValue.of("abc")
]),
VariableReference.of("var")
]).build()
| CoercedVariables.of("var": [a: "xyz", b: 789])

}

def "getArgumentValues: invalid oneOf list input because element contains null value - #testCase"() {
Expand Down Expand Up @@ -844,10 +903,6 @@ class ValuesResolverTest extends Specification {
a: NullValue.of()
]) | CoercedVariables.emptyVariables()

'$var [{ a: null }]'
| VariableReference.of("var")
| CoercedVariables.of("var": [[a: null]])

}

def "getArgumentValues: invalid oneOf list input without an array literal because element contains duplicate key - #testCase"() {
Expand Down Expand Up @@ -884,10 +939,6 @@ class ValuesResolverTest extends Specification {
b: IntValue.of(123)
]) | CoercedVariables.emptyVariables()

'$var [{ a: "abc", b: 123 }]'
| VariableReference.of("var")
| CoercedVariables.of("var": [[a: "abc", b: 123]])

}

def "getArgumentValues: invalid oneOf non-null list input because element contains duplicate key - #testCase"() {
Expand Down Expand Up @@ -939,16 +990,6 @@ class ValuesResolverTest extends Specification {
]).build()
| CoercedVariables.emptyVariables()

'[{ a: "abc" }, $var ] [{ a: "abc" }, { a: "xyz", b: 789 }]'
| ArrayValue.newArrayValue()
.values([
buildObjectLiteral([
a: StringValue.of("abc")
]),
VariableReference.of("var")
]).build()
| CoercedVariables.of("var": [a: "xyz", b: 789])

}

def "getArgumentValues: invalid oneOf list input with non-nullable elements, because element contains duplicate key - #testCase"() {
Expand Down Expand Up @@ -1000,16 +1041,6 @@ class ValuesResolverTest extends Specification {
]).build()
| CoercedVariables.emptyVariables()

'[{ a: "abc" }, $var ] [{ a: "abc" }, { a: "xyz", b: 789 }]'
| ArrayValue.newArrayValue()
.values([
buildObjectLiteral([
a: StringValue.of("abc")
]),
VariableReference.of("var")
]).build()
| CoercedVariables.of("var": [a: "xyz", b: 789])

}

def "getArgumentValues: valid oneOf input - #testCase"() {
Expand Down
Loading