Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,23 @@ private static bool ValidateDefaultValue(
{
inaccessibleCoordinate = null;

// Spec list input coercion: a non-list value on a list type is treated as a singleton list.
if (defaultType.NullableType() is ListType coercedListType
&& defaultValue.Kind is not (SyntaxKind.ListValue or SyntaxKind.NullValue or SyntaxKind.Variable))
{
return ValidateDefaultValue(
defaultValue,
coercedListType.ElementType,
out inaccessibleCoordinate);
}

switch (defaultValue)
{
case EnumValueNode enumValue:
var enumType = (MutableEnumTypeDefinition)defaultType.NullableType();
if (defaultType.NullableType() is not MutableEnumTypeDefinition enumType)
{
return true;
}

if (!enumType.Values.TryGetValue(enumValue.Value, out var value)
|| value.HasFusionInaccessibleDirective())
Expand All @@ -86,13 +99,14 @@ private static bool ValidateDefaultValue(
return true;

case ListValueNode listValue:
var listType = (ListType)defaultType.NullableType();
if (defaultType.NullableType() is not ListType listType)
{
return true;
}

foreach (var item in listValue.Items)
{
defaultType = listType.ElementType;

if (!ValidateDefaultValue(item, defaultType, out inaccessibleCoordinate))
if (!ValidateDefaultValue(item, listType.ElementType, out inaccessibleCoordinate))
{
return false;
}
Expand All @@ -101,13 +115,19 @@ private static bool ValidateDefaultValue(
return true;

case ObjectValueNode objectValue:
var inputObjectType = (MutableInputObjectTypeDefinition)defaultType.NullableType();
if (defaultType.NullableType() is not MutableInputObjectTypeDefinition inputObjectType)
{
return true;
}

foreach (var field in objectValue.Fields)
{
defaultType = inputObjectType.Fields[field.Name.Value].Type;
if (!inputObjectType.Fields.TryGetField(field.Name.Value, out var inputField))
{
continue;
}

if (!ValidateDefaultValue(field.Value, defaultType, out inaccessibleCoordinate))
if (!ValidateDefaultValue(field.Value, inputField.Type, out inaccessibleCoordinate))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,58 @@ input Input1 {
]);
}

// In this example the bare enum value "FOO" is a valid default for the list type "[Enum1!]"
// through list input coercion, and "FOO" is not marked with @inaccessible, hence it does not
// violate the rule.
[Fact]
public void Validate_EnumTypeDefaultValueAccessibleBareValueOnListType_Succeeds()
{
AssertValid(
[
"""
# Schema A
type Query {
field(arg: [Enum1!] = FOO): [Baz!]!
}

input Input1 {
field: [Enum1!] = FOO
}

enum Enum1 {
FOO
BAR
}
"""
]);
}

// In this example the object default value names a field (undeclared) that the input type does
// not declare. Undeclared fields are not checked by this rule, hence it does not violate the
// rule.
[Fact]
public void Validate_EnumTypeDefaultValueObjectValueUndeclaredField_Succeeds()
{
AssertValid(
[
"""
# Schema A
type Query {
field(arg: Input1 = { undeclared: FOO }): [Baz!]!
}

input Input1 {
field1: Enum1
}

enum Enum1 {
FOO @inaccessible
BAR
}
"""
]);
}

// The following example violates this rule because the default value for the argument (arg) and
// the input field (field) references an enum value (FOO), that is marked as @inaccessible.
[Fact]
Expand Down Expand Up @@ -202,4 +254,93 @@ FOO @inaccessible
"""
]);
}

// The following example violates this rule because the bare enum default value (FOO) for the
// list-typed argument (arg) and the input field (field) is coerced into a singleton list and
// references an enum value that is marked as @inaccessible.
[Fact]
public void Validate_EnumTypeDefaultValueInaccessibleBareValueOnListType_Fails()
{
AssertInvalid(
[
"""
# Schema A
type Query {
field(arg: [Enum1!] = FOO): [Baz!]!
}

input Input1 {
field: [Enum1!] = FOO
}

enum Enum1 {
FOO @inaccessible
BAR
}
"""
],
[
"""
{
"message": "The default value of 'Query.field(arg:)' references the inaccessible enum value 'Enum1.FOO'.",
"code": "ENUM_TYPE_DEFAULT_VALUE_INACCESSIBLE",
"severity": "Error",
"coordinate": "Query.field(arg:)",
"member": "arg",
"schema": "default",
"extensions": {}
}
""",
"""
{
"message": "The default value of 'Input1.field' references the inaccessible enum value 'Enum1.FOO'.",
"code": "ENUM_TYPE_DEFAULT_VALUE_INACCESSIBLE",
"severity": "Error",
"coordinate": "Input1.field",
"member": "field",
"schema": "default",
"extensions": {}
}
"""
]);
}

// The following example violates this rule because the bare object default value for the
// list-typed argument (arg) is coerced into a singleton list and references an @inaccessible
// enum value (FOO) within the object value.
[Fact]
public void Validate_EnumTypeDefaultValueInaccessibleObjectValueOnListType_Fails()
{
AssertInvalid(
[
"""
# Schema A
type Query {
field(arg: [Input1] = { field1: FOO }): [Baz!]!
}

input Input1 {
field1: Enum1
}

enum Enum1 {
FOO @inaccessible
BAR
}
"""
],
[
"""
{
"message": "The default value of 'Query.field(arg:)' references the inaccessible enum value 'Enum1.FOO'.",
"code": "ENUM_TYPE_DEFAULT_VALUE_INACCESSIBLE",
"severity": "Error",
"coordinate": "Query.field(arg:)",
"member": "arg",
"schema": "default",
"extensions": {}
}
"""
]);
}
}
Loading