Skip to content
Closed
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
41 changes: 0 additions & 41 deletions src/main/java/graphql/schema/validation/OneOfInputObjectRules.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum SchemaValidationErrorType implements SchemaValidationErrorClassifica
InputTypeUsedInOutputTypeContext,
OneOfDefaultValueOnField,
OneOfNonNullableField,
OneOfNotInhabited,
RequiredInputFieldCannotBeDeprecated,
RequiredFieldArgumentCannotBeDeprecated,
RequiredDirectiveArgumentCannotBeDeprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public SchemaValidator() {
rules.add(new AppliedDirectivesAreValid());
rules.add(new AppliedDirectiveArgumentsAreValid());
rules.add(new InputAndOutputTypesUsedAppropriately());
rules.add(new OneOfInputObjectRules());
rules.add(new DeprecatedInputObjectAndArgumentsAreValid());
}

Expand Down
57 changes: 57 additions & 0 deletions src/main/java/graphql/schema/validation/TypeAndFieldRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import graphql.schema.GraphQLTypeUtil;
import graphql.schema.GraphQLTypeVisitorStub;
import graphql.schema.GraphQLUnionType;
import graphql.schema.GraphQLUnmodifiedType;
import graphql.util.FpKit;
import graphql.util.TraversalControl;
import graphql.util.TraverserContext;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
Expand Down Expand Up @@ -91,6 +93,35 @@ public TraversalControl visitGraphQLInputObjectType(GraphQLInputObjectType type,
}
SchemaValidationErrorCollector errorCollector = context.getVarFromParents(SchemaValidationErrorCollector.class);
validateInputObject((GraphQLInputObjectType) type, errorCollector);

// OneOf validation: check if OneOf input types are inhabited
if (type.isOneOf()) {
if (!canBeProvidedAFiniteValue(type, new LinkedHashSet<>())) {
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());
errorCollector.addError(new SchemaValidationError(SchemaValidationErrorType.OneOfNotInhabited, message));
}
}

return TraversalControl.CONTINUE;
}

@Override
public TraversalControl visitGraphQLInputObjectField(GraphQLInputObjectField inputObjectField, TraverserContext<GraphQLSchemaElement> context) {
GraphQLInputObjectType inputObjectType = (GraphQLInputObjectType) context.getParentNode();
if (!inputObjectType.isOneOf()) {
return TraversalControl.CONTINUE;
}
SchemaValidationErrorCollector errorCollector = context.getVarFromParents(SchemaValidationErrorCollector.class);
// OneOf validation: error messages taken from the reference implementation
if (inputObjectField.hasSetDefaultValue()) {
String message = String.format("OneOf input field %s.%s cannot have a default value.", inputObjectType.getName(), inputObjectField.getName());
errorCollector.addError(new SchemaValidationError(SchemaValidationErrorType.OneOfDefaultValueOnField, message));
}

if (GraphQLTypeUtil.isNonNull(inputObjectField.getType())) {
String message = String.format("OneOf input field %s.%s must be nullable.", inputObjectType.getName(), inputObjectField.getName());
errorCollector.addError(new SchemaValidationError(SchemaValidationErrorType.OneOfNonNullableField, message));
}
return TraversalControl.CONTINUE;
}

Expand Down Expand Up @@ -121,6 +152,32 @@ private void validateInputObject(GraphQLInputObjectType type, SchemaValidationEr
}
}

private boolean canBeProvidedAFiniteValue(GraphQLInputObjectType oneOfInputObject, Set<GraphQLInputObjectType> visited) {
if (visited.contains(oneOfInputObject)) {
return false;
}
Set<GraphQLInputObjectType> nextVisited = new LinkedHashSet<>(visited);
nextVisited.add(oneOfInputObject);
for (GraphQLInputObjectField field : oneOfInputObject.getFieldDefinitions()) {
GraphQLType fieldType = field.getType();
if (GraphQLTypeUtil.isList(fieldType)) {
return true;
}
GraphQLUnmodifiedType namedFieldType = GraphQLTypeUtil.unwrapAll(fieldType);
if (!(namedFieldType instanceof GraphQLInputObjectType)) {
return true;
}
GraphQLInputObjectType inputFieldType = (GraphQLInputObjectType) namedFieldType;
if (!inputFieldType.isOneOf()) {
return true;
}
if (canBeProvidedAFiniteValue(inputFieldType, nextVisited)) {
return true;
}
}
return false;
}

