Skip to content
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
39 changes: 26 additions & 13 deletions src/main/java/graphql/execution/ValuesResolverOneOfValidation.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,36 @@ static void validateOneOfInputTypes(GraphQLType type, Object inputValue, Value<?
}

private static void validateOneOfInputTypesInternal(GraphQLInputObjectType oneOfInputType, Value<?> argumentValue, Map<String, Object> objectMap, Locale locale) {
int mapSize;

final String fieldName;
if (argumentValue instanceof ObjectValue) {
mapSize = ((ObjectValue) argumentValue).getObjectFields().size();
List<ObjectField> 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();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug was in this line. We were checking the size of argumentValue.getObjectFields() but trying to get the first element of objectMap, which would be empty if no variables were passed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (argumentValue instanceof ObjectValue)

we got mapSize from argumentValue. But objectMap can still be empty and we didn't check objectMap size.

That's the problem , right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that's right


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);
}
}
3 changes: 3 additions & 0 deletions src/test/groovy/graphql/execution/ValuesResolverTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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"() {
Expand Down