Skip to content

Commit e347d87

Browse files
cristunaranjoyaacovCR
authored andcommitted
fix(coerce-input-value): input object coercion rejects arrays (#4367)
1 parent 8f5760b commit e347d87

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

src/utilities/__tests__/coerceInputValue-test.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,13 @@ describe('coerceInputValue', () => {
122122
});
123123

124124
describe('for GraphQLInputObject', () => {
125-
const TestInputObject = new GraphQLInputObjectType({
125+
const TestInputObject: GraphQLInputObjectType = new GraphQLInputObjectType({
126126
name: 'TestInputObject',
127-
fields: {
127+
fields: () => ({
128128
foo: { type: new GraphQLNonNull(GraphQLInt) },
129129
bar: { type: GraphQLInt },
130-
},
130+
nestedObject: { type: TestInputObject },
131+
}),
131132
});
132133

133134
it('returns no error for a valid input', () => {
@@ -153,6 +154,18 @@ describe('coerceInputValue', () => {
153154
it('invalid for an unknown field', () => {
154155
test({ foo: 123, unknownField: 123 }, TestInputObject, undefined);
155156
});
157+
158+
it('invalid when supplied with an array', () => {
159+
test([{ foo: 123 }, { bar: 456 }], TestInputObject, undefined);
160+
});
161+
162+
it('invalid when a nested input object is supplied with an array', () => {
163+
test(
164+
{ foo: 123, nested: [{ foo: 123 }, { bar: 456 }] },
165+
TestInputObject,
166+
undefined,
167+
);
168+
});
156169
});
157170

158171
describe('for GraphQLInputObject that isOneOf', () => {

src/utilities/__tests__/validateInputValue-test.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,13 @@ describe('validateInputValue', () => {
228228
});
229229

230230
describe('for GraphQLInputObject', () => {
231-
const TestInputObject = new GraphQLInputObjectType({
231+
const TestInputObject: GraphQLInputObjectType = new GraphQLInputObjectType({
232232
name: 'TestInputObject',
233-
fields: {
233+
fields: () => ({
234234
foo: { type: new GraphQLNonNull(GraphQLInt) },
235235
bar: { type: GraphQLInt },
236-
},
236+
nested: { type: TestInputObject },
237+
}),
237238
});
238239

239240
it('returns no error for a valid input', () => {
@@ -292,6 +293,30 @@ describe('validateInputValue', () => {
292293
]);
293294
});
294295

296+
it('returns error when supplied with an array', () => {
297+
test([{ foo: 123 }, { bar: 456 }], TestInputObject, [
298+
{
299+
error:
300+
'Expected value of type "TestInputObject" to be an object, found: [{ foo: 123 }, { bar: 456 }].',
301+
path: [],
302+
},
303+
]);
304+
});
305+
306+
it('returns error when a nested input object is supplied with an array', () => {
307+
test(
308+
{ foo: 123, nested: [{ foo: 123 }, { bar: 456 }] },
309+
TestInputObject,
310+
[
311+
{
312+
error:
313+
'Expected value of type "TestInputObject" to be an object, found: [{ foo: 123 }, { bar: 456 }].',
314+
path: ['nested'],
315+
},
316+
],
317+
);
318+
});
319+
295320
it('returns error for a misspelled field', () => {
296321
test({ foo: 123, bart: 123 }, TestInputObject, [
297322
{

src/utilities/coerceInputValue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function coerceInputValue(
6565
}
6666

6767
if (isInputObjectType(type)) {
68-
if (!isObjectLike(inputValue)) {
68+
if (!isObjectLike(inputValue) || Array.isArray(inputValue)) {
6969
return; // Invalid: intentionally return no value.
7070
}
7171

src/utilities/validateInputValue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function validateInputValueImpl(
107107
}
108108
}
109109
} else if (isInputObjectType(type)) {
110-
if (!isObjectLike(inputValue)) {
110+
if (!isObjectLike(inputValue) || Array.isArray(inputValue)) {
111111
reportInvalidValue(
112112
onError,
113113
`Expected value of type "${type}" to be an object, found: ${inspect(

0 commit comments

Comments
 (0)