From 3e617f098769f9bfcff3edf342764f3ff5112b1f Mon Sep 17 00:00:00 2001 From: Andreas Marek Date: Mon, 24 Aug 2026 09:08:13 +1000 Subject: [PATCH 1/6] Simplify circular default value validation Port the focused cleanup from the 26.x backport in #4450 to master. --- .../validation/DefaultValuesAreValid.java | 16 +- .../NoDefaultValueCircularRefs.java | 237 +++++++----------- .../schema/validation/SchemaValidator.java | 11 +- .../CircularInputDefaultValuesTest.groovy | 90 ------- .../NoDefaultValueCircularRefsTest.groovy | 229 +++++++---------- .../validation/SchemaValidatorTest.groovy | 26 +- 6 files changed, 222 insertions(+), 387 deletions(-) delete mode 100644 src/test/groovy/graphql/CircularInputDefaultValuesTest.groovy diff --git a/src/main/java/graphql/schema/validation/DefaultValuesAreValid.java b/src/main/java/graphql/schema/validation/DefaultValuesAreValid.java index 4527973461..fb373ba919 100644 --- a/src/main/java/graphql/schema/validation/DefaultValuesAreValid.java +++ b/src/main/java/graphql/schema/validation/DefaultValuesAreValid.java @@ -47,7 +47,7 @@ public TraversalControl visitGraphQLInputObjectField(GraphQLInputObjectField inp !validationUtil.isValidLiteralValue((Value) defaultValue.getValue(), inputObjectField.getType(), schema, graphQLContext, Locale.getDefault())) { invalid = true; } else if (defaultValue.isExternal() && - !isValidExternalValue(schema, defaultValue.getValue(), inputObjectField.getType(), graphQLContext)) { + !isValidExternalValue(schema, defaultValue.getValue(), inputObjectField.getType(), graphQLContext, errorCollector)) { invalid = true; } if (invalid) { @@ -70,7 +70,7 @@ public TraversalControl visitGraphQLArgument(GraphQLArgument argument, Traverser !validationUtil.isValidLiteralValue((Value) defaultValue.getValue(), argument.getType(), schema, graphQLContext, Locale.getDefault())) { invalid = true; } else if (defaultValue.isExternal() && - !isValidExternalValue(schema, defaultValue.getValue(), argument.getType(), graphQLContext)) { + !isValidExternalValue(schema, defaultValue.getValue(), argument.getType(), graphQLContext, errorCollector)) { invalid = true; } if (invalid) { @@ -80,7 +80,17 @@ public TraversalControl visitGraphQLArgument(GraphQLArgument argument, Traverser return TraversalControl.CONTINUE; } - private boolean isValidExternalValue(GraphQLSchema schema, Object externalValue, GraphQLInputType type, GraphQLContext graphQLContext) { + private boolean isValidExternalValue( + GraphQLSchema schema, + Object externalValue, + GraphQLInputType type, + GraphQLContext graphQLContext, + SchemaValidationErrorCollector errorCollector + ) { + // Coercion expands nested field defaults. Avoid recursing into a cycle that has already made the schema invalid. + if (errorCollector.containsValidationError(SchemaValidationErrorType.DefaultValueCircularRef)) { + return true; + } try { ValuesResolver.externalValueToInternalValue(schema.getCodeRegistry().getFieldVisibility(), externalValue, type, graphQLContext, Locale.getDefault()); return true; diff --git a/src/main/java/graphql/schema/validation/NoDefaultValueCircularRefs.java b/src/main/java/graphql/schema/validation/NoDefaultValueCircularRefs.java index d33d4526a1..4e39dfe6fc 100644 --- a/src/main/java/graphql/schema/validation/NoDefaultValueCircularRefs.java +++ b/src/main/java/graphql/schema/validation/NoDefaultValueCircularRefs.java @@ -5,14 +5,17 @@ import graphql.language.ObjectField; import graphql.language.ObjectValue; import graphql.language.Value; +import graphql.schema.GraphQLArgument; import graphql.schema.GraphQLInputObjectField; import graphql.schema.GraphQLInputObjectType; import graphql.schema.GraphQLSchemaElement; import graphql.schema.GraphQLType; import graphql.schema.GraphQLTypeVisitorStub; import graphql.schema.InputValueWithState; +import graphql.util.FpKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; +import org.jspecify.annotations.Nullable; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -42,198 +45,146 @@ @Internal public class NoDefaultValueCircularRefs extends GraphQLTypeVisitorStub { - // Coordinates already fully traversed without finding a cycle, used to avoid duplicate error reports - // when the same coordinate is reachable from multiple input object types. - private final Set fullyExplored = new LinkedHashSet<>(); - - // The spec's "visitedFields" set, tracked as coordinate strings ("Type.field"). - // The spec creates a new immutable set at each step; this implementation mutates and backtracks - // for the same effect. - private final LinkedHashSet visitedFields = new LinkedHashSet<>(); + private final Set checkedFields = new LinkedHashSet<>(); + private final LinkedHashSet fieldPath = new LinkedHashSet<>(); @Override public TraversalControl visitGraphQLInputObjectType(GraphQLInputObjectType type, TraverserContext context) { - SchemaValidationErrorCollector errorCollector = context.getVarFromParents(SchemaValidationErrorCollector.class); - - // Implements InputObjectDefaultValueHasCycle(inputObject) from the spec: - // "If defaultValue is not provided, initialize it to an empty unordered map." - inputObjectDefaultValueHasCycle(type, ObjectValue.newObjectValue().build(), errorCollector); - + checkType(type, getErrorCollector(context)); return TraversalControl.CONTINUE; } - /** - * Implements {@code InputObjectDefaultValueHasCycle(inputObject, defaultValue, visitedFields)} - * from the spec, for literal (AST) default values. - */ - private void inputObjectDefaultValueHasCycle( - GraphQLInputObjectType inputObject, - Value defaultValue, - SchemaValidationErrorCollector errorCollector - ) { - // "If defaultValue is a list: for each itemValue in defaultValue..." - if (defaultValue instanceof ArrayValue) { - for (Value itemValue : ((ArrayValue) defaultValue).getValues()) { - inputObjectDefaultValueHasCycle(inputObject, itemValue, errorCollector); - } - return; - } - - // "Otherwise, if defaultValue is an unordered map..." - if (!(defaultValue instanceof ObjectValue)) { - return; - } - - ObjectValue objectValue = (ObjectValue) defaultValue; - Map> defaultValueMap = new LinkedHashMap<>(); - for (ObjectField field : objectValue.getObjectFields()) { - defaultValueMap.put(field.getName(), field.getValue()); + @Override + public TraversalControl visitGraphQLArgument(GraphQLArgument argument, TraverserContext context) { + GraphQLType namedType = unwrapAll(argument.getType()); + if (namedType instanceof GraphQLInputObjectType) { + checkType((GraphQLInputObjectType) namedType, getErrorCollector(context)); } + return TraversalControl.CONTINUE; + } - // "For each field in inputObject: if InputFieldDefaultValueHasCycle(...)" - for (GraphQLInputObjectField field : inputObject.getFieldDefinitions()) { - String fieldName = field.getName(); - boolean hasDefaultValue = defaultValueMap.containsKey(fieldName); - if (!hasDefaultValue && field.getInputFieldDefaultValue().isNotSet()) { - continue; - } - - GraphQLType namedFieldType = unwrapAll(field.getType()); - if (!(namedFieldType instanceof GraphQLInputObjectType)) { + private void checkType(GraphQLInputObjectType type, SchemaValidationErrorCollector errorCollector) { + for (GraphQLInputObjectField field : type.getFieldDefinitions()) { + GraphQLInputObjectType fieldType = getInputObjectType(field); + if (fieldType == null) { continue; } - - GraphQLInputObjectType fieldInputObject = (GraphQLInputObjectType) namedFieldType; - if (hasDefaultValue) { - // "Let fieldDefaultValue be the value for fieldName in defaultValue. - // If fieldDefaultValue exists: InputObjectDefaultValueHasCycle(namedFieldType, fieldDefaultValue, visitedFields)" - inputObjectDefaultValueHasCycle(fieldInputObject, defaultValueMap.get(fieldName), errorCollector); - } else { - // "Otherwise: let fieldDefaultValue be the default value of field..." - inputFieldDefaultValueHasCycle(field, fieldInputObject, inputObject.getName(), errorCollector); - } + checkFieldDefaultValue(field, fieldType, type.getName(), errorCollector); } } - /** - * Implements {@code InputObjectDefaultValueHasCycle(inputObject, defaultValue, visitedFields)} - * from the spec, for external (programmatic Map/List) default values. - */ - private void inputObjectDefaultValueHasCycle( + private void checkValue( GraphQLInputObjectType inputObject, - Object defaultValue, + @Nullable Object value, SchemaValidationErrorCollector errorCollector ) { - // "If defaultValue is a list: for each itemValue in defaultValue..." - if (defaultValue instanceof Iterable) { - for (Object itemValue : (Iterable) defaultValue) { - if (itemValue != null) { - inputObjectDefaultValueHasCycle(inputObject, itemValue, errorCollector); - } + if (value == null) { + return; + } + if (value instanceof ArrayValue) { + for (Value itemValue : ((ArrayValue) value).getValues()) { + checkValue(inputObject, itemValue, errorCollector); } return; } - - // "Otherwise, if defaultValue is an unordered map..." - if (!(defaultValue instanceof Map)) { + if (FpKit.isIterable(value)) { + for (Object itemValue : FpKit.toIterable(value)) { + checkValue(inputObject, itemValue, errorCollector); + } return; } - @SuppressWarnings("unchecked") - Map defaultValueMap = (Map) defaultValue; + Map valueMap = getValueMap(value); + if (valueMap == null) { + return; + } + checkObjectValue(inputObject, valueMap, errorCollector); + } - // "For each field in inputObject: if InputFieldDefaultValueHasCycle(...)" + private void checkObjectValue( + GraphQLInputObjectType inputObject, + Map valueMap, + SchemaValidationErrorCollector errorCollector + ) { for (GraphQLInputObjectField field : inputObject.getFieldDefinitions()) { - String fieldName = field.getName(); - boolean hasDefaultValue = defaultValueMap.containsKey(fieldName); - if (!hasDefaultValue && field.getInputFieldDefaultValue().isNotSet()) { + boolean hasValue = valueMap.containsKey(field.getName()); + if (!hasValue && field.getInputFieldDefaultValue().isNotSet()) { continue; } - GraphQLType namedFieldType = unwrapAll(field.getType()); - if (!(namedFieldType instanceof GraphQLInputObjectType)) { + GraphQLInputObjectType fieldType = getInputObjectType(field); + if (fieldType == null) { continue; } - - GraphQLInputObjectType fieldInputObject = (GraphQLInputObjectType) namedFieldType; - if (hasDefaultValue) { - // "Let fieldDefaultValue be the value for fieldName in defaultValue. - // If fieldDefaultValue exists: InputObjectDefaultValueHasCycle(namedFieldType, fieldDefaultValue, visitedFields)" - Object fieldDefaultValue = defaultValueMap.get(fieldName); - if (fieldDefaultValue != null) { - inputObjectDefaultValueHasCycle(fieldInputObject, fieldDefaultValue, errorCollector); - } - } else { - // "Otherwise: let fieldDefaultValue be the default value of field..." - inputFieldDefaultValueHasCycle(field, fieldInputObject, inputObject.getName(), errorCollector); + if (hasValue) { + checkValue(fieldType, valueMap.get(field.getName()), errorCollector); + continue; } + checkFieldDefaultValue(field, fieldType, inputObject.getName(), errorCollector); } } - /** - * Implements the "Otherwise" branch of {@code InputFieldDefaultValueHasCycle(field, defaultValue, visitedFields)} - * from the spec — called when the field is not present in the parent's default value, - * so the field's own default will be used at runtime. - */ - private void inputFieldDefaultValueHasCycle( + private void checkFieldDefaultValue( GraphQLInputObjectField field, - GraphQLInputObjectType namedFieldType, + GraphQLInputObjectType fieldType, String parentTypeName, SchemaValidationErrorCollector errorCollector ) { - // "Let fieldDefaultValue be the default value of field. - // If fieldDefaultValue does not exist: return false." - InputValueWithState fieldDefaultValue = field.getInputFieldDefaultValue(); - if (fieldDefaultValue.isNotSet()) { + InputValueWithState defaultValue = field.getInputFieldDefaultValue(); + if (!defaultValue.isLiteral() && !defaultValue.isExternal()) { return; } String coordinate = parentTypeName + "." + field.getName(); + if (fieldPath.contains(coordinate)) { + addError(coordinate, errorCollector); + return; + } + if (!checkedFields.add(coordinate)) { + return; + } - // "If field is within visitedFields: return true." - if (visitedFields.contains(coordinate)) { - // Cycle found — collect intermediate nodes (everything after the coordinate itself) - List intermediaries = new ArrayList<>(); - boolean found = false; - for (String entry : visitedFields) { - if (found) { - intermediaries.add(entry); - } - if (entry.equals(coordinate)) { - found = true; - } - } + fieldPath.add(coordinate); + checkValue(fieldType, defaultValue.getValue(), errorCollector); + fieldPath.remove(coordinate); + } - String message; - if (intermediaries.isEmpty()) { - message = "Invalid circular reference. The default value of Input Object field " - + coordinate + " references itself."; - } else { - message = "Invalid circular reference. The default value of Input Object field " - + coordinate + " references itself via the default values of: " - + String.join(", ", intermediaries) + "."; - } + private void addError(String coordinate, SchemaValidationErrorCollector errorCollector) { + List path = new ArrayList<>(fieldPath); + List intermediaries = path.subList(path.indexOf(coordinate) + 1, path.size()); + String via = intermediaries.isEmpty() + ? "" + : " via the default values of: " + String.join(", ", intermediaries); + String message = "Invalid circular reference. The default value of Input Object field " + + coordinate + " references itself" + via + "."; + errorCollector.addError(new SchemaValidationError( + SchemaValidationErrorType.DefaultValueCircularRef, message)); + } - errorCollector.addError(new SchemaValidationError( - SchemaValidationErrorType.DefaultValueCircularRef, message)); - return; + private @Nullable GraphQLInputObjectType getInputObjectType(GraphQLInputObjectField field) { + GraphQLType type = unwrapAll(field.getType()); + if (type instanceof GraphQLInputObjectType) { + return (GraphQLInputObjectType) type; } + return null; + } - if (fullyExplored.contains(coordinate)) { - return; + private @Nullable Map getValueMap(Object value) { + if (value instanceof Map) { + return (Map) value; + } + if (!(value instanceof ObjectValue)) { + return null; } - fullyExplored.add(coordinate); - - // "Let nextVisitedFields be a new set containing field and everything from visitedFields. - // Return InputObjectDefaultValueHasCycle(namedFieldType, fieldDefaultValue, nextVisitedFields)." - visitedFields.add(coordinate); - if (fieldDefaultValue.isLiteral() && fieldDefaultValue.getValue() instanceof Value) { - inputObjectDefaultValueHasCycle(namedFieldType, (Value) fieldDefaultValue.getValue(), errorCollector); - } else if (fieldDefaultValue.isExternal() && fieldDefaultValue.getValue() != null) { - inputObjectDefaultValueHasCycle(namedFieldType, fieldDefaultValue.getValue(), errorCollector); + Map> valueMap = new LinkedHashMap<>(); + for (ObjectField field : ((ObjectValue) value).getObjectFields()) { + valueMap.put(field.getName(), field.getValue()); } + return valueMap; + } - visitedFields.remove(coordinate); + private SchemaValidationErrorCollector getErrorCollector(TraverserContext context) { + return context.getVarFromParents(SchemaValidationErrorCollector.class); } } diff --git a/src/main/java/graphql/schema/validation/SchemaValidator.java b/src/main/java/graphql/schema/validation/SchemaValidator.java index fac409378a..5e8432b0a9 100644 --- a/src/main/java/graphql/schema/validation/SchemaValidator.java +++ b/src/main/java/graphql/schema/validation/SchemaValidator.java @@ -14,10 +14,8 @@ @Internal public class SchemaValidator { - - private final List rules = new ArrayList<>(); - - public SchemaValidator() { + public List getRules() { + List rules = new ArrayList<>(); rules.add(new NoUnbrokenInputCycles()); rules.add(new NoDefaultValueCircularRefs()); rules.add(new TypesImplementInterfaces()); @@ -28,9 +26,6 @@ public SchemaValidator() { rules.add(new InputAndOutputTypesUsedAppropriately()); rules.add(new OneOfInputObjectRules()); rules.add(new DeprecatedInputObjectAndArgumentsAreValid()); - } - - public List getRules() { return rules; } @@ -39,7 +34,7 @@ public Set validateSchema(GraphQLSchema schema) { Map, Object> rootVars = new LinkedHashMap<>(); rootVars.put(GraphQLSchema.class, schema); rootVars.put(SchemaValidationErrorCollector.class, validationErrorCollector); - new SchemaTraverser().depthFirstFullSchema(rules, schema, rootVars); + new SchemaTraverser().depthFirstFullSchema(getRules(), schema, rootVars); return validationErrorCollector.getErrors(); } diff --git a/src/test/groovy/graphql/CircularInputDefaultValuesTest.groovy b/src/test/groovy/graphql/CircularInputDefaultValuesTest.groovy deleted file mode 100644 index 43a92975e3..0000000000 --- a/src/test/groovy/graphql/CircularInputDefaultValuesTest.groovy +++ /dev/null @@ -1,90 +0,0 @@ -package graphql - -import graphql.schema.validation.InvalidSchemaException -import spock.lang.Specification - -/** - * Tests for mutually recursive input types with default values. - * - * These schemas are now rejected at build time by NoDefaultValueCircularRefs, - * which detects circular references in input object field default values. - * - * Previously, graphql-java accepted these schemas at build time but hit a - * StackOverflowError at query execution time when the circular defaults were - * expanded in ValuesResolverConversion.defaultValueToInternalValue. - */ -class CircularInputDefaultValuesTest extends Specification { - - def "mutually recursive input types with default values - rejected at schema build time"() { - when: - TestUtil.schema(''' - type Query { - test(arg: A): String - } - input A { b: B = {} } - input B { a: A = {} } - ''') - - then: - def e = thrown(InvalidSchemaException) - e.message.contains("Invalid circular reference") - } - - def "self-referential input type with default value - rejected at schema build time"() { - when: - TestUtil.schema(''' - type Query { - test(arg: A): String - } - input A { a: A = {} } - ''') - - then: - def e = thrown(InvalidSchemaException) - e.message.contains("Invalid circular reference") - } - - def "mutually recursive input types with default values - rejected before query execution"() { - when: - TestUtil.schema(''' - type Query { - test(arg: A): String - } - input A { b: B = {} } - input B { a: A = {} } - ''') - - then: - def e = thrown(InvalidSchemaException) - e.message.contains("Invalid circular reference") - } - - def "self-referential input type with default value - rejected before query execution"() { - when: - TestUtil.schema(''' - type Query { - test(arg: A): String - } - input A { a: A = {} } - ''') - - then: - def e = thrown(InvalidSchemaException) - e.message.contains("Invalid circular reference") - } - - def "mutually recursive defaults via argument default - rejected at schema build time"() { - when: - TestUtil.schema(''' - type Query { - test(arg: A = {}): String - } - input A { b: B = {} } - input B { a: A = {} } - ''') - - then: - def e = thrown(InvalidSchemaException) - e.message.contains("Invalid circular reference") - } -} diff --git a/src/test/groovy/graphql/schema/validation/NoDefaultValueCircularRefsTest.groovy b/src/test/groovy/graphql/schema/validation/NoDefaultValueCircularRefsTest.groovy index bd35006c25..29b376236c 100644 --- a/src/test/groovy/graphql/schema/validation/NoDefaultValueCircularRefsTest.groovy +++ b/src/test/groovy/graphql/schema/validation/NoDefaultValueCircularRefsTest.groovy @@ -1,115 +1,74 @@ package graphql.schema.validation import graphql.TestUtil +import graphql.schema.GraphQLSchema import spock.lang.Specification +import static graphql.Scalars.GraphQLString +import static graphql.schema.GraphQLArgument.newArgument +import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition +import static graphql.schema.GraphQLInputObjectField.newInputObjectField +import static graphql.schema.GraphQLInputObjectType.newInputObject +import static graphql.schema.GraphQLList.list +import static graphql.schema.GraphQLObjectType.newObject +import static graphql.schema.GraphQLSchema.newSchema +import static graphql.schema.GraphQLTypeReference.typeRef + class NoDefaultValueCircularRefsTest extends Specification { - def "self-referential default value is rejected"() { + + def "circular SDL defaults are rejected: #description"() { when: - TestUtil.schema(''' - type Query { test(arg: A): String } - input A { x: A = {} } - ''') + TestUtil.schema(sdl) then: - def e = thrown(InvalidSchemaException) - e.message.contains("Invalid circular reference. The default value of Input Object field A.x references itself.") - } + def exception = thrown(InvalidSchemaException) + exception.message.contains(expectedMessage) - def "mutual recursion through defaults is rejected"() { - when: - TestUtil.schema(''' + where: + description | sdl | expectedMessage + "self reference" | ''' + type Query { test(arg: A): String } + input A { x: A = {} } + ''' | "Invalid circular reference. The default value of Input Object field A.x references itself." + "mutual types" | ''' type Query { test(arg: A): String } input A { b: B = {} } input B { a: A = {} } - ''') - - then: - def e = thrown(InvalidSchemaException) - e.message.contains("Invalid circular reference") - e.message.contains("A.b") - } - - def "transitive cycle through three types is rejected"() { - when: - TestUtil.schema(''' + ''' | "Invalid circular reference" + "three types" | ''' type Query { test(arg: B): String } input B { x: B2 = {} } input B2 { x: B3 = {} } input B3 { x: B = {} } - ''') - - then: - def e = thrown(InvalidSchemaException) - e.message.contains("Invalid circular reference. The default value of Input Object field B.x references itself via the default values of: B2.x, B3.x.") - } - - def "self-reference through list wrapping"() { - when: - TestUtil.schema(''' + ''' | "Invalid circular reference. The default value of Input Object field B.x references itself via the default values of: B2.x, B3.x." + "list wrapping" | ''' type Query { test(arg: C): String } input C { x: [C] = [{}] } - ''') - - then: - def e = thrown(InvalidSchemaException) - e.message.contains("Invalid circular reference. The default value of Input Object field C.x references itself.") - } - - def "nested default value that eventually cycles"() { - when: - TestUtil.schema(''' + ''' | "Invalid circular reference. The default value of Input Object field C.x references itself." + "nested value" | ''' type Query { test(arg: D): String } input D { x: D = { x: { x: {} } } } - ''') - - then: - def e = thrown(InvalidSchemaException) - e.message.contains("Invalid circular reference. The default value of Input Object field D.x references itself.") - } - - def "cross-field cycle through defaults"() { - when: - TestUtil.schema(''' + ''' | "Invalid circular reference. The default value of Input Object field D.x references itself." + "cross field" | ''' type Query { test(arg: E): String } input E { x: E = { x: null } y: E = { y: null } } - ''') - - then: - def e = thrown(InvalidSchemaException) - e.message.contains("Invalid circular reference. The default value of Input Object field E.x references itself via the default values of: E.y.") - } - - def "cycle through non-null wrapping"() { - when: - TestUtil.schema(''' + ''' | "Invalid circular reference. The default value of Input Object field E.x references itself via the default values of: E.y." + "non-null type" | ''' type Query { test(arg: F): String } input F { x: F2! = {} } input F2 { x: F = { x: {} } } - ''') - - then: - def e = thrown(InvalidSchemaException) - e.message.contains("Invalid circular reference. The default value of Input Object field F2.x references itself.") - } - - def "partial default with non-provided recursive field"() { - when: - TestUtil.schema(''' + ''' | "Invalid circular reference. The default value of Input Object field F2.x references itself." + "omitted field" | ''' type Query { test(arg: A): String } input A { x: B = {name: "hi"} } input B { name: String a: A = {} } - ''') - - then: - def e = thrown(InvalidSchemaException) - e.message.contains("Invalid circular reference. The default value of Input Object field A.x references itself via the default values of: B.a.") + ''' | "Invalid circular reference. The default value of Input Object field A.x references itself via the default values of: B.a." } def "multiple independent cycles are reported"() { @@ -121,82 +80,88 @@ class NoDefaultValueCircularRefsTest extends Specification { ''') then: - def e = thrown(InvalidSchemaException) - e.message.contains("A.x references itself") - e.message.contains("P.x references itself") + def exception = thrown(InvalidSchemaException) + exception.message.contains("A.x references itself") + exception.message.contains("P.x references itself") } - def "explicit field in default breaks cycle"() { + def "non-circular SDL defaults are accepted: #description"() { when: - def schema = TestUtil.schema(''' - type Query { test(arg: A): String } - input A { b: B = {a: null} } - input B { a: A = {} } - ''') + def schema = TestUtil.schema(sdl) then: - noExceptionThrown() schema.getType("A") != null - } - def "recursive field without default does not cycle"() { - when: - def schema = TestUtil.schema(''' + where: + description | sdl + "explicit nested null" | ''' + type Query { test(arg: A): String } + input A { b: B = {a: null} } + input B { a: A = {} } + ''' + "recursive field unset" | ''' type Query { test(arg: A): String } input A { b: B = {} } input B { a: A } - ''') - - then: - noExceptionThrown() - schema.getType("A") != null - } - - def "scalar default value does not cycle"() { - when: - def schema = TestUtil.schema(''' + ''' + "scalar default" | ''' type Query { test(arg: A): String } input A { name: String = "hi" } - ''') - - then: - noExceptionThrown() - schema.getType("A") != null - } - - def "null literal default does not cycle"() { - when: - def schema = TestUtil.schema(''' + ''' + "null default" | ''' type Query { test(arg: A): String } input A { x: A = null } - ''') - - then: - noExceptionThrown() - schema.getType("A") != null + ''' + "empty list" | ''' + type Query { test(arg: A): String } + input A { x: [A] = [] } + ''' + "explicit self field null" | ''' + type Query { test(arg: A): String } + input A { x: A = {x: null} } + ''' } - def "empty list default does not cycle"() { + def "circular programmatic defaults are rejected: #description"() { when: - def schema = TestUtil.schema(''' - type Query { test(arg: A): String } - input A { x: [A] = [] } - ''') + buildProgrammaticSchema(defaultValue, listType) then: - noExceptionThrown() - schema.getType("A") != null + def exception = thrown(InvalidSchemaException) + exception.message.contains("Invalid circular reference") + + where: + description | defaultValue | listType + "map" | [:] | false + "list" | [[:]] | true + "Java array" | ([[:]] as Object[]) | true } - def "explicit null on recursive field breaks self-reference"() { - when: - def schema = TestUtil.schema(''' - type Query { test(arg: A): String } - input A { x: A = {x: null} } - ''') + def "explicit null breaks a programmatic default cycle"() { + expect: + buildProgrammaticSchema([x: null], false).getType("A") != null + } - then: - noExceptionThrown() - schema.getType("A") != null + private static GraphQLSchema buildProgrammaticSchema(Object defaultValue, boolean listType) { + def recursiveType = typeRef("A") + def fieldType = listType ? list(recursiveType) : recursiveType + def inputType = newInputObject() + .name("A") + .field(newInputObjectField() + .name("x") + .type(fieldType) + .defaultValueProgrammatic(defaultValue)) + .build() + def queryType = newObject() + .name("Query") + .field(newFieldDefinition() + .name("test") + .type(GraphQLString) + .argument(newArgument() + .name("arg") + .type(inputType) + .defaultValueProgrammatic([:]))) + .build() + return newSchema().query(queryType).build() } } diff --git a/src/test/groovy/graphql/schema/validation/SchemaValidatorTest.groovy b/src/test/groovy/graphql/schema/validation/SchemaValidatorTest.groovy index a854129d41..7a9404f93a 100644 --- a/src/test/groovy/graphql/schema/validation/SchemaValidatorTest.groovy +++ b/src/test/groovy/graphql/schema/validation/SchemaValidatorTest.groovy @@ -10,17 +10,21 @@ class SchemaValidatorTest extends Specification { when: def validator = new SchemaValidator() def rules = validator.rules + def nextRules = validator.rules + then: - rules.size() == 10 - rules[0] instanceof NoUnbrokenInputCycles - rules[1] instanceof NoDefaultValueCircularRefs - rules[2] instanceof TypesImplementInterfaces - rules[3] instanceof TypeAndFieldRule - rules[4] instanceof DefaultValuesAreValid - rules[5] instanceof AppliedDirectivesAreValid - rules[6] instanceof AppliedDirectiveArgumentsAreValid - rules[7] instanceof InputAndOutputTypesUsedAppropriately - rules[8] instanceof OneOfInputObjectRules - rules[9] instanceof DeprecatedInputObjectAndArgumentsAreValid + rules*.class == [ + NoUnbrokenInputCycles, + NoDefaultValueCircularRefs, + TypesImplementInterfaces, + TypeAndFieldRule, + DefaultValuesAreValid, + AppliedDirectivesAreValid, + AppliedDirectiveArgumentsAreValid, + InputAndOutputTypesUsedAppropriately, + OneOfInputObjectRules, + DeprecatedInputObjectAndArgumentsAreValid, + ] + rules[1] !== nextRules[1] } } From f180dcf5eb1106067c67c1420103ad8792dfec8e Mon Sep 17 00:00:00 2001 From: Andreas Marek Date: Mon, 24 Aug 2026 09:19:08 +1000 Subject: [PATCH 2/6] Restore circular default integration coverage Keep the higher-level SDL and argument-default regression tests on master. --- .../CircularInputDefaultValuesTest.groovy | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/test/groovy/graphql/CircularInputDefaultValuesTest.groovy diff --git a/src/test/groovy/graphql/CircularInputDefaultValuesTest.groovy b/src/test/groovy/graphql/CircularInputDefaultValuesTest.groovy new file mode 100644 index 0000000000..43a92975e3 --- /dev/null +++ b/src/test/groovy/graphql/CircularInputDefaultValuesTest.groovy @@ -0,0 +1,90 @@ +package graphql + +import graphql.schema.validation.InvalidSchemaException +import spock.lang.Specification + +/** + * Tests for mutually recursive input types with default values. + * + * These schemas are now rejected at build time by NoDefaultValueCircularRefs, + * which detects circular references in input object field default values. + * + * Previously, graphql-java accepted these schemas at build time but hit a + * StackOverflowError at query execution time when the circular defaults were + * expanded in ValuesResolverConversion.defaultValueToInternalValue. + */ +class CircularInputDefaultValuesTest extends Specification { + + def "mutually recursive input types with default values - rejected at schema build time"() { + when: + TestUtil.schema(''' + type Query { + test(arg: A): String + } + input A { b: B = {} } + input B { a: A = {} } + ''') + + then: + def e = thrown(InvalidSchemaException) + e.message.contains("Invalid circular reference") + } + + def "self-referential input type with default value - rejected at schema build time"() { + when: + TestUtil.schema(''' + type Query { + test(arg: A): String + } + input A { a: A = {} } + ''') + + then: + def e = thrown(InvalidSchemaException) + e.message.contains("Invalid circular reference") + } + + def "mutually recursive input types with default values - rejected before query execution"() { + when: + TestUtil.schema(''' + type Query { + test(arg: A): String + } + input A { b: B = {} } + input B { a: A = {} } + ''') + + then: + def e = thrown(InvalidSchemaException) + e.message.contains("Invalid circular reference") + } + + def "self-referential input type with default value - rejected before query execution"() { + when: + TestUtil.schema(''' + type Query { + test(arg: A): String + } + input A { a: A = {} } + ''') + + then: + def e = thrown(InvalidSchemaException) + e.message.contains("Invalid circular reference") + } + + def "mutually recursive defaults via argument default - rejected at schema build time"() { + when: + TestUtil.schema(''' + type Query { + test(arg: A = {}): String + } + input A { b: B = {} } + input B { a: A = {} } + ''') + + then: + def e = thrown(InvalidSchemaException) + e.message.contains("Invalid circular reference") + } +} From c17bd68e33c31d81c18e92977ab9223e82ad8174 Mon Sep 17 00:00:00 2001 From: Andreas Marek Date: Sun, 8 Nov 2026 09:55:08 +1000 Subject: [PATCH 3/6] Reject iterator and stream schema defaults --- .../graphql/schema/DefaultValueSupport.java | 45 ++++++ .../java/graphql/schema/GraphQLArgument.java | 7 + .../schema/GraphQLInputObjectField.java | 12 ++ .../NoDefaultValueCircularRefs.java | 2 +- src/main/java/graphql/util/FpKit.java | 4 + .../ProgrammaticDefaultValuesTest.groovy | 129 ++++++++++++++++++ src/test/groovy/graphql/util/FpKitTest.groovy | 20 +++ 7 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 src/main/java/graphql/schema/DefaultValueSupport.java create mode 100644 src/test/groovy/graphql/schema/ProgrammaticDefaultValuesTest.groovy diff --git a/src/main/java/graphql/schema/DefaultValueSupport.java b/src/main/java/graphql/schema/DefaultValueSupport.java new file mode 100644 index 0000000000..a6e195389e --- /dev/null +++ b/src/main/java/graphql/schema/DefaultValueSupport.java @@ -0,0 +1,45 @@ +package graphql.schema; + +import graphql.Internal; +import graphql.util.FpKit; +import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.Nullable; + +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.stream.BaseStream; + +import static graphql.Assert.assertTrue; + +@Internal +@NullMarked +public class DefaultValueSupport { + + public static void assertValidDefaultValue(@Nullable Object value) { + assertValidDefaultValue(value, null); + } + + private static void assertValidDefaultValue(@Nullable Object value, @Nullable Set visited) { + if (value == null) { + return; + } + assertTrue(!(value instanceof Iterator) && !(value instanceof BaseStream), + "Default values must not contain an Iterator or a Stream"); + if (!(value instanceof Map) && !FpKit.isIterableOrArray(value)) { + return; + } + if (visited == null) { + visited = Collections.newSetFromMap(new IdentityHashMap<>()); + } + if (!visited.add(value)) { + return; + } + Iterable values = value instanceof Map ? ((Map) value).values() : FpKit.toIterable(value); + for (Object item : values) { + assertValidDefaultValue(item, visited); + } + } +} diff --git a/src/main/java/graphql/schema/GraphQLArgument.java b/src/main/java/graphql/schema/GraphQLArgument.java index 924cdf1198..82cff5f6a5 100644 --- a/src/main/java/graphql/schema/GraphQLArgument.java +++ b/src/main/java/graphql/schema/GraphQLArgument.java @@ -373,6 +373,7 @@ public Builder type(GraphQLInputType type) { */ @Deprecated(since = "2021-05-10") public Builder defaultValue(Object defaultValue) { + DefaultValueSupport.assertValidDefaultValue(defaultValue); this.defaultValue = InputValueWithState.newInternalValue(defaultValue); return this; } @@ -388,11 +389,17 @@ public Builder defaultValueLiteral(@NonNull Value defaultValue) { } /** + * Sets a programmatic default value. Iterators and streams, including nested values, + * are not supported; use an iterable or array instead. + * * @param defaultValue Can be null to represent null value * * @return this builder + * + * @throws graphql.AssertException if the default value contains an iterator or stream */ public Builder defaultValueProgrammatic(@Nullable Object defaultValue) { + DefaultValueSupport.assertValidDefaultValue(defaultValue); this.defaultValue = InputValueWithState.newExternalValue(defaultValue); return this; } diff --git a/src/main/java/graphql/schema/GraphQLInputObjectField.java b/src/main/java/graphql/schema/GraphQLInputObjectField.java index 23323393b8..fb8f1beb1c 100644 --- a/src/main/java/graphql/schema/GraphQLInputObjectField.java +++ b/src/main/java/graphql/schema/GraphQLInputObjectField.java @@ -318,6 +318,7 @@ public Builder type(GraphQLInputType type) { */ @Deprecated(since = "2021-05-10") public Builder defaultValue(Object defaultValue) { + DefaultValueSupport.assertValidDefaultValue(defaultValue); this.defaultValue = InputValueWithState.newInternalValue(defaultValue); return this; } @@ -327,7 +328,18 @@ public Builder defaultValueLiteral(Value defaultValue) { return this; } + /** + * Sets a programmatic default value. Iterators and streams, including nested values, + * are not supported; use an iterable or array instead. + * + * @param defaultValue the default value, which can be null + * + * @return this builder + * + * @throws graphql.AssertException if the default value contains an iterator or stream + */ public Builder defaultValueProgrammatic(Object defaultValue) { + DefaultValueSupport.assertValidDefaultValue(defaultValue); this.defaultValue = InputValueWithState.newExternalValue(defaultValue); return this; } diff --git a/src/main/java/graphql/schema/validation/NoDefaultValueCircularRefs.java b/src/main/java/graphql/schema/validation/NoDefaultValueCircularRefs.java index 4e39dfe6fc..74bfa50b6d 100644 --- a/src/main/java/graphql/schema/validation/NoDefaultValueCircularRefs.java +++ b/src/main/java/graphql/schema/validation/NoDefaultValueCircularRefs.java @@ -87,7 +87,7 @@ private void checkValue( } return; } - if (FpKit.isIterable(value)) { + if (FpKit.isIterableOrArray(value)) { for (Object itemValue : FpKit.toIterable(value)) { checkValue(inputObject, itemValue, errorCollector); } diff --git a/src/main/java/graphql/util/FpKit.java b/src/main/java/graphql/util/FpKit.java index b4bf6ca60d..e006dd3bc1 100644 --- a/src/main/java/graphql/util/FpKit.java +++ b/src/main/java/graphql/util/FpKit.java @@ -178,6 +178,10 @@ public static boolean isIterable(Object result) { return result.getClass().isArray() || result instanceof Iterable || result instanceof Stream || result instanceof Iterator; } + public static boolean isIterableOrArray(Object result) { + return result instanceof Iterable || result.getClass().isArray(); + } + @SuppressWarnings("unchecked") public static Iterable toIterable(Object iterableResult) { diff --git a/src/test/groovy/graphql/schema/ProgrammaticDefaultValuesTest.groovy b/src/test/groovy/graphql/schema/ProgrammaticDefaultValuesTest.groovy new file mode 100644 index 0000000000..c0fa7ac5ff --- /dev/null +++ b/src/test/groovy/graphql/schema/ProgrammaticDefaultValuesTest.groovy @@ -0,0 +1,129 @@ +package graphql.schema + +import graphql.AssertException +import graphql.GraphQL +import spock.lang.Specification + +import java.util.stream.IntStream +import java.util.stream.Stream + +import static graphql.Scalars.GraphQLString +import static graphql.schema.GraphQLArgument.newArgument +import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition +import static graphql.schema.GraphQLInputObjectField.newInputObjectField +import static graphql.schema.GraphQLInputObjectType.newInputObject +import static graphql.schema.GraphQLList.list +import static graphql.schema.GraphQLObjectType.newObject +import static graphql.schema.GraphQLSchema.newSchema + +class ProgrammaticDefaultValuesTest extends Specification { + + def "#element #setter rejects #kind inside #shape without consuming it"() { + given: + def builder = element == 'argument' ? newArgument() : newInputObjectField() + def singleUseValue = singleUseValue(kind) + def defaultValue = wrapValue(singleUseValue, shape) + + when: + builder."$setter"(defaultValue) + + then: + def exception = thrown(AssertException) + exception.message == 'Default values must not contain an Iterator or a Stream' + kind == 'iterator' ? singleUseValue.next() == 'ok' : singleUseValue.count() == 1 + + where: + [element, setter, kind, shape] << [ + ['argument', 'input field'], + ['defaultValueProgrammatic', 'defaultValue'], + ['iterator', 'stream', 'primitive stream'], + ['direct', 'map', 'list', 'array', 'nested containers'] + ].combinations() + } + + def "#element defaults preserve #representation input values across executions"() { + given: + def defaultValue = repeatableValues(representation) + def leaf = newInputObject().name('Leaf') + .field(newInputObjectField().name('name').type(GraphQLString)).build() + def argument = newArgument().name('arg').type(list(leaf)) + def queryText = '{ test }' + if (element == 'input field') { + def root = newInputObject().name('Root') + .field(newInputObjectField().name('items').type(list(leaf)) + .defaultValueProgrammatic(defaultValue)).build() + argument.type(root) + queryText = '{ test(arg: {}) }' + } else { + argument.defaultValueProgrammatic(defaultValue) + } + def query = newObject().name('Query') + .field(newFieldDefinition().name('test').type(GraphQLString).argument(argument)).build() + DataFetcher fetcher = { environment -> + def value = environment.getArgument('arg') + def items = element == 'input field' ? value['items'] : value + items.collect { it['name'] }.join(',') + } + def registry = GraphQLCodeRegistry.newCodeRegistry() + .dataFetcher(FieldCoordinates.coordinates('Query', 'test'), fetcher).build() + def graphQL = GraphQL.newGraphQL(newSchema().query(query).codeRegistry(registry).build()).build() + + expect: + (1..2).every { + def result = graphQL.execute(queryText) + result.errors.empty && result.getData() == [test: 'ok'] + } + + where: + [element, representation] << [ + ['argument', 'input field'], + ['list', 'set', 'array', 'iterable'] + ].combinations() + } + + def "null and primitive arrays remain supported defaults"() { + expect: + newArgument().defaultValueProgrammatic(null) != null + newInputObjectField().defaultValueProgrammatic(null) != null + newArgument().defaultValueProgrammatic([1, 2] as int[]) != null + newInputObjectField().defaultValueProgrammatic([1, 2] as int[]) != null + } + + def "checking defaults handles shared and recursive Java containers by identity"() { + given: + def shared = [name: 'ok'] + def value = [first: shared, second: shared] + value['self'] = value + + expect: + newArgument().defaultValueProgrammatic(value) != null + newInputObjectField().defaultValueProgrammatic(value) != null + } + + private static Object singleUseValue(String kind) { + if (kind == 'iterator') { + return ['ok'].iterator() + } + return kind == 'stream' ? Stream.of('ok') : IntStream.of(1) + } + + private static Object wrapValue(Object value, String shape) { + switch (shape) { + case 'map': return [value: value] + case 'list': return [value] + case 'array': return [value] as Object[] + case 'nested containers': return [outer: [[value] as Object[]]] + default: return value + } + } + + private static Object repeatableValues(String representation) { + def values = [[name: 'ok']] + switch (representation) { + case 'array': return values as Object[] + case 'set': return values as Set + case 'iterable': return { -> values.iterator() } as Iterable + default: return values + } + } +} diff --git a/src/test/groovy/graphql/util/FpKitTest.groovy b/src/test/groovy/graphql/util/FpKitTest.groovy index 21dad8ef1f..3326cac629 100644 --- a/src/test/groovy/graphql/util/FpKitTest.groovy +++ b/src/test/groovy/graphql/util/FpKitTest.groovy @@ -4,6 +4,8 @@ import com.google.common.collect.ImmutableList import spock.lang.Specification import java.util.function.Supplier +import java.util.stream.IntStream +import java.util.stream.Stream class FpKitTest extends Specification { @@ -98,6 +100,24 @@ class FpKitTest extends Specification { l == ["Parrot"] } + def "isIterableOrArray excludes iterators and streams"() { + expect: + FpKit.isIterableOrArray(value) == expected + + where: + value | expected + [1, 2] | true + [1, 2] as Set | true + [1, 2] as Object[] | true + [1, 2] as int[] | true + new IterableThing([1, 2]) | true + [1, 2].iterator() | false + Stream.of(1, 2) | false + IntStream.of(1, 2) | false + [a: 1] | false + "hello" | false + } + class Person { String name String city From 52910ab8a7a5343087270839d0dd47c9d8044a0d Mon Sep 17 00:00:00 2001 From: Andreas Marek Date: Sun, 8 Nov 2026 10:06:03 +1000 Subject: [PATCH 4/6] Check default cycles before coercing schema values --- .../validation/DefaultValuesAreValid.java | 16 +- .../NoDefaultValueCircularRefs.java | 30 +-- .../schema/validation/SchemaValidator.java | 5 +- .../DefaultValueCyclePreflightTest.groovy | 177 ++++++++++++++++++ .../validation/SchemaValidatorTest.groovy | 3 +- 5 files changed, 193 insertions(+), 38 deletions(-) create mode 100644 src/test/groovy/graphql/schema/validation/DefaultValueCyclePreflightTest.groovy diff --git a/src/main/java/graphql/schema/validation/DefaultValuesAreValid.java b/src/main/java/graphql/schema/validation/DefaultValuesAreValid.java index fb373ba919..4527973461 100644 --- a/src/main/java/graphql/schema/validation/DefaultValuesAreValid.java +++ b/src/main/java/graphql/schema/validation/DefaultValuesAreValid.java @@ -47,7 +47,7 @@ public TraversalControl visitGraphQLInputObjectField(GraphQLInputObjectField inp !validationUtil.isValidLiteralValue((Value) defaultValue.getValue(), inputObjectField.getType(), schema, graphQLContext, Locale.getDefault())) { invalid = true; } else if (defaultValue.isExternal() && - !isValidExternalValue(schema, defaultValue.getValue(), inputObjectField.getType(), graphQLContext, errorCollector)) { + !isValidExternalValue(schema, defaultValue.getValue(), inputObjectField.getType(), graphQLContext)) { invalid = true; } if (invalid) { @@ -70,7 +70,7 @@ public TraversalControl visitGraphQLArgument(GraphQLArgument argument, Traverser !validationUtil.isValidLiteralValue((Value) defaultValue.getValue(), argument.getType(), schema, graphQLContext, Locale.getDefault())) { invalid = true; } else if (defaultValue.isExternal() && - !isValidExternalValue(schema, defaultValue.getValue(), argument.getType(), graphQLContext, errorCollector)) { + !isValidExternalValue(schema, defaultValue.getValue(), argument.getType(), graphQLContext)) { invalid = true; } if (invalid) { @@ -80,17 +80,7 @@ public TraversalControl visitGraphQLArgument(GraphQLArgument argument, Traverser return TraversalControl.CONTINUE; } - private boolean isValidExternalValue( - GraphQLSchema schema, - Object externalValue, - GraphQLInputType type, - GraphQLContext graphQLContext, - SchemaValidationErrorCollector errorCollector - ) { - // Coercion expands nested field defaults. Avoid recursing into a cycle that has already made the schema invalid. - if (errorCollector.containsValidationError(SchemaValidationErrorType.DefaultValueCircularRef)) { - return true; - } + private boolean isValidExternalValue(GraphQLSchema schema, Object externalValue, GraphQLInputType type, GraphQLContext graphQLContext) { try { ValuesResolver.externalValueToInternalValue(schema.getCodeRegistry().getFieldVisibility(), externalValue, type, graphQLContext, Locale.getDefault()); return true; diff --git a/src/main/java/graphql/schema/validation/NoDefaultValueCircularRefs.java b/src/main/java/graphql/schema/validation/NoDefaultValueCircularRefs.java index 74bfa50b6d..58682d808f 100644 --- a/src/main/java/graphql/schema/validation/NoDefaultValueCircularRefs.java +++ b/src/main/java/graphql/schema/validation/NoDefaultValueCircularRefs.java @@ -5,16 +5,13 @@ import graphql.language.ObjectField; import graphql.language.ObjectValue; import graphql.language.Value; -import graphql.schema.GraphQLArgument; import graphql.schema.GraphQLInputObjectField; import graphql.schema.GraphQLInputObjectType; -import graphql.schema.GraphQLSchemaElement; +import graphql.schema.GraphQLNamedType; +import graphql.schema.GraphQLSchema; import graphql.schema.GraphQLType; -import graphql.schema.GraphQLTypeVisitorStub; import graphql.schema.InputValueWithState; import graphql.util.FpKit; -import graphql.util.TraversalControl; -import graphql.util.TraverserContext; import org.jspecify.annotations.Nullable; import java.util.ArrayList; @@ -43,24 +40,17 @@ * @see Input Objects Type Validation */ @Internal -public class NoDefaultValueCircularRefs extends GraphQLTypeVisitorStub { +public class NoDefaultValueCircularRefs { private final Set checkedFields = new LinkedHashSet<>(); private final LinkedHashSet fieldPath = new LinkedHashSet<>(); - @Override - public TraversalControl visitGraphQLInputObjectType(GraphQLInputObjectType type, TraverserContext context) { - checkType(type, getErrorCollector(context)); - return TraversalControl.CONTINUE; - } - - @Override - public TraversalControl visitGraphQLArgument(GraphQLArgument argument, TraverserContext context) { - GraphQLType namedType = unwrapAll(argument.getType()); - if (namedType instanceof GraphQLInputObjectType) { - checkType((GraphQLInputObjectType) namedType, getErrorCollector(context)); + public void checkSchema(GraphQLSchema schema, SchemaValidationErrorCollector errorCollector) { + for (GraphQLNamedType type : schema.getTypeMap().values()) { + if (type instanceof GraphQLInputObjectType) { + checkType((GraphQLInputObjectType) type, errorCollector); + } } - return TraversalControl.CONTINUE; } private void checkType(GraphQLInputObjectType type, SchemaValidationErrorCollector errorCollector) { @@ -183,8 +173,4 @@ private void addError(String coordinate, SchemaValidationErrorCollector errorCol } return valueMap; } - - private SchemaValidationErrorCollector getErrorCollector(TraverserContext context) { - return context.getVarFromParents(SchemaValidationErrorCollector.class); - } } diff --git a/src/main/java/graphql/schema/validation/SchemaValidator.java b/src/main/java/graphql/schema/validation/SchemaValidator.java index 5e8432b0a9..37f2ce7107 100644 --- a/src/main/java/graphql/schema/validation/SchemaValidator.java +++ b/src/main/java/graphql/schema/validation/SchemaValidator.java @@ -17,7 +17,6 @@ public class SchemaValidator { public List getRules() { List rules = new ArrayList<>(); rules.add(new NoUnbrokenInputCycles()); - rules.add(new NoDefaultValueCircularRefs()); rules.add(new TypesImplementInterfaces()); rules.add(new TypeAndFieldRule()); rules.add(new DefaultValuesAreValid()); @@ -31,6 +30,10 @@ public List getRules() { public Set validateSchema(GraphQLSchema schema) { SchemaValidationErrorCollector validationErrorCollector = new SchemaValidationErrorCollector(); + new NoDefaultValueCircularRefs().checkSchema(schema, validationErrorCollector); + if (!validationErrorCollector.getErrors().isEmpty()) { + return validationErrorCollector.getErrors(); + } Map, Object> rootVars = new LinkedHashMap<>(); rootVars.put(GraphQLSchema.class, schema); rootVars.put(SchemaValidationErrorCollector.class, validationErrorCollector); diff --git a/src/test/groovy/graphql/schema/validation/DefaultValueCyclePreflightTest.groovy b/src/test/groovy/graphql/schema/validation/DefaultValueCyclePreflightTest.groovy new file mode 100644 index 0000000000..2307590a62 --- /dev/null +++ b/src/test/groovy/graphql/schema/validation/DefaultValueCyclePreflightTest.groovy @@ -0,0 +1,177 @@ +package graphql.schema.validation + +import graphql.language.NullValue +import graphql.language.ObjectValue +import graphql.schema.Coercing +import graphql.schema.GraphQLArgument +import graphql.schema.GraphQLCodeRegistry +import graphql.schema.GraphQLDirective +import graphql.schema.GraphQLInputObjectType +import graphql.schema.GraphQLNamedType +import graphql.schema.GraphQLObjectType +import graphql.schema.GraphQLSchema +import spock.lang.Specification + +import static graphql.Scalars.GraphQLString +import static graphql.introspection.Introspection.DirectiveLocation.FIELD_DEFINITION +import static graphql.schema.GraphQLAppliedDirective.newDirective as newAppliedDirective +import static graphql.schema.GraphQLAppliedDirectiveArgument.newArgument as newAppliedArgument +import static graphql.schema.GraphQLArgument.newArgument +import static graphql.schema.GraphQLDirective.newDirective +import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition +import static graphql.schema.GraphQLInputObjectField.newInputObjectField +import static graphql.schema.GraphQLInputObjectType.newInputObject +import static graphql.schema.GraphQLList.list +import static graphql.schema.GraphQLNonNull.nonNull +import static graphql.schema.GraphQLObjectType.newObject +import static graphql.schema.GraphQLScalarType.newScalar +import static graphql.schema.GraphQLSchema.newSchema +import static graphql.schema.GraphQLTypeReference.typeRef + +class DefaultValueCyclePreflightTest extends Specification { + + def "nested #representation argument default reports its cycle before coercion - fast #fast"() { + given: + def recursive = recursiveType('ZRecursive') + def childType = nonNull(typeRef('ZRecursive')) + def wrapper = newInputObject().name('AWrapper') + .field(newInputObjectField().name('child') + .type(representation == 'object' ? childType : list(childType))).build() + def value = representation == 'object' ? [:] : [[:]] + if (representation == 'array') { + value = value as Object[] + } + def query = queryWithArgument(newArgument().name('arg').type(wrapper) + .defaultValueProgrammatic([child: value]).build()) + + when: + buildSchema(query, [wrapper, recursive], fast) + + then: + def exception = thrown(InvalidSchemaException) + exception.errors*.classification == [SchemaValidationErrorType.DefaultValueCircularRef] + exception.message.contains('ZRecursive.next references itself') + + where: + [fast, representation] << [[false, true], ['object', 'list', 'array']].combinations() + } + + def "input types used only by directives are checked - fast #fast"() { + given: + def recursive = recursiveType('DirectiveInput') + def directive = newDirective().name('directive').validLocation(FIELD_DEFINITION) + .argument(newArgument().name('arg').type(recursive)).build() + def query = newObject().name('Query') + .field(newFieldDefinition().name('test').type(GraphQLString)).build() + + when: + buildSchema(query, fast ? [recursive] : [], fast, directive) + + then: + def exception = thrown(InvalidSchemaException) + exception.message.contains('DirectiveInput.next references itself') + + where: + fast << [false, true] + } + + def "all cycles are reported before any default or directive coercion - fast #fast"() { + given: + def coercing = Mock(Coercing) + def scalar = newScalar().name('Tracked').coercing(coercing).build() + def directive = newDirective().name('probe').validLocation(FIELD_DEFINITION) + .argument(newArgument().name('value').type(scalar)).build() + def applied = newAppliedDirective().name('probe') + .argument(newAppliedArgument().name('value').type(scalar) + .valueProgrammatic('applied')).build() + def query = newObject().name('Query') + .field(newFieldDefinition().name('test').type(GraphQLString) + .argument(newArgument().name('arg').type(scalar).defaultValueProgrammatic('default')) + .argument(newArgument().name('invalid').type(GraphQLString).defaultValueProgrammatic(42)) + .withAppliedDirective(applied)).build() + def input = newInputObject().name('Input') + .field(newInputObjectField().name('value').type(scalar).defaultValueProgrammatic('input')).build() + + when: + buildSchema(query, [scalar, input, recursiveType('First'), recursiveType('Second')], fast, directive) + + then: + def exception = thrown(InvalidSchemaException) + exception.errors.size() == 2 + exception.errors.every { it.classification == SchemaValidationErrorType.DefaultValueCircularRef } + exception.message.contains('First.next references itself') + exception.message.contains('Second.next references itself') + 0 * coercing._ + + where: + fast << [false, true] + } + + def "repeated validation does not retain cycle state between schemas or runs"() { + given: + def valid = uncheckedSchema(false) + def invalid = uncheckedSchema(true) + def validator = new SchemaValidator() + + when: + def before = validator.validateSchema(valid) + def first = validator.validateSchema(invalid) + def second = validator.validateSchema(invalid) + def after = validator.validateSchema(valid) + + then: + before.empty + first*.classification == [SchemaValidationErrorType.DefaultValueCircularRef] + first*.description == second*.description + after.empty + } + + def "terminating defaults are accepted through nested arguments - fast #fast default #defaultValue"() { + given: + def recursive = newInputObject().name('Recursive') + .field(newInputObjectField().name('next').type(list(typeRef('Recursive'))) + .defaultValueProgrammatic(defaultValue)).build() + def wrapper = newInputObject().name('Wrapper') + .field(newInputObjectField().name('child').type(recursive)).build() + def query = queryWithArgument(newArgument().name('arg').type(wrapper) + .defaultValueProgrammatic([child: [:]]).build()) + + expect: + buildSchema(query, [wrapper, recursive], fast).getType('Recursive') != null + + where: + [fast, defaultValue] << [[false, true], [null, []]].combinations() + } + + private static GraphQLInputObjectType recursiveType(String name) { + newInputObject().name(name) + .field(newInputObjectField().name('next').type(typeRef(name)) + .defaultValueProgrammatic([:])).build() + } + + private static GraphQLObjectType queryWithArgument(GraphQLArgument argument) { + newObject().name('Query') + .field(newFieldDefinition().name('test').type(GraphQLString).argument(argument)).build() + } + + private static GraphQLSchema uncheckedSchema(boolean cyclic) { + def input = newInputObject().name('Input') + .field(newInputObjectField().name('next').type(typeRef('Input')) + .defaultValueLiteral(cyclic ? ObjectValue.newObjectValue().build() : NullValue.of())).build() + def query = queryWithArgument(newArgument().name('arg').type(input).build()) + new GraphQLSchema.FastBuilder(GraphQLCodeRegistry.newCodeRegistry(), query, null, null) + .addType(input).build() + } + + private static GraphQLSchema buildSchema(GraphQLObjectType query, List types, + boolean fast, GraphQLDirective directive = null) { + def builder = fast + ? new GraphQLSchema.FastBuilder(GraphQLCodeRegistry.newCodeRegistry(), query, null, null) + .addTypes(types).withValidation(true) + : newSchema().query(query).additionalTypes(new LinkedHashSet<>(types)) + if (directive != null) { + builder.additionalDirective(directive) + } + builder.build() + } +} diff --git a/src/test/groovy/graphql/schema/validation/SchemaValidatorTest.groovy b/src/test/groovy/graphql/schema/validation/SchemaValidatorTest.groovy index 7a9404f93a..b79b8c5fda 100644 --- a/src/test/groovy/graphql/schema/validation/SchemaValidatorTest.groovy +++ b/src/test/groovy/graphql/schema/validation/SchemaValidatorTest.groovy @@ -15,7 +15,6 @@ class SchemaValidatorTest extends Specification { then: rules*.class == [ NoUnbrokenInputCycles, - NoDefaultValueCircularRefs, TypesImplementInterfaces, TypeAndFieldRule, DefaultValuesAreValid, @@ -25,6 +24,6 @@ class SchemaValidatorTest extends Specification { OneOfInputObjectRules, DeprecatedInputObjectAndArgumentsAreValid, ] - rules[1] !== nextRules[1] + (0.. Date: Sun, 8 Nov 2026 11:42:58 +1000 Subject: [PATCH 5/6] Reject unsupported default containers during schema validation --- .../graphql/schema/DefaultValueSupport.java | 45 ------ .../java/graphql/schema/GraphQLArgument.java | 6 +- .../schema/GraphQLInputObjectField.java | 6 +- .../DefaultValueContainersAreValid.java | 72 +++++++++ .../schema/validation/SchemaValidator.java | 13 +- .../ProgrammaticDefaultValuesTest.groovy | 147 ++++++++++++++++-- 6 files changed, 213 insertions(+), 76 deletions(-) delete mode 100644 src/main/java/graphql/schema/DefaultValueSupport.java create mode 100644 src/main/java/graphql/schema/validation/DefaultValueContainersAreValid.java diff --git a/src/main/java/graphql/schema/DefaultValueSupport.java b/src/main/java/graphql/schema/DefaultValueSupport.java deleted file mode 100644 index a6e195389e..0000000000 --- a/src/main/java/graphql/schema/DefaultValueSupport.java +++ /dev/null @@ -1,45 +0,0 @@ -package graphql.schema; - -import graphql.Internal; -import graphql.util.FpKit; -import org.jspecify.annotations.NullMarked; -import org.jspecify.annotations.Nullable; - -import java.util.Collections; -import java.util.IdentityHashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; -import java.util.stream.BaseStream; - -import static graphql.Assert.assertTrue; - -@Internal -@NullMarked -public class DefaultValueSupport { - - public static void assertValidDefaultValue(@Nullable Object value) { - assertValidDefaultValue(value, null); - } - - private static void assertValidDefaultValue(@Nullable Object value, @Nullable Set visited) { - if (value == null) { - return; - } - assertTrue(!(value instanceof Iterator) && !(value instanceof BaseStream), - "Default values must not contain an Iterator or a Stream"); - if (!(value instanceof Map) && !FpKit.isIterableOrArray(value)) { - return; - } - if (visited == null) { - visited = Collections.newSetFromMap(new IdentityHashMap<>()); - } - if (!visited.add(value)) { - return; - } - Iterable values = value instanceof Map ? ((Map) value).values() : FpKit.toIterable(value); - for (Object item : values) { - assertValidDefaultValue(item, visited); - } - } -} diff --git a/src/main/java/graphql/schema/GraphQLArgument.java b/src/main/java/graphql/schema/GraphQLArgument.java index 82cff5f6a5..516e2410fd 100644 --- a/src/main/java/graphql/schema/GraphQLArgument.java +++ b/src/main/java/graphql/schema/GraphQLArgument.java @@ -373,7 +373,6 @@ public Builder type(GraphQLInputType type) { */ @Deprecated(since = "2021-05-10") public Builder defaultValue(Object defaultValue) { - DefaultValueSupport.assertValidDefaultValue(defaultValue); this.defaultValue = InputValueWithState.newInternalValue(defaultValue); return this; } @@ -390,16 +389,13 @@ public Builder defaultValueLiteral(@NonNull Value defaultValue) { /** * Sets a programmatic default value. Iterators and streams, including nested values, - * are not supported; use an iterable or array instead. + * are rejected during schema validation; use an iterable or array instead. * * @param defaultValue Can be null to represent null value * * @return this builder - * - * @throws graphql.AssertException if the default value contains an iterator or stream */ public Builder defaultValueProgrammatic(@Nullable Object defaultValue) { - DefaultValueSupport.assertValidDefaultValue(defaultValue); this.defaultValue = InputValueWithState.newExternalValue(defaultValue); return this; } diff --git a/src/main/java/graphql/schema/GraphQLInputObjectField.java b/src/main/java/graphql/schema/GraphQLInputObjectField.java index fb8f1beb1c..bce7e3aa24 100644 --- a/src/main/java/graphql/schema/GraphQLInputObjectField.java +++ b/src/main/java/graphql/schema/GraphQLInputObjectField.java @@ -318,7 +318,6 @@ public Builder type(GraphQLInputType type) { */ @Deprecated(since = "2021-05-10") public Builder defaultValue(Object defaultValue) { - DefaultValueSupport.assertValidDefaultValue(defaultValue); this.defaultValue = InputValueWithState.newInternalValue(defaultValue); return this; } @@ -330,16 +329,13 @@ public Builder defaultValueLiteral(Value defaultValue) { /** * Sets a programmatic default value. Iterators and streams, including nested values, - * are not supported; use an iterable or array instead. + * are rejected during schema validation; use an iterable or array instead. * * @param defaultValue the default value, which can be null * * @return this builder - * - * @throws graphql.AssertException if the default value contains an iterator or stream */ public Builder defaultValueProgrammatic(Object defaultValue) { - DefaultValueSupport.assertValidDefaultValue(defaultValue); this.defaultValue = InputValueWithState.newExternalValue(defaultValue); return this; } diff --git a/src/main/java/graphql/schema/validation/DefaultValueContainersAreValid.java b/src/main/java/graphql/schema/validation/DefaultValueContainersAreValid.java new file mode 100644 index 0000000000..b6863018e8 --- /dev/null +++ b/src/main/java/graphql/schema/validation/DefaultValueContainersAreValid.java @@ -0,0 +1,72 @@ +package graphql.schema.validation; + +import graphql.Internal; +import graphql.schema.GraphQLArgument; +import graphql.schema.GraphQLInputObjectField; +import graphql.schema.GraphQLSchemaElement; +import graphql.schema.GraphQLTypeVisitorStub; +import graphql.schema.InputValueWithState; +import graphql.util.FpKit; +import graphql.util.TraversalControl; +import graphql.util.TraverserContext; +import org.jspecify.annotations.Nullable; + +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.stream.BaseStream; + +@Internal +public class DefaultValueContainersAreValid extends GraphQLTypeVisitorStub { + + @Override + public TraversalControl visitGraphQLArgument(GraphQLArgument argument, TraverserContext context) { + checkDefaultValue(argument.getArgumentDefaultValue(), argument.getName(), context); + return TraversalControl.CONTINUE; + } + + @Override + public TraversalControl visitGraphQLInputObjectField(GraphQLInputObjectField field, TraverserContext context) { + checkDefaultValue(field.getInputFieldDefaultValue(), field.getName(), context); + return TraversalControl.CONTINUE; + } + + private void checkDefaultValue(InputValueWithState defaultValue, String name, TraverserContext context) { + if (!defaultValue.isExternal() && !defaultValue.isInternal()) { + return; + } + if (hasValidContainers(defaultValue.getValue(), null)) { + return; + } + SchemaValidationErrorCollector collector = context.getVarFromParents(SchemaValidationErrorCollector.class); + collector.addError(new SchemaValidationError(SchemaValidationErrorType.InvalidDefaultValue, + "Invalid default value for '" + name + "': default values must not contain an Iterator or a Stream")); + } + + private boolean hasValidContainers(@Nullable Object value, @Nullable Set visited) { + if (value == null) { + return true; + } + if (value instanceof Iterator || value instanceof BaseStream) { + return false; + } + if (!(value instanceof Map) && !FpKit.isIterableOrArray(value)) { + return true; + } + if (visited == null) { + visited = Collections.newSetFromMap(new IdentityHashMap<>()); + } + if (!visited.add(value)) { + return true; + } + Iterable values = value instanceof Map ? ((Map) value).values() : FpKit.toIterable(value); + for (Object item : values) { + if (!hasValidContainers(item, visited)) { + return false; + } + } + return true; + } +} diff --git a/src/main/java/graphql/schema/validation/SchemaValidator.java b/src/main/java/graphql/schema/validation/SchemaValidator.java index 37f2ce7107..419e872d8f 100644 --- a/src/main/java/graphql/schema/validation/SchemaValidator.java +++ b/src/main/java/graphql/schema/validation/SchemaValidator.java @@ -30,14 +30,19 @@ public List getRules() { public Set validateSchema(GraphQLSchema schema) { SchemaValidationErrorCollector validationErrorCollector = new SchemaValidationErrorCollector(); + Map, Object> rootVars = new LinkedHashMap<>(); + rootVars.put(GraphQLSchema.class, schema); + rootVars.put(SchemaValidationErrorCollector.class, validationErrorCollector); + SchemaTraverser traverser = new SchemaTraverser(); + traverser.depthFirstFullSchema(List.of(new DefaultValueContainersAreValid()), schema, rootVars); + if (!validationErrorCollector.getErrors().isEmpty()) { + return validationErrorCollector.getErrors(); + } new NoDefaultValueCircularRefs().checkSchema(schema, validationErrorCollector); if (!validationErrorCollector.getErrors().isEmpty()) { return validationErrorCollector.getErrors(); } - Map, Object> rootVars = new LinkedHashMap<>(); - rootVars.put(GraphQLSchema.class, schema); - rootVars.put(SchemaValidationErrorCollector.class, validationErrorCollector); - new SchemaTraverser().depthFirstFullSchema(getRules(), schema, rootVars); + traverser.depthFirstFullSchema(getRules(), schema, rootVars); return validationErrorCollector.getErrors(); } diff --git a/src/test/groovy/graphql/schema/ProgrammaticDefaultValuesTest.groovy b/src/test/groovy/graphql/schema/ProgrammaticDefaultValuesTest.groovy index c0fa7ac5ff..187967cc8e 100644 --- a/src/test/groovy/graphql/schema/ProgrammaticDefaultValuesTest.groovy +++ b/src/test/groovy/graphql/schema/ProgrammaticDefaultValuesTest.groovy @@ -1,14 +1,19 @@ package graphql.schema -import graphql.AssertException import graphql.GraphQL +import graphql.schema.validation.InvalidSchemaException +import graphql.schema.validation.SchemaValidationErrorType +import graphql.schema.validation.SchemaValidator import spock.lang.Specification import java.util.stream.IntStream import java.util.stream.Stream +import static graphql.Scalars.GraphQLInt import static graphql.Scalars.GraphQLString +import static graphql.introspection.Introspection.DirectiveLocation.FIELD_DEFINITION import static graphql.schema.GraphQLArgument.newArgument +import static graphql.schema.GraphQLDirective.newDirective import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition import static graphql.schema.GraphQLInputObjectField.newInputObjectField import static graphql.schema.GraphQLInputObjectType.newInputObject @@ -18,26 +23,28 @@ import static graphql.schema.GraphQLSchema.newSchema class ProgrammaticDefaultValuesTest extends Specification { - def "#element #setter rejects #kind inside #shape without consuming it"() { + def "validation rejects #element #setter #kind inside #shape without consuming it - fast #fast"() { given: - def builder = element == 'argument' ? newArgument() : newInputObjectField() def singleUseValue = singleUseValue(kind) def defaultValue = wrapValue(singleUseValue, shape) + def builder = schemaBuilderWithDefault(element, setter, defaultValue, fast) when: - builder."$setter"(defaultValue) + builder.build() then: - def exception = thrown(AssertException) - exception.message == 'Default values must not contain an Iterator or a Stream' + def exception = thrown(InvalidSchemaException) + exception.errors*.classification == [SchemaValidationErrorType.InvalidDefaultValue] + exception.message.contains('default values must not contain an Iterator or a Stream') kind == 'iterator' ? singleUseValue.next() == 'ok' : singleUseValue.count() == 1 where: - [element, setter, kind, shape] << [ - ['argument', 'input field'], + [element, setter, kind, shape, fast] << [ + ['argument', 'input field', 'directive argument'], ['defaultValueProgrammatic', 'defaultValue'], ['iterator', 'stream', 'primitive stream'], - ['direct', 'map', 'list', 'array', 'nested containers'] + ['direct', 'map', 'list', 'array', 'nested containers'], + [false, true] ].combinations() } @@ -82,22 +89,128 @@ class ProgrammaticDefaultValuesTest extends Specification { } def "null and primitive arrays remain supported defaults"() { + given: + def input = newInputObject().name('Input') + .field(newInputObjectField().name('items').type(list(GraphQLInt)) + .defaultValueProgrammatic([1, 2] as int[])) + .field(newInputObjectField().name('name').type(GraphQLString) + .defaultValueProgrammatic(null)).build() + def query = newObject().name('Query') + .field(newFieldDefinition().name('test').type(GraphQLString) + .argument(newArgument().name('input').type(input)) + .argument(newArgument().name('items').type(list(GraphQLInt)) + .defaultValueProgrammatic([1, 2] as int[])) + .argument(newArgument().name('name').type(GraphQLString) + .defaultValueProgrammatic(null))).build() + expect: - newArgument().defaultValueProgrammatic(null) != null - newInputObjectField().defaultValueProgrammatic(null) != null - newArgument().defaultValueProgrammatic([1, 2] as int[]) != null - newInputObjectField().defaultValueProgrammatic([1, 2] as int[]) != null + newSchema().query(query).build() != null + } + + def "default setters do not traverse values"() { + given: + def value = Mock(Iterable) + + when: + newArgument().defaultValueProgrammatic(value) + newInputObjectField().defaultValueProgrammatic(value) + newArgument().defaultValue(value) + newInputObjectField().defaultValue(value) + + then: + 0 * value.iterator() + } + + def "validation catches an iterator added after setting the default"() { + given: + def value = [name: 'ok'] + def builder = schemaBuilderWithDefault('argument', 'defaultValueProgrammatic', value, false) + def iterator = ['later'].iterator() + value['later'] = iterator + + when: + builder.build() + + then: + thrown(InvalidSchemaException) + iterator.next() == 'later' + } + + def "unchecked FastBuilder defers rejection until explicit schema validation"() { + given: + def value = Stream.of('ok') + def builder = schemaBuilderWithDefault('argument', 'defaultValueProgrammatic', value, true) + def schema = builder.withValidation(false).build() + + when: + def errors = new SchemaValidator().validateSchema(schema) + + then: + errors*.classification == [SchemaValidationErrorType.InvalidDefaultValue] + value.count() == 1 + } + + def "input field defaults are rejected before argument coercion expands them"() { + given: + def value = Stream.of('ok') + def input = newInputObject().name('Input') + .field(newInputObjectField().name('items').type(list(GraphQLString)) + .defaultValueProgrammatic(value)).build() + def query = newObject().name('Query') + .field(newFieldDefinition().name('test').type(GraphQLString) + .argument(newArgument().name('arg').type(input).defaultValueProgrammatic([:]))).build() + + when: + newSchema().query(query).build() + + then: + def exception = thrown(InvalidSchemaException) + exception.errors*.classification == [SchemaValidationErrorType.InvalidDefaultValue] + exception.message.contains("Invalid default value for 'items'") + value.count() == 1 } - def "checking defaults handles shared and recursive Java containers by identity"() { + def "validation handles shared and recursive containers without overlooking an iterator"() { given: def shared = [name: 'ok'] def value = [first: shared, second: shared] value['self'] = value + def iterator = ['ok'].iterator() + value['iterator'] = iterator + def builder = schemaBuilderWithDefault('argument', 'defaultValueProgrammatic', value, false) - expect: - newArgument().defaultValueProgrammatic(value) != null - newInputObjectField().defaultValueProgrammatic(value) != null + when: + builder.build() + + then: + thrown(InvalidSchemaException) + iterator.next() == 'ok' + } + + private static Object schemaBuilderWithDefault(String element, String setter, Object value, boolean fast) { + def argument = newArgument().name('arg').type(GraphQLString) + def types = [] + def directive = newDirective().name('probe').validLocation(FIELD_DEFINITION) + if (element == 'input field') { + def field = newInputObjectField().name('value').type(GraphQLString) + field."$setter"(value) + def input = newInputObject().name('Input').field(field).build() + argument.type(input) + types.add(input) + } else { + argument."$setter"(value) + } + def queryField = newFieldDefinition().name('test').type(GraphQLString) + if (element == 'directive argument') { + directive.argument(argument) + } else { + queryField.argument(argument) + } + def query = newObject().name('Query').field(queryField).build() + return fast + ? new GraphQLSchema.FastBuilder(GraphQLCodeRegistry.newCodeRegistry(), query, null, null) + .addTypes(types).additionalDirective(directive.build()).withValidation(true) + : newSchema().query(query).additionalDirective(directive.build()) } private static Object singleUseValue(String kind) { From ef1a66a577b49ab5a4d464fdc2c5fb4889f9c404 Mon Sep 17 00:00:00 2001 From: Andreas Marek Date: Sun, 8 Nov 2026 19:49:59 +1000 Subject: [PATCH 6/6] Retry transient Gradle distribution download failures --- gradle/wrapper/gradle-wrapper.properties | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a351597e62..99fffaf09b 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,7 +1,9 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-9.6.1-bin.zip -networkTimeout=10000 +networkTimeout=60000 +retries=3 +retryBackOffMs=1000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists