|
31 | 31 | import graphql.schema.GraphQLScalarType; |
32 | 32 | import graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError; |
33 | 33 |
|
| 34 | +import java.util.Collections; |
34 | 35 | import java.util.List; |
35 | 36 | import java.util.Locale; |
36 | 37 | import java.util.Map; |
|
41 | 42 | import static graphql.collect.ImmutableKit.emptyList; |
42 | 43 | import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.DUPLICATED_KEYS_MESSAGE; |
43 | 44 | import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.EXPECTED_ENUM_MESSAGE; |
44 | | -import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.EXPECTED_LIST_MESSAGE; |
45 | 45 | import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.EXPECTED_NON_NULL_MESSAGE; |
46 | 46 | import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.EXPECTED_OBJECT_MESSAGE; |
47 | 47 | import static graphql.schema.idl.errors.DirectiveIllegalArgumentTypeError.MISSING_REQUIRED_FIELD_MESSAGE; |
@@ -259,25 +259,24 @@ private void checkArgValueMatchesAllowedNonNullType(List<GraphQLError> errors, V |
259 | 259 | } |
260 | 260 |
|
261 | 261 | private void checkArgValueMatchesAllowedListType(List<GraphQLError> errors, Value<?> instanceValue, ListType allowedArgType) { |
262 | | - if (instanceValue instanceof NullValue) { |
263 | | - return; |
| 262 | + // From the spec, on input coercion: |
| 263 | + // If the value passed as an input to a list type is not a list and not the null value, |
| 264 | + // then the result of input coercion is a list of size one where the single item value |
| 265 | + // is the result of input coercion for the list’s item type on the provided value |
| 266 | + // (note this may apply recursively for nested lists). |
| 267 | + |
| 268 | + Value<?> coercedInstanceValue = instanceValue; |
| 269 | + if (!(instanceValue instanceof ArrayValue) && !(instanceValue instanceof NullValue)) { |
| 270 | + coercedInstanceValue = new ArrayValue(Collections.singletonList(instanceValue)); |
264 | 271 | } |
265 | 272 |
|
266 | | - Type<?> unwrappedAllowedType = allowedArgType.getType(); |
267 | | - if (!(instanceValue instanceof ArrayValue)) { |
268 | | - checkArgValueMatchesAllowedType(errors, instanceValue, unwrappedAllowedType); |
| 273 | + if (coercedInstanceValue instanceof NullValue) { |
269 | 274 | return; |
270 | 275 | } |
271 | 276 |
|
272 | | - ArrayValue arrayValue = ((ArrayValue) instanceValue); |
273 | | - boolean isUnwrappedList = unwrappedAllowedType instanceof ListType; |
274 | | - |
275 | | - // validate each instance value in the list, all instances must match for the list to match |
| 277 | + Type<?> unwrappedAllowedType = allowedArgType.getType(); |
| 278 | + ArrayValue arrayValue = ((ArrayValue) coercedInstanceValue); |
276 | 279 | arrayValue.getValues().forEach(value -> { |
277 | | - // restrictive check for sub-arrays |
278 | | - if (isUnwrappedList && !(value instanceof ArrayValue)) { |
279 | | - addValidationError(errors, EXPECTED_LIST_MESSAGE, value.getClass().getSimpleName()); |
280 | | - } |
281 | 280 | checkArgValueMatchesAllowedType(errors, value, unwrappedAllowedType); |
282 | 281 | }); |
283 | 282 | } |
|
0 commit comments