Skip to content

Avoid crash when oneOf input variable is not defined #4445

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

Open
wants to merge 1 commit into
base: 16.x.x
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions src/validation/__tests__/ValuesOfCorrectTypeRule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,19 @@ describe('Validate: Values of correct type', () => {
},
]);
});

it('Undefined variable in oneOf input object', () => {
// This test verifies that undefined variables in OneOf input objects
// don't cause a TypeError in ValuesOfCorrectTypeRule.
// The undefined variable should be caught by NoUndefinedVariablesRule instead.
expectErrors(`
{
complicatedArgs {
oneOfArgField(oneOfArg: { stringField: $undefinedVariable })
}
}
`).toDeepEqual([]);
});
});

describe('Directive arguments', () => {
Expand Down
7 changes: 7 additions & 0 deletions src/validation/rules/ValuesOfCorrectTypeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ function validateOneOfInputObject(
if (isVariable) {
const variableName = value.name.value;
const definition = variableDefinitions[variableName];

// If the variable definition is missing, skip validation here.
// This should be caught by other validation rules.
if (!definition) {
return;
}
Comment on lines +220 to +224
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically "Values of Correct Type" in the spec only applies to literals ("For each literal Input Value {value} in the document:"), what you're actually dealing with here is maybe "All Variable Usages Are Allowed"?

But yes, I think in general the spec probably needs all of its validation rules to say "if doesn't exist, return" where that situation has already been covered by a different validation rule explicitly checking that X exists.


const isNullableVariable = definition.type.kind !== Kind.NON_NULL_TYPE;

if (isNullableVariable) {
Expand Down
Loading