|
19 | 19 | import graphql.schema.GraphQLTypeUtil; |
20 | 20 | import graphql.schema.GraphQLTypeVisitorStub; |
21 | 21 | import graphql.schema.GraphQLUnionType; |
| 22 | +import graphql.schema.GraphQLUnmodifiedType; |
22 | 23 | import graphql.util.FpKit; |
23 | 24 | import graphql.util.TraversalControl; |
24 | 25 | import graphql.util.TraverserContext; |
25 | 26 |
|
26 | 27 | import java.util.HashSet; |
| 28 | +import java.util.LinkedHashSet; |
27 | 29 | import java.util.List; |
28 | 30 | import java.util.Set; |
29 | 31 | import java.util.function.Predicate; |
@@ -91,6 +93,35 @@ public TraversalControl visitGraphQLInputObjectType(GraphQLInputObjectType type, |
91 | 93 | } |
92 | 94 | SchemaValidationErrorCollector errorCollector = context.getVarFromParents(SchemaValidationErrorCollector.class); |
93 | 95 | validateInputObject((GraphQLInputObjectType) type, errorCollector); |
| 96 | + |
| 97 | + // OneOf validation: check if OneOf input types are inhabited |
| 98 | + if (type.isOneOf()) { |
| 99 | + if (!canBeProvidedAFiniteValue(type, new LinkedHashSet<>())) { |
| 100 | + String message = String.format("OneOf Input Object %s must be inhabited but all fields recursively reference only other OneOf Input Objects forming an unresolvable cycle.", type.getName()); |
| 101 | + errorCollector.addError(new SchemaValidationError(SchemaValidationErrorType.OneOfNotInhabited, message)); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return TraversalControl.CONTINUE; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public TraversalControl visitGraphQLInputObjectField(GraphQLInputObjectField inputObjectField, TraverserContext<GraphQLSchemaElement> context) { |
| 110 | + GraphQLInputObjectType inputObjectType = (GraphQLInputObjectType) context.getParentNode(); |
| 111 | + if (!inputObjectType.isOneOf()) { |
| 112 | + return TraversalControl.CONTINUE; |
| 113 | + } |
| 114 | + SchemaValidationErrorCollector errorCollector = context.getVarFromParents(SchemaValidationErrorCollector.class); |
| 115 | + // OneOf validation: error messages taken from the reference implementation |
| 116 | + if (inputObjectField.hasSetDefaultValue()) { |
| 117 | + String message = String.format("OneOf input field %s.%s cannot have a default value.", inputObjectType.getName(), inputObjectField.getName()); |
| 118 | + errorCollector.addError(new SchemaValidationError(SchemaValidationErrorType.OneOfDefaultValueOnField, message)); |
| 119 | + } |
| 120 | + |
| 121 | + if (GraphQLTypeUtil.isNonNull(inputObjectField.getType())) { |
| 122 | + String message = String.format("OneOf input field %s.%s must be nullable.", inputObjectType.getName(), inputObjectField.getName()); |
| 123 | + errorCollector.addError(new SchemaValidationError(SchemaValidationErrorType.OneOfNonNullableField, message)); |
| 124 | + } |
94 | 125 | return TraversalControl.CONTINUE; |
95 | 126 | } |
96 | 127 |
|
@@ -121,6 +152,32 @@ private void validateInputObject(GraphQLInputObjectType type, SchemaValidationEr |
121 | 152 | } |
122 | 153 | } |
123 | 154 |
|
| 155 | + private boolean canBeProvidedAFiniteValue(GraphQLInputObjectType oneOfInputObject, Set<GraphQLInputObjectType> visited) { |
| 156 | + if (visited.contains(oneOfInputObject)) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + Set<GraphQLInputObjectType> nextVisited = new LinkedHashSet<>(visited); |
| 160 | + nextVisited.add(oneOfInputObject); |
| 161 | + for (GraphQLInputObjectField field : oneOfInputObject.getFieldDefinitions()) { |
| 162 | + GraphQLType fieldType = field.getType(); |
| 163 | + if (GraphQLTypeUtil.isList(fieldType)) { |
| 164 | + return true; |
| 165 | + } |
| 166 | + GraphQLUnmodifiedType namedFieldType = GraphQLTypeUtil.unwrapAll(fieldType); |
| 167 | + if (!(namedFieldType instanceof GraphQLInputObjectType)) { |
| 168 | + return true; |
| 169 | + } |
| 170 | + GraphQLInputObjectType inputFieldType = (GraphQLInputObjectType) namedFieldType; |
| 171 | + if (!inputFieldType.isOneOf()) { |
| 172 | + return true; |
| 173 | + } |
| 174 | + if (canBeProvidedAFiniteValue(inputFieldType, nextVisited)) { |
| 175 | + return true; |
| 176 | + } |
| 177 | + } |
| 178 | + return false; |
| 179 | + } |
| 180 | + |
124 | 181 | private void validateUnion(GraphQLUnionType type, SchemaValidationErrorCollector errorCollector) { |
125 | 182 | List<GraphQLNamedOutputType> memberTypes = type.getTypes(); |
126 | 183 | if (memberTypes.size() == 0) { |
|
0 commit comments