question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Should we allow enum value to ignore casing.

See original GitHub issue

Describe 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:

  1. 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.

  2. 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.

  3. 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:closed
  • Created 5 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
michaelstaibcommented, Jan 21, 2019

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.

Enum values are represented as unquoted names (ex. MOBILE_WEB). It is recommended that Enum values be “all caps”. Enum values are only used in contexts where the precise enumeration type is known. Therefore it’s not necessary to supply an enumeration type name in the literal.

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.

public enum Foo 
{
   BAR
   bar
} 

So, I am not really sold yet on changing the behavior.

1reaction
OneCyruscommented, Jan 21, 2019

btw. the dictionary class allows to specify a comparer in the constructor

dictionary constructor doc

Read more comments on GitHub >

github_iconTop Results From Across the Web

Case-insensitive matching of a string to a Java enum
No, the same requirement is applicable irrespective of case or other naming conventions. A common method for all the purposes was the request....
Read more >
Should we allow enum value to ignore casing. · Issue #516
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...
Read more >
Codable: Ignore casing while decoding Enum values
Fetches the value from the container · First, it tries to create the enum as-is from the value · If above fails, then...
Read more >
How to decode enums ignoring case in Swift Codable
Swift makes it is effortless to decode Enums from its raw value. All you need to do are make sure your enum is...
Read more >
Spring Boot: How to ignore case when using an Enum as a ...
Here is a way to enable the use of case-insensitive enumerations request parameters. You won't need to do any code modification in your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found