Skip to content
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

coerceValues: correctly handle NaN and similar values #2047

Merged
merged 1 commit into from Jul 22, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/utilities/__tests__/coerceValue-test.js
Expand Up @@ -71,6 +71,11 @@ describe('coerceValue', () => {
expectValue(result).to.equal(null);
});

it('returns no error for NaN result', () => {
const result = coerceValue({ value: NaN }, TestScalar);
expectValue(result).to.satisfy(Number.isNaN);
});

it('returns an error for undefined result', () => {
const result = coerceValue({ value: undefined }, TestScalar);
expectErrors(result).to.deep.equal(['Expected type TestScalar.']);
Expand Down Expand Up @@ -140,9 +145,9 @@ describe('coerceValue', () => {
});

it('returns an error for an invalid field', () => {
const result = coerceValue({ foo: 'abc' }, TestInputObject);
const result = coerceValue({ foo: NaN }, TestInputObject);
expectErrors(result).to.deep.equal([
'Expected type Int at value.foo. Int cannot represent non-integer value: "abc"',
'Expected type Int at value.foo. Int cannot represent non-integer value: NaN',
]);
});

Expand Down Expand Up @@ -184,7 +189,10 @@ describe('coerceValue', () => {
new GraphQLInputObjectType({
name: 'TestInputObject',
fields: {
foo: { type: GraphQLInt, defaultValue },
foo: {
type: new GraphQLScalarType({ name: 'TestScalar' }),
defaultValue,
},
},
});

Expand All @@ -199,8 +207,15 @@ describe('coerceValue', () => {
});

it('returns null as value', () => {
const result = coerceValue({}, TestInputObject(7));
expectValue(result).to.deep.equal({ foo: 7 });
const result = coerceValue({}, TestInputObject(null));
expectValue(result).to.deep.equal({ foo: null });
});

it('returns NaN as value', () => {
const result = coerceValue({}, TestInputObject(NaN));
expectValue(result)
.to.have.property('foo')
.that.satisfy(Number.isNaN);
});
});

Expand Down
7 changes: 3 additions & 4 deletions src/utilities/coerceValue.js
Expand Up @@ -3,7 +3,6 @@
import { forEach, isCollection } from 'iterall';
import objectValues from '../polyfills/objectValues';
import inspect from '../jsutils/inspect';
import isInvalid from '../jsutils/isInvalid';
import didYouMean from '../jsutils/didYouMean';
import isObjectLike from '../jsutils/isObjectLike';
import suggestionList from '../jsutils/suggestionList';
Expand Down Expand Up @@ -63,7 +62,7 @@ export function coerceValue(
// the original error.
try {
const parseResult = type.parseValue(value);
if (isInvalid(parseResult)) {
if (parseResult === undefined) {
return ofErrors([
coercionError(`Expected type ${type.name}`, blameNode, path),
]);
Expand Down Expand Up @@ -145,8 +144,8 @@ export function coerceValue(
// Ensure every defined field is valid.
for (const field of objectValues(fields)) {
const fieldValue = value[field.name];
if (isInvalid(fieldValue)) {
if (!isInvalid(field.defaultValue)) {
if (fieldValue === undefined) {
if (field.defaultValue !== undefined) {
coercedValue[field.name] = field.defaultValue;
} else if (isNonNullType(field.type)) {
errors = add(
Expand Down