-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
OneOf validation not being applied to nested inputs #3365
Merged
bbakerman
merged 4 commits into
graphql-java:master
from
felipe-gdr:oneof-fix-nested-inputs
Oct 30, 2023
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/graphql/execution/ValuesResolverOneOfValidation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package graphql.execution; | ||
|
||
import graphql.Assert; | ||
import graphql.i18n.I18n; | ||
import graphql.language.ObjectField; | ||
import graphql.language.ObjectValue; | ||
import graphql.language.Value; | ||
import graphql.schema.GraphQLInputObjectField; | ||
import graphql.schema.GraphQLInputObjectType; | ||
import graphql.schema.GraphQLInputType; | ||
import graphql.schema.GraphQLType; | ||
import graphql.schema.GraphQLTypeUtil; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
final class ValuesResolverOneOfValidation { | ||
|
||
@SuppressWarnings("unchecked") | ||
static void validateOneOfInputTypes(GraphQLType type, Object inputValue, Value<?> argumentValue, String argumentName, Locale locale) { | ||
GraphQLType unwrappedType = GraphQLTypeUtil.unwrapNonNull(type); | ||
|
||
if (unwrappedType instanceof GraphQLInputObjectType && !ValuesResolverConversion.isNullValue(inputValue)) { | ||
Assert.assertTrue(inputValue instanceof Map, () -> String.format("The coerced argument %s GraphQLInputObjectType is unexpectedly not a map", argumentName)); | ||
Map<String, Object> objectMap = (Map<String, Object>) inputValue; | ||
|
||
GraphQLInputObjectType inputObjectType = (GraphQLInputObjectType) unwrappedType; | ||
|
||
if (inputObjectType.isOneOf()) { | ||
validateOneOfInputTypesInternal(inputObjectType, argumentValue, objectMap, locale); | ||
} | ||
|
||
for (GraphQLInputObjectField fieldDefinition : inputObjectType.getFields()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of the new code is in this |
||
GraphQLInputType childFieldType = fieldDefinition.getType(); | ||
String childFieldName = fieldDefinition.getName(); | ||
Object childFieldInputValue = objectMap.get(childFieldName); | ||
|
||
if (argumentValue instanceof ObjectValue) { | ||
List<Value> values = ((ObjectValue) argumentValue).getObjectFields().stream() | ||
.filter(of -> of.getName().equals(childFieldName)) | ||
.map(ObjectField::getValue) | ||
.collect(Collectors.toList()); | ||
|
||
if (values.size() > 1) { | ||
Assert.assertShouldNeverHappen(String.format("argument %s has %s object fields with the same name: '%s'. A maximum of 1 is expected", argumentName, values.size(), childFieldName)); | ||
} else if (!values.isEmpty()) { | ||
validateOneOfInputTypes(childFieldType, childFieldInputValue, values.get(0), argumentName, locale); | ||
} | ||
} else { | ||
validateOneOfInputTypes(childFieldType, childFieldInputValue, argumentValue, argumentName, locale); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static void validateOneOfInputTypesInternal(GraphQLInputObjectType oneOfInputType, Value<?> argumentValue, Map<String, Object> objectMap, Locale locale) { | ||
int mapSize; | ||
|
||
if (argumentValue instanceof ObjectValue) { | ||
mapSize = ((ObjectValue) argumentValue).getObjectFields().size(); | ||
} else { | ||
mapSize = objectMap.size(); | ||
} | ||
if (mapSize != 1) { | ||
String msg = I18n.i18n(I18n.BundleType.Execution, locale) | ||
.msg("Execution.handleOneOfNotOneFieldError", oneOfInputType.getName()); | ||
throw new OneOfTooManyKeysException(msg); | ||
} | ||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Internal
please - albeit its package scoped but stillThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done ;-)