private void validateUnion(GraphQLUnionType type, SchemaValidationErrorCollector errorCollector) {
List<GraphQLNamedOutputType> memberTypes = type.getTypes();
if (memberTypes.size() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,243 @@ class OneOfInputObjectRulesTest extends Specification {
schemaProblem.errors[1].description == "OneOf input field OneOfInputType.badDefaulted cannot have a default value."
schemaProblem.errors[1].classification == SchemaValidationErrorType.OneOfDefaultValueOnField
}

def "oneOf with scalar fields is inhabited"() {
def sdl = """
type Query {
f(arg : A) : String
}

input A @oneOf {
a : String
b : Int
}
"""

when:
def registry = new SchemaParser().parse(sdl)
new SchemaGenerator().makeExecutableSchema(registry, TestUtil.getMockRuntimeWiring())

then:
noExceptionThrown()
}

def "oneOf with enum field is inhabited"() {
def sdl = """
type Query {
f(arg : A) : String
}

enum Color {
RED
GREEN
BLUE
}

input A @oneOf {
a : Color
}
"""

when:
def registry = new SchemaParser().parse(sdl)
new SchemaGenerator().makeExecutableSchema(registry, TestUtil.getMockRuntimeWiring())

then:
noExceptionThrown()
}

def "oneOf with list field is inhabited"() {
def sdl = """
type Query {
f(arg : A) : String
}

input A @oneOf {
a : [A]
}
"""

when:
def registry = new SchemaParser().parse(sdl)
new SchemaGenerator().makeExecutableSchema(registry, TestUtil.getMockRuntimeWiring())

then:
noExceptionThrown()
}

def "oneOf referencing non-oneOf input is inhabited"() {
def sdl = """
type Query {
f(arg : A) : String
}

input RegularInput {
x : String
}

input A @oneOf {
a : RegularInput
}
"""

when:
def registry = new SchemaParser().parse(sdl)
new SchemaGenerator().makeExecutableSchema(registry, TestUtil.getMockRuntimeWiring())

then:
noExceptionThrown()
}

def "oneOf with escape field is inhabited"() {
def sdl = """
type Query {
f(arg : A) : String
}

input A @oneOf {
b : B
escape : String
}

input B @oneOf {
a : A
}
"""

when:
def registry = new SchemaParser().parse(sdl)
new SchemaGenerator().makeExecutableSchema(registry, TestUtil.getMockRuntimeWiring())

then:
noExceptionThrown()
}

def "mutually referencing oneOf types with scalar escape is inhabited"() {
def sdl = """
type Query {
f(arg : A) : String
}

input A @oneOf {
b : B
}

input B @oneOf {
a : A
escape : Int
}
"""

when:
def registry = new SchemaParser().parse(sdl)
new SchemaGenerator().makeExecutableSchema(registry, TestUtil.getMockRuntimeWiring())

then:
noExceptionThrown()
}

def "oneOf referencing non-oneOf with back-reference is inhabited"() {
def sdl = """
type Query {
f(arg : A) : String
}

input A @oneOf {
b : RegularInput
}

input RegularInput {
back : A
}
"""

when:
def registry = new SchemaParser().parse(sdl)
new SchemaGenerator().makeExecutableSchema(registry, TestUtil.getMockRuntimeWiring())

then:
noExceptionThrown()
}

def "multiple fields with chained oneOf escape is inhabited"() {
def sdl = """
type Query {
f(arg : A) : String
}

input A @oneOf {
b : B
c : C
}

input B @oneOf {
a : A
}

input C @oneOf {
a : A
escape : String
}
"""

when:
def registry = new SchemaParser().parse(sdl)
new SchemaGenerator().makeExecutableSchema(registry, TestUtil.getMockRuntimeWiring())

then:
noExceptionThrown()
}

def "single oneOf self-reference cycle is not inhabited"() {
def sdl = """
type Query {
f(arg : A) : String
}

input A @oneOf {
self : A
}
"""

when:
def registry = new SchemaParser().parse(sdl)
new SchemaGenerator().makeExecutableSchema(registry, TestUtil.getMockRuntimeWiring())

then:
def schemaProblem = thrown(InvalidSchemaException)
schemaProblem.errors.size() == 1
schemaProblem.errors[0].classification == SchemaValidationErrorType.OneOfNotInhabited
}

def "multiple oneOf types forming cycle are not inhabited"() {
def sdl = """
type Query {
f(arg : A) : String
}

input A @oneOf {
b : B
}

input B @oneOf {
c : C
}

input C @oneOf {
a : A
}
"""

when:
def registry = new SchemaParser().parse(sdl)
new SchemaGenerator().makeExecutableSchema(registry, TestUtil.getMockRuntimeWiring())

then:
def schemaProblem = thrown(InvalidSchemaException)
schemaProblem.errors.size() == 3
schemaProblem.errors[0].classification == SchemaValidationErrorType.OneOfNotInhabited
schemaProblem.errors[1].classification == SchemaValidationErrorType.OneOfNotInhabited
schemaProblem.errors[2].classification == SchemaValidationErrorType.OneOfNotInhabited
}
}
Loading