From b3afb5f7ee3b8339795154ac26b49cd2e2b066c0 Mon Sep 17 00:00:00 2001 From: Felipe Reis Date: Thu, 1 Feb 2024 14:11:50 +1100 Subject: [PATCH] Fix oneOf bug when variables is empty --- .../ValuesResolverOneOfValidation.java | 39 ++++++++++++------- .../execution/ValuesResolverTest.groovy | 3 ++ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/main/java/graphql/execution/ValuesResolverOneOfValidation.java b/src/main/java/graphql/execution/ValuesResolverOneOfValidation.java index 6456ffaa01..c0f98f5523 100644 --- a/src/main/java/graphql/execution/ValuesResolverOneOfValidation.java +++ b/src/main/java/graphql/execution/ValuesResolverOneOfValidation.java @@ -75,23 +75,36 @@ static void validateOneOfInputTypes(GraphQLType type, Object inputValue, Value argumentValue, Map objectMap, Locale locale) { - int mapSize; - + final String fieldName; if (argumentValue instanceof ObjectValue) { - mapSize = ((ObjectValue) argumentValue).getObjectFields().size(); + List objectFields = ((ObjectValue) argumentValue).getObjectFields(); + if (objectFields.size() != 1) { + throwNotOneFieldError(oneOfInputType, locale); + } + + fieldName = objectFields.iterator().next().getName(); } else { - mapSize = objectMap.size(); - } - if (mapSize != 1) { - String msg = I18n.i18n(I18n.BundleType.Execution, locale) - .msg("Execution.handleOneOfNotOneFieldError", oneOfInputType.getName()); - throw new OneOfTooManyKeysException(msg); + if (objectMap.size() != 1) { + throwNotOneFieldError(oneOfInputType, locale); + } + + fieldName = objectMap.keySet().iterator().next(); } - String fieldName = objectMap.keySet().iterator().next(); + if (objectMap.get(fieldName) == null) { - String msg = I18n.i18n(I18n.BundleType.Execution, locale) - .msg("Execution.handleOneOfValueIsNullError", oneOfInputType.getName() + "." + fieldName); - throw new OneOfNullValueException(msg); + throwValueIsNullError(oneOfInputType, locale, fieldName); } } + + private static void throwValueIsNullError(GraphQLInputObjectType oneOfInputType, Locale locale, String fieldName) { + String msg = I18n.i18n(I18n.BundleType.Execution, locale) + .msg("Execution.handleOneOfValueIsNullError", oneOfInputType.getName() + "." + fieldName); + throw new OneOfNullValueException(msg); + } + + private static void throwNotOneFieldError(GraphQLInputObjectType oneOfInputType, Locale locale) { + String msg = I18n.i18n(I18n.BundleType.Execution, locale) + .msg("Execution.handleOneOfNotOneFieldError", oneOfInputType.getName()); + throw new OneOfTooManyKeysException(msg); + } } diff --git a/src/test/groovy/graphql/execution/ValuesResolverTest.groovy b/src/test/groovy/graphql/execution/ValuesResolverTest.groovy index 8e40ac4433..83488658d2 100644 --- a/src/test/groovy/graphql/execution/ValuesResolverTest.groovy +++ b/src/test/groovy/graphql/execution/ValuesResolverTest.groovy @@ -600,6 +600,9 @@ class ValuesResolverTest extends Specification { a: VariableReference.of("var") ]) | CoercedVariables.of(["var": null]) + '`{ a: $var }` { }' | buildObjectLiteral([ + a: VariableReference.of("var") + ]) | CoercedVariables.emptyVariables() } def "getArgumentValues: invalid oneOf list input because element contains duplicate key - #testCase"() {