Should we allow enum value to ignore casing.
See original GitHub issueDescribe the bug In 0.7.0-preview33 - When submitting an enum value as anything other than all-caps, the parser fails to recognize the value as a legitimate enum value.
To Reproduce With the following unit test added in EnumTypeTests.cs
[Fact]
public void ParseLiteralIgnoresCase()
{
// act
Schema schema = Schema.Create(c =>
{
c.RegisterType(new EnumType<Foo>());
c.Options.StrictValidation = false;
});
var enumValue = new EnumValueNode("Bar1");
// assert
EnumType type = schema.GetType<EnumType>("Foo");
var value = type.ParseLiteral(enumValue);
Assert.True(true);
}
the bug can be seen.
Expected behavior Enum values should be able to be parsed when they match the Enum value, as well as matching the schema description. Ideally we would ignore case when parsing enum values
Additional context Since the implementation of EnumType uses a dictionary of strings to Enum Values, there is no way to ignore case universally when parsing literal. I see three ways to potentially fix this:
-
Use a ToUpper() on each value before adding to the dictionary or trying to access the dictionary. I personally don’t like this solution because it adds a lot of extra code in several different places, and forgetting to add it in a new place would reintroduce the bug.
-
Add all permutations of the Enum values to the dictionary on initialize: ie, loop through the values, add their descriptions, add the values string representations, add those string representations ToUpper and ToLower. This is better because all of the string manipulation would happen in the initialize method, so at least its only in one place. However, there is no way to know that we have captured every permutation someone might enter, but maybe that’s acceptable. If someone enters bAr1 as their enum value, maybe it’s okay to just fail.
-
Stop using a dictionary for this and just use the Enum.Parse method, which allows us to ignore case. This requires the most code to be changed, but is probably the most elegant solution.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (6 by maintainers)
Top GitHub Comments
The GraphQL enum type does not necessarily have an enum as value type. You could have other types as .net representations.
Moreover, the spec recommends to use all caps for enum names.
2018 Spec Links Enum Value Enum Type
Furthermore, if the API specifies that the name is all caps then the enum value should be send all caps.
In c# the enum value Abc is different from the enum value ABC, so you could create an enum.
So, I am not really sold yet on changing the behavior.
btw. the dictionary class allows to specify a comparer in the constructor
dictionary constructor doc