From 945245571153d03d2053a1e334f5c7470c6843c6 Mon Sep 17 00:00:00 2001 From: Nabiha Date: Sun, 15 Nov 2026 20:20:37 +0530 Subject: [PATCH] stop re-walking variable values in validateOneOfInputTypes --- .../execution/ValuesResolverConversion.java | 35 +++-- .../ValuesResolverOneOfValidation.java | 6 + .../execution/ValuesResolverTest.groovy | 129 +++++++++++------- 3 files changed, 113 insertions(+), 57 deletions(-) diff --git a/src/main/java/graphql/execution/ValuesResolverConversion.java b/src/main/java/graphql/execution/ValuesResolverConversion.java index fd15b1b0d..35b3c21bc 100644 --- a/src/main/java/graphql/execution/ValuesResolverConversion.java +++ b/src/main/java/graphql/execution/ValuesResolverConversion.java @@ -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); } @@ -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); @@ -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( @@ -518,8 +539,6 @@ private static Object externalValueToInternalValueImpl( locale, depth + 1, maxDepth); - - ValuesResolverOneOfValidation.validateOneOfInputTypes(inputObjectType, coercedMap, null, locale); return coercedMap; } else { throw CoercingParseValueException.newCoercingParseValueException() @@ -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(); } diff --git a/src/main/java/graphql/execution/ValuesResolverOneOfValidation.java b/src/main/java/graphql/execution/ValuesResolverOneOfValidation.java index 7c3ff6b55..a964464f7 100644 --- a/src/main/java/graphql/execution/ValuesResolverOneOfValidation.java +++ b/src/main/java/graphql/execution/ValuesResolverOneOfValidation.java @@ -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; @@ -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) diff --git a/src/test/groovy/graphql/execution/ValuesResolverTest.groovy b/src/test/groovy/graphql/execution/ValuesResolverTest.groovy index cc4d7e7a4..d02dc660b 100644 --- a/src/test/groovy/graphql/execution/ValuesResolverTest.groovy +++ b/src/test/groovy/graphql/execution/ValuesResolverTest.groovy @@ -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 @@ -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 @@ -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"() { @@ -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": [:]]]) } @@ -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"() { @@ -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"() { @@ -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"() { @@ -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"() { @@ -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"() {