Skip to content

Spec compliance: coercion of Int values #837

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
merged 1 commit into from
May 18, 2017
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
37 changes: 26 additions & 11 deletions src/type/__tests__/serialization-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,30 @@ describe('Type System: Scalar coercion', () => {
GraphQLInt.serialize(-1)
).to.equal(-1);
expect(
GraphQLInt.serialize(1e5)
).to.equal(100000);
// The GraphQL specification does not allow serializing non-integer values
// as Int to avoid accidental data loss.
expect(() =>
GraphQLInt.serialize(0.1)
).to.equal(0);
expect(
).to.throw(
'Int cannot represent non-integer value: 0.1'
);
expect(() =>
GraphQLInt.serialize(1.1)
).to.equal(1);
expect(
).to.throw(
'Int cannot represent non-integer value: 1.1'
);
expect(() =>
GraphQLInt.serialize(-1.1)
).to.equal(-1);
expect(
GraphQLInt.serialize(1e5)
).to.equal(100000);
).to.throw(
'Int cannot represent non-integer value: -1.1'
);
expect(() =>
GraphQLInt.serialize('-1.1')
).to.throw(
'Int cannot represent non-integer value: -1.1'
);
// Maybe a safe JavaScript int, but bigger than 2^32, so not
// representable as a GraphQL Int
expect(() =>
Expand All @@ -67,9 +80,6 @@ describe('Type System: Scalar coercion', () => {
).to.throw(
'Int cannot represent non 32-bit signed integer value: -1e+100'
);
expect(
GraphQLInt.serialize('-1.1')
).to.equal(-1);
expect(() =>
GraphQLInt.serialize('one')
).to.throw(
Expand All @@ -86,6 +96,11 @@ describe('Type System: Scalar coercion', () => {
).to.throw(
'Int cannot represent non 32-bit signed integer value: (empty string)'
);
expect(() =>
GraphQLInt.serialize(NaN)
).to.throw(
'Int cannot represent non 32-bit signed integer value: NaN'
);
});

it('serializes output float', () => {
Expand Down
16 changes: 11 additions & 5 deletions src/type/scalars.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ function coerceInt(value: mixed): ?number {
);
}
const num = Number(value);
if (num === num && num <= MAX_INT && num >= MIN_INT) {
return (num < 0 ? Math.ceil : Math.floor)(num);
if (num !== num || num > MAX_INT || num < MIN_INT) {
throw new TypeError(
'Int cannot represent non 32-bit signed integer value: ' + String(value)
);
}
throw new TypeError(
'Int cannot represent non 32-bit signed integer value: ' + String(value)
);
const int = Math.floor(num);
if (int !== num) {
throw new TypeError(
'Int cannot represent non-integer value: ' + String(value)
);
}
return int;
}

export const GraphQLInt = new GraphQLScalarType({
Expand Down
12 changes: 8 additions & 4 deletions src/utilities/__tests__/astFromValue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ describe('astFromValue', () => {
null
);

expect(astFromValue(NaN, GraphQLInt)).to.deep.equal(null);

expect(astFromValue(null, GraphQLBoolean)).to.deep.equal(
{ kind: 'NullValue' }
);
Expand All @@ -61,14 +63,16 @@ describe('astFromValue', () => {
{ kind: 'IntValue', value: '123' }
);

expect(astFromValue(123.5, GraphQLInt)).to.deep.equal(
{ kind: 'IntValue', value: '123' }
);

expect(astFromValue(1e4, GraphQLInt)).to.deep.equal(
{ kind: 'IntValue', value: '10000' }
);

// GraphQL spec does not allow coercing non-integer values to Int to avoid
// accidental data loss.
expect(() => astFromValue(123.5, GraphQLInt)).to.throw(
'Int cannot represent non-integer value: 123.5'
);

// Note: outside the bounds of 32bit signed int.
expect(() => astFromValue(1e40, GraphQLInt)).to.throw(
'Int cannot represent non 32-bit signed integer value: 1e+40'
Expand Down
13 changes: 9 additions & 4 deletions src/utilities/__tests__/isValidJSValue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ describe('isValidJSValue for GraphQLInt', () => {
expectNoErrors(result);
});

it('returns no error for exponent input', () => {
const result = isValidJSValue('1e3', GraphQLInt);
it('returns no error for negative int input', () => {
const result = isValidJSValue('-1', GraphQLInt);
expectNoErrors(result);
});

it('returns no error for float input', () => {
const result = isValidJSValue('1.5', GraphQLInt);
it('returns no error for exponent input', () => {
const result = isValidJSValue('1e3', GraphQLInt);
expectNoErrors(result);
});

Expand All @@ -51,6 +51,11 @@ describe('isValidJSValue for GraphQLInt', () => {
expectErrorResult(result, 1);
});

it('returns error for float input as int', () => {
const result = isValidJSValue('1.5', GraphQLInt);
expectErrorResult(result, 1);
});

it('returns a single error for char input', () => {
const result = isValidJSValue('a', GraphQLInt);
expectErrorResult(result, 1);
Expand Down