-
-
Notifications
You must be signed in to change notification settings - Fork 750
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fusion] Added pre-merge validation rule "EnumTypesInconsistentRule" (#…
…7901) Co-authored-by: Glen <[email protected]>
- Loading branch information
1 parent
1f43842
commit 85ead14
Showing
10 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/HotChocolate/Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Info/EnumTypeInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using HotChocolate.Skimmed; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Info; | ||
|
||
internal record EnumTypeInfo(EnumTypeDefinition Type, SchemaDefinition Schema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...Fusion-vnext/src/Fusion.Composition/PreMergeValidation/Rules/EnumTypesInconsistentRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Collections.Immutable; | ||
using HotChocolate.Fusion.Events; | ||
using static HotChocolate.Fusion.Logging.LogEntryHelper; | ||
|
||
namespace HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
/// <summary> | ||
/// <para> | ||
/// This rule ensures that enum types with the same name across different source schemas in a | ||
/// composite schema have identical sets of values. Enums must be consistent across source schemas | ||
/// to avoid conflicts and ambiguities in the composite schema. | ||
/// </para> | ||
/// <para> | ||
/// When an enum is defined with differing values, it can lead to confusion and errors in query | ||
/// execution. For instance, a value valid in one schema might be passed to another where it’s | ||
/// unrecognized, leading to unexpected behavior or failures. This rule prevents such | ||
/// inconsistencies by enforcing that all instances of the same named enum across schemas have an | ||
/// exact match in their values. | ||
/// </para> | ||
/// </summary> | ||
/// <seealso href="https://graphql.github.io/composite-schemas-spec/draft/#sec-Enum-Types-Inconsistent"> | ||
/// Specification | ||
/// </seealso> | ||
internal sealed class EnumTypesInconsistentRule : IEventHandler<EnumTypeGroupEvent> | ||
{ | ||
public void Handle(EnumTypeGroupEvent @event, CompositionContext context) | ||
{ | ||
var (_, enumGroup) = @event; | ||
|
||
if (enumGroup.Length < 2) | ||
{ | ||
return; | ||
} | ||
|
||
var enumValues = enumGroup | ||
.SelectMany(e => e.Type.Values) | ||
.Where(ValidationHelper.IsAccessible) | ||
.Select(v => v.Name) | ||
.ToImmutableHashSet(); | ||
|
||
foreach (var (enumType, schema) in enumGroup) | ||
{ | ||
foreach (var enumValue in enumValues) | ||
{ | ||
if (!enumType.Values.ContainsName(enumValue)) | ||
{ | ||
context.Log.Write( | ||
EnumTypesInconsistent(enumType, enumValue, schema)); | ||
} | ||
} | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...Chocolate/Fusion-vnext/src/Fusion.Composition/Properties/CompositionResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
.../test/Fusion.Composition.Tests/PreMergeValidation/Rules/EnumTypesInconsistentRuleTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
using HotChocolate.Fusion.Logging; | ||
using HotChocolate.Fusion.PreMergeValidation; | ||
using HotChocolate.Fusion.PreMergeValidation.Rules; | ||
|
||
namespace HotChocolate.Composition.PreMergeValidation.Rules; | ||
|
||
public sealed class EnumTypesInconsistentRuleTests : CompositionTestBase | ||
{ | ||
private readonly PreMergeValidator _preMergeValidator = new([new EnumTypesInconsistentRule()]); | ||
|
||
[Theory] | ||
[MemberData(nameof(ValidExamplesData))] | ||
public void Examples_Valid(string[] sdl) | ||
{ | ||
// arrange | ||
var context = CreateCompositionContext(sdl); | ||
|
||
// act | ||
var result = _preMergeValidator.Validate(context); | ||
|
||
// assert | ||
Assert.True(result.IsSuccess); | ||
Assert.True(context.Log.IsEmpty); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(InvalidExamplesData))] | ||
public void Examples_Invalid(string[] sdl, string[] errorMessages) | ||
{ | ||
// arrange | ||
var context = CreateCompositionContext(sdl); | ||
|
||
// act | ||
var result = _preMergeValidator.Validate(context); | ||
|
||
// assert | ||
Assert.True(result.IsFailure); | ||
Assert.Equal(errorMessages, context.Log.Select(e => e.Message).ToArray()); | ||
Assert.True(context.Log.All(e => e.Code == "ENUM_TYPES_INCONSISTENT")); | ||
Assert.True(context.Log.All(e => e.Severity == LogSeverity.Error)); | ||
} | ||
|
||
public static TheoryData<string[]> ValidExamplesData() | ||
{ | ||
return new TheoryData<string[]> | ||
{ | ||
// In this example, both source schemas define "Genre" with the same value "FANTASY", | ||
// satisfying the rule. | ||
{ | ||
[ | ||
""" | ||
enum Genre { | ||
FANTASY | ||
} | ||
""", | ||
""" | ||
enum Genre { | ||
FANTASY | ||
} | ||
""" | ||
] | ||
}, | ||
// Here, the two definitions of "Genre" have shared values and additional values | ||
// declared as @inaccessible, satisfying the rule. | ||
{ | ||
[ | ||
""" | ||
enum Genre { | ||
FANTASY | ||
SCIENCE_FICTION @inaccessible | ||
} | ||
""", | ||
""" | ||
enum Genre { | ||
FANTASY | ||
} | ||
""" | ||
] | ||
}, | ||
// Here, the two definitions of "Genre" have shared values in a differing order. | ||
{ | ||
[ | ||
""" | ||
enum Genre { | ||
FANTASY | ||
SCIENCE_FICTION @inaccessible | ||
ANIMATED | ||
} | ||
""", | ||
""" | ||
enum Genre { | ||
ANIMATED | ||
FANTASY | ||
CRIME @inaccessible | ||
} | ||
""" | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public static TheoryData<string[], string[]> InvalidExamplesData() | ||
{ | ||
return new TheoryData<string[], string[]> | ||
{ | ||
// Here, the two definitions of "Genre" have different values ("FANTASY" and | ||
// "SCIENCE_FICTION"), violating the rule. | ||
{ | ||
[ | ||
""" | ||
enum Genre { | ||
FANTASY | ||
} | ||
""", | ||
""" | ||
enum Genre { | ||
SCIENCE_FICTION | ||
} | ||
""" | ||
], | ||
[ | ||
"The enum type 'Genre' in schema 'A' must define the value 'SCIENCE_FICTION'.", | ||
"The enum type 'Genre' in schema 'B' must define the value 'FANTASY'." | ||
] | ||
} | ||
}; | ||
} | ||
} |