Repository navigation
reject null variable values in non-null list items and input fields #4507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andimarek
merged 4 commits into
graphql-java:master
from
nab-iha3:non-null-variable-list-items
Nov 1, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ff81e54
reject null variable values in non-null list items and input fields
nab-iha3 5a89318
Add integration coverage for null variables in non-null positions
andimarek 05a52ea
Keep lazy coercion regression tests out of PR 4507
andimarek 39e9c54
Merge branch 'master' into non-null-variable-list-items
andimarek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
src/test/groovy/graphql/execution/ValuesResolverNullabilityTest.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| package graphql.execution | ||
|
|
||
| import graphql.ErrorType | ||
| import graphql.ExceptionWhileDataFetching | ||
| import graphql.ExecutionInput | ||
| import graphql.ExecutionResult | ||
| import graphql.GraphQL | ||
| import graphql.ParseAndValidate | ||
| import graphql.TestUtil | ||
| import graphql.schema.DataFetcher | ||
| import spock.lang.Specification | ||
|
|
||
| class ValuesResolverNullabilityTest extends Specification { | ||
|
|
||
| def "nullable variable with a non-null default at #argumentType with variables #variables"() { | ||
| given: | ||
| def received = [] | ||
| DataFetcher fetcher = { env -> | ||
| received.add(env.getArgument('arg')) | ||
| 'resolved' | ||
| } | ||
| def graphQL = buildGraphQL(argumentType, fetcher) | ||
| def query = 'query($v: String = "x") { f(arg: ' + literal + ') good }' | ||
|
|
||
| when: | ||
| def result = executeValid(graphQL, query, variables) | ||
|
|
||
| then: | ||
| result.data == [f: fails ? null : 'resolved', good: 'good'] | ||
| result.errors.size() == (fails ? 1 : 0) | ||
| result.errors*.path == (fails ? [['f']] : []) | ||
| result.errors.every { it instanceof ExceptionWhileDataFetching && it.exception instanceof NonNullableValueCoercedAsNullException } | ||
| received == (fails ? [] : [expectedValue]) | ||
|
|
||
| where: | ||
| argumentType | literal | variables | expectedValue | fails | ||
| '[String!]!' | '[$v]' | [:] | ['x'] | false | ||
| '[String!]!' | '[$v]' | [v: 'z'] | ['z'] | false | ||
| '[String!]!' | '[$v]' | [v: null] | null | true | ||
| 'RequiredInput'| '{a: $v}' | [:] | [a: 'x'] | false | ||
| 'RequiredInput'| '{a: $v}' | [v: 'z'] | [a: 'z'] | false | ||
| 'RequiredInput'| '{a: $v}' | [v: null] | null | true | ||
| 'ListInput' | '{items:[$v]}'| [:] | [items: ['x']] | false | ||
| 'ListInput' | '{items:[$v]}'| [v: 'z'] | [items: ['z']] | false | ||
| 'ListInput' | '{items:[$v]}'| [v: null] | null | true | ||
| '[[String!]!]!'| '[[$v]]' | [:] | [['x']] | false | ||
| '[[String!]!]!'| '[[$v]]' | [v: 'z'] | [['z']] | false | ||
| '[[String!]!]!'| '[[$v]]' | [v: null] | null | true | ||
| } | ||
|
|
||
| def "input field default distinguishes omission from null for #variableDefinition with #variables"() { | ||
| given: | ||
| def received = [] | ||
| DataFetcher fetcher = { env -> | ||
| received.add(env.getArgument('arg')) | ||
| 'resolved' | ||
| } | ||
| def graphQL = buildGraphQL('DefaultInput', fetcher) | ||
| def query = 'query($v: ' + variableDefinition + ') { f(arg: {a: $v}) good }' | ||
|
|
||
| when: | ||
| def result = executeValid(graphQL, query, variables) | ||
|
|
||
| then: | ||
| result.data == [f: fails ? null : 'resolved', good: 'good'] | ||
| result.errors.size() == (fails ? 1 : 0) | ||
| result.errors*.path == (fails ? [['f']] : []) | ||
| result.errors.every { it instanceof ExceptionWhileDataFetching && it.exception instanceof NonNullableValueCoercedAsNullException } | ||
| received == (fails ? [] : [[a: expectedValue]]) | ||
|
|
||
| where: | ||
| variableDefinition | variables | expectedValue | fails | ||
| 'String' | [:] | 'd' | false | ||
| 'String' | [v: 'z'] | 'z' | false | ||
| 'String' | [v: null] | null | true | ||
| 'String = "x"' | [:] | 'x' | false | ||
| 'String = "x"' | [v: null] | null | true | ||
| 'String = null' | [:] | null | true | ||
| } | ||
|
|
||
| def "nullable positions preserve missing and null variables in #literal with #variables"() { | ||
| given: | ||
| def received = [] | ||
| DataFetcher fetcher = { env -> | ||
| received.add(env.getArgument('arg')) | ||
| 'resolved' | ||
| } | ||
| def graphQL = buildGraphQL(argumentType, fetcher) | ||
| def query = 'query($v: String) { f(arg: ' + literal + ') }' | ||
|
|
||
| when: | ||
| def result = executeValid(graphQL, query, variables) | ||
|
|
||
| then: | ||
| result.errors.empty | ||
| result.data == [f: 'resolved'] | ||
| received == [expectedValue] | ||
|
|
||
| where: | ||
| argumentType | literal | variables | expectedValue | ||
| '[String]' | '[$v]' | [:] | [null] | ||
| '[String]' | '[$v]' | [v: null] | [null] | ||
| 'NullableInput'| '{a: $v}' | [:] | [:] | ||
| 'NullableInput'| '{a: $v}' | [v: null] | [a: null] | ||
| } | ||
|
|
||
| def "a nullable variable without a non-null default cannot be used as a non-null list item"() { | ||
| given: | ||
| int calls = 0 | ||
| DataFetcher fetcher = { calls++; 'unexpected' } | ||
| def graphQL = buildGraphQL('[String!]!', fetcher) | ||
| def input = ExecutionInput.newExecutionInput('query($v: ' + variableDefinition + ') { f(arg: [$v]) }').build() | ||
|
|
||
| when: | ||
| def result = graphQL.execute(input) | ||
|
|
||
| then: | ||
| !result.dataPresent | ||
| result.errors.size() == 1 | ||
| result.errors[0].errorType == ErrorType.ValidationError | ||
| calls == 0 | ||
|
|
||
| where: | ||
| variableDefinition << ['String', 'String = null'] | ||
| } | ||
|
|
||
| def "#operation coercion error uses the response alias and propagates through #resultType"() { | ||
| given: | ||
| DataFetcher fetcher = { env -> | ||
| env.getArgument('arg') | ||
| 'unexpected' | ||
| } | ||
| def graphQL = buildGraphQL('[String!]!', fetcher, resultType) | ||
| def query = operation + '($v: String = "x") { alias: f(arg: [$v]) }' | ||
|
|
||
| when: | ||
| def result = executeValid(graphQL, query, [v: null]) | ||
|
|
||
| then: | ||
| result.dataPresent | ||
| result.data == (resultType == 'String!' ? null : [alias: null]) | ||
| result.errors.size() == 1 | ||
| result.errors[0].path == ['alias'] | ||
| !result.errors[0].locations.empty | ||
| result.errors[0] instanceof ExceptionWhileDataFetching | ||
| result.errors[0].exception instanceof NonNullableValueCoercedAsNullException | ||
|
|
||
| where: | ||
| operation | resultType | ||
| 'query' | 'String' | ||
| 'query' | 'String!' | ||
| 'mutation' | 'String' | ||
| 'mutation' | 'String!' | ||
| } | ||
|
|
||
| private GraphQL buildGraphQL(String argumentType, DataFetcher fetcher, String resultType = 'String') { | ||
| def sdl = """ | ||
| input RequiredInput { a: String! } | ||
| input DefaultInput { a: String! = "d" } | ||
| input NullableInput { a: String } | ||
| input ListInput { items: [String!] } | ||
| type Query { f(arg: $argumentType): $resultType good: String! } | ||
| type Mutation { f(arg: $argumentType): $resultType } | ||
| """ | ||
| TestUtil.graphQL(sdl, [Query: [f: fetcher, good: { 'good' } as DataFetcher], Mutation: [f: fetcher]]).build() | ||
| } | ||
|
|
||
| private ExecutionResult executeValid(GraphQL graphQL, String query, Map<String, Object> variables) { | ||
| def input = ExecutionInput.newExecutionInput(query).variables(variables).build() | ||
| assert !ParseAndValidate.parseAndValidate(graphQL.graphQLSchema, input).isFailure() | ||
| graphQL.execute(input) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ye old nullability on variables - this has long been a challenge around is a value present but null versus null because its absent.
I am not sure this is correct or that we can tell here if a value is present and was set explicitly to null versus being null
We may have to use
graphql.execution.CoercedVariables#containsKeyto distinguishI would like @andimarek to comment on this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fair concern, but i don't think the distinction changes the outcome at either site.
externalValueToInternalValueForVariablesonly puts a key intoCoercedVariableswhen the variable was supplied (explicit null included) or has a default, soget()returning null here means either an explicit null or an absent variable with no default. for a non-null target both are invalid: graphql-jsvalueFromASTrejects a missing variable in a non-null list item the same way it rejects an explicit null. the one place absent has to behave differently is an input field with a default, and that branch already goes throughcontainsKey(hasValue) and runs before the line i changed, so an absent variable still picks up the field default.i ran the combinations against this branch to check:
an absent variable without a default can only reach the list item check with validation bypassed (a nullable variable with no default isn't allowed in a
[String!]position, and a non-null variable fails variable coercion first), and rejecting it there still matches graphql-js. happy to switch the list item check tocontainsKeyif you'd rather see the absent case spelled out, but the result would be the same.