Hotchocolate: Error with parsing enum values when submitted as a variable in a query

Created on 8 Jan 2019  Â·  5Comments  Â·  Source: ChilliCream/hotchocolate

Describe the bug
Given an enum like
enum Foo { Bar },

line 18 in EnumValueDescriptor.cs creates a string value that is all uppercase, ie: "BAR".

Then, if you make a query where that Enum is submitted in a variable in a query, an exception is thrown if you submit the value as BAR.

"Requested value 'BAR' was not found.\r\n\r\n at System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult)\r\n at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)\r\n at HotChocolate.Execution.DictionaryToObjectConverter.VisitValue(Object value, DeserializationContext context)\r\n at HotChocolate.Execution.DictionaryToObjectConverter.VisitField(KeyValuePair2 field, DeserializationContext context)\r\n"`

However, if you submit the value as "Bar" it works fine.

The schema documentation generated is therefore confusion.

I suggest either getting rid of "ToUpperInvariant()" on line 18 of EnumValueDescriptor.cs or changing line 98 on DictionaryToObjectConverter from context.Object = Enum.Parse(context.Type, s); to context.Object = Enum.Parse(context.Type, s, true); to have it ignore the case of the string being parsed.

To Reproduce
I can't find an easy reproduction case without standing up an entire schema. To see the issue in pieces, note that the test on line 51 of EnumTypeDescriptorTests.cs confirms that the values generated are capitalized and the following test:

 [Fact]
        public void DictionaryToObjectConverterCanParseImplicitlyAddedValues()
        {
            var converter = new DictionaryToObjectConverter();
            var context = new DeserializationContext();
            context.Type = typeof(FooEnum);

            converter.Visit("BAR1", context);
       }

throws the Exception

Expected behavior
The values generated for the schema should be able to be parsed when submitted as variables

bug

All 5 comments

context.Object = Enum.Parse(context.Type, s, true);

this one should be the fix. Do you want to make a pull request?

The GraphQL spec does not force upper-case enums but strongly urges to make enum values upper case.

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.

https://facebook.github.io/graphql/June2018/#EnumValue

Yup, I'll have it soon.

It looks like I was working off an old version of the code when I wrote up this bug report. 0.6.11. Looking through the code it appears that 0.7.0-preview has fixed this issue. Unfortunately because I can't figure out how the new DataLoaderRegistry is supposed to work, I can't test it.

This is fixed in 7.0.0

Great :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hundreder picture hundreder  Â·  3Comments

sascha-andres picture sascha-andres  Â·  4Comments

hognevevle picture hognevevle  Â·  3Comments

nigel-sampson picture nigel-sampson  Â·  5Comments

sergeyshaykhullin picture sergeyshaykhullin  Â·  3Comments