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

Reject Infinity supplied as Int or Float value #1365

Merged
merged 1 commit into from
Jun 5, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/jsutils/isInteger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

declare function isInteger(value: mixed): boolean %checks(typeof value ===
'number');

/* eslint-disable no-redeclare */
// $FlowFixMe workaround for: https://github.com/facebook/flow/issues/4441
const isInteger =
Number.isInteger ||
function(value) {
return (
typeof value === 'number' &&
isFinite(value) &&
Math.floor(value) === value
);
};
export default isInteger;
3 changes: 1 addition & 2 deletions src/subscription/__tests__/subscribe-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,7 @@ describe('Subscription Initialization Phase', () => {
{
message:
'Variable "$priority" got invalid value "meow"; Expected ' +
'type Int; Int cannot represent non 32-bit signed ' +
'integer value: meow',
'type Int; Int cannot represent non-integer value: meow',
locations: [{ line: 2, column: 21 }],
},
],
Expand Down
12 changes: 9 additions & 3 deletions src/type/__tests__/serialization-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ describe('Type System: Scalar coercion', () => {
'Int cannot represent non 32-bit signed integer value: -1e+100',
);
expect(() => GraphQLInt.serialize('one')).to.throw(
'Int cannot represent non 32-bit signed integer value: one',
'Int cannot represent non-integer value: one',
);
// Doesn't represent number
expect(() => GraphQLInt.serialize('')).to.throw(
'Int cannot represent non 32-bit signed integer value: (empty string)',
'Int cannot represent non-integer value: (empty string)',
);
expect(() => GraphQLInt.serialize(NaN)).to.throw(
'Int cannot represent non 32-bit signed integer value: NaN',
'Int cannot represent non-integer value: NaN',
);
expect(() => GraphQLInt.serialize(Infinity)).to.throw(
'Int cannot represent non-integer value: Infinity',
);
expect(() => GraphQLInt.serialize([5])).to.throw(
'Int cannot represent an array value: [5]',
Expand All @@ -85,6 +88,9 @@ describe('Type System: Scalar coercion', () => {
expect(() => GraphQLFloat.serialize(NaN)).to.throw(
'Float cannot represent non numeric value: NaN',
);
expect(() => GraphQLFloat.serialize(Infinity)).to.throw(
'Float cannot represent non numeric value: Infinity',
);
expect(() => GraphQLFloat.serialize('one')).to.throw(
'Float cannot represent non numeric value: one',
);
Expand Down
17 changes: 9 additions & 8 deletions src/type/scalars.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import inspect from '../jsutils/inspect';
import isInteger from '../jsutils/isInteger';
import { GraphQLScalarType, isNamedType } from './definition';
import { Kind } from '../language/kinds';

Expand All @@ -27,22 +28,22 @@ function coerceInt(value: mixed): number {
}
if (value === '') {
throw new TypeError(
'Int cannot represent non 32-bit signed integer value: (empty string)',
'Int cannot represent non-integer value: (empty string)',
);
}
const num = Number(value);
if (num !== num || num > MAX_INT || num < MIN_INT) {
if (!isInteger(num)) {
throw new TypeError(
'Int cannot represent non 32-bit signed integer value: ' + inspect(value),
'Int cannot represent non-integer value: ' + inspect(value),
);
}
const int = Math.floor(num);
if (int !== num) {

if (num > MAX_INT || num < MIN_INT) {
throw new TypeError(
'Int cannot represent non-integer value: ' + inspect(value),
'Int cannot represent non 32-bit signed integer value: ' + inspect(value),
);
}
return int;
return num;
}

export const GraphQLInt = new GraphQLScalarType({
Expand Down Expand Up @@ -75,7 +76,7 @@ function coerceFloat(value: mixed): number {
);
}
const num = Number(value);
if (num === num) {
if (isFinite(num)) {
return num;
}
throw new TypeError(
Expand Down
39 changes: 30 additions & 9 deletions src/utilities/__tests__/coerceValue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,31 +62,45 @@ describe('coerceValue', () => {
expectValue(result).to.equal(null);
});

it('returns a single error for empty value', () => {
it('returns a single error for empty string as value', () => {
const result = coerceValue('', GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non 32-bit signed integer value: (empty string)',
'Expected type Int; Int cannot represent non-integer value: (empty string)',
]);
});

it('returns error for float input as int', () => {
it('returns a single error for 2^32 input as int', () => {
const result = coerceValue(Math.pow(2, 32), GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non 32-bit signed integer value: 4294967296',
]);
});

it('returns a single error for float input as int', () => {
const result = coerceValue('1.5', GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non-integer value: 1.5',
]);
});

it('returns a single error for Infinity input as int', () => {
const result = coerceValue(Infinity, GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non-integer value: Infinity',
]);
});

it('returns a single error for char input', () => {
const result = coerceValue('a', GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non 32-bit signed integer value: a',
'Expected type Int; Int cannot represent non-integer value: a',
]);
});

it('returns a single error for char input', () => {
const result = coerceValue('meow', GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non 32-bit signed integer value: meow',
'Expected type Int; Int cannot represent non-integer value: meow',
]);
});
});
Expand All @@ -112,13 +126,20 @@ describe('coerceValue', () => {
expectValue(result).to.equal(null);
});

it('returns a single error for empty value', () => {
it('returns a single error for empty string input', () => {
const result = coerceValue('', GraphQLFloat);
expectErrors(result).to.deep.equal([
'Expected type Float; Float cannot represent non numeric value: (empty string)',
]);
});

it('returns a single error for Infinity input', () => {
const result = coerceValue(Infinity, GraphQLFloat);
expectErrors(result).to.deep.equal([
'Expected type Float; Float cannot represent non numeric value: Infinity',
]);
});

it('returns a single error for char input', () => {
const result = coerceValue('a', GraphQLFloat);
expectErrors(result).to.deep.equal([
Expand Down Expand Up @@ -191,15 +212,15 @@ describe('coerceValue', () => {
it('returns no error for an invalid field', () => {
const result = coerceValue({ foo: 'abc' }, TestInputObject);
expectErrors(result).to.deep.equal([
'Expected type Int at value.foo; Int cannot represent non 32-bit signed integer value: abc',
'Expected type Int at value.foo; Int cannot represent non-integer value: abc',
]);
});

it('returns multiple errors for multiple invalid fields', () => {
const result = coerceValue({ foo: 'abc', bar: 'def' }, TestInputObject);
expectErrors(result).to.deep.equal([
'Expected type Int at value.foo; Int cannot represent non 32-bit signed integer value: abc',
'Expected type Int at value.bar; Int cannot represent non 32-bit signed integer value: def',
'Expected type Int at value.foo; Int cannot represent non-integer value: abc',
'Expected type Int at value.bar; Int cannot represent non-integer value: def',
]);
});

Expand Down