From 3be0cccf3aaf8be959435e65074d698a999b0fed Mon Sep 17 00:00:00 2001 From: Michael Staib Date: Sun, 16 Aug 2026 15:13:33 +0200 Subject: [PATCH] Fix composition crash on bare enum default for list-typed argument --- .../EnumTypeDefaultValueInaccessibleRule.cs | 36 ++++- ...umTypeDefaultValueInaccessibleRuleTests.cs | 141 ++++++++++++++++++ 2 files changed, 169 insertions(+), 8 deletions(-) diff --git a/src/HotChocolate/Fusion/src/Fusion.Composition/PostMergeValidationRules/EnumTypeDefaultValueInaccessibleRule.cs b/src/HotChocolate/Fusion/src/Fusion.Composition/PostMergeValidationRules/EnumTypeDefaultValueInaccessibleRule.cs index e7286c5520c..ca5b24b6d44 100644 --- a/src/HotChocolate/Fusion/src/Fusion.Composition/PostMergeValidationRules/EnumTypeDefaultValueInaccessibleRule.cs +++ b/src/HotChocolate/Fusion/src/Fusion.Composition/PostMergeValidationRules/EnumTypeDefaultValueInaccessibleRule.cs @@ -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()) @@ -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; } @@ -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; } diff --git a/src/HotChocolate/Fusion/test/Fusion.Composition.Tests/PostMergeValidationRules/EnumTypeDefaultValueInaccessibleRuleTests.cs b/src/HotChocolate/Fusion/test/Fusion.Composition.Tests/PostMergeValidationRules/EnumTypeDefaultValueInaccessibleRuleTests.cs index 0a1f0cae5c3..9cf72b8ddd6 100644 --- a/src/HotChocolate/Fusion/test/Fusion.Composition.Tests/PostMergeValidationRules/EnumTypeDefaultValueInaccessibleRuleTests.cs +++ b/src/HotChocolate/Fusion/test/Fusion.Composition.Tests/PostMergeValidationRules/EnumTypeDefaultValueInaccessibleRuleTests.cs @@ -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] @@ -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": {} + } + """ + ]); + } }