You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
The Java client offers a useCaseInsensitiveEnums flag in the configuration to support case-insensitive string enums. It would be great if the C# SDK offered the same - it looks like this is requested for other generators in ##16634 and #17398
Describe the solution you'd like
A config option for allowing case-insensitive string enums. Our API sends a webhook that we have defined as an enum in our OpenAPI spec with the values in and out. When attempting to serialize to a model generated via the project - I get this error
Microsoft.AspNetCore.Http.BadHttpRequestException: Failed to read parameter "List<MessageDeliveredCallback> callbackData" from the request body as JSON.
---> System.Text.Json.JsonException: The JSON value could not be converted to Bandwidth.Standard.Model.MessageDeliveredCallbackMessage. Path: $[0].message.direction | LineNumber: 22 | BytePositionInLine: 23.
at System.Text.Json.ThrowHelper.ThrowJsonException(String message)
at System.Text.Json.Serialization.Converters.EnumConverter`1.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options)
at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
at System.Text.Json.Serialization.JsonConverter`1.TryReadAsObject(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, Object& value)
.....
This is the generated model definition
[JsonConverter(typeof(StringEnumConverter))]publicenumMessageDirectionEnum{/// <summary>/// Enum In for value: in/// </summary>[EnumMember(Value="in")]In=1,/// <summary>/// Enum Out for value: out/// </summary>[EnumMember(Value="out")]Out=2}
From what I can tell - dotnet doesnt have any built in support for this so we would need a custom CaseInsensitiveEnumConverter<T> class that looks something like
usingSystem;usingSystem.Text.Json;usingSystem.Text.Json.Serialization;publicclassCaseInsensitiveEnumConverter<T>:JsonConverter<T>whereT:struct,Enum{publicoverrideTRead(refUtf8JsonReaderreader,TypetypeToConvert,JsonSerializerOptionsoptions){varvalue=reader.GetString();if(Enum.TryParse(value,ignoreCase:true,outTresult)){returnresult;}thrownewJsonException($"Invalid value '{value}' for enum {typeof(T).Name}");}publicoverridevoidWrite(Utf8JsonWriterwriter,Tvalue,JsonSerializerOptionsoptions){writer.WriteStringValue(value.ToString());}}
Looking through the codebase for the csharp generator, there is a lot of custom logic around the enums already with the zeroBasedEnums config option, so ideally we wouldn't want to break that
Describe alternatives you've considered
Clients can set an option to use caseInsensitive serialization - but it would be nice to have this built in
With some guidance on how the config options dictate what is generated in the templates i could take a crack at implementing this - I have contributed to the templates previously - but not connected the build args to whats generated yet - so if there is any guidance or old PRs to look at, I'd be willing to take a crack at it
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem? Please describe.
The Java client offers a
useCaseInsensitiveEnums
flag in the configuration to support case-insensitive string enums. It would be great if the C# SDK offered the same - it looks like this is requested for other generators in ##16634 and #17398Describe the solution you'd like
A config option for allowing case-insensitive string enums. Our API sends a webhook that we have defined as an enum in our OpenAPI spec with the values
in
andout
. When attempting to serialize to a model generated via the project - I get this errorThis is the generated model definition
From what I can tell - dotnet doesnt have any built in support for this so we would need a custom
CaseInsensitiveEnumConverter<T>
class that looks something liketo be used on the generated enum classes
Looking through the codebase for the csharp generator, there is a lot of custom logic around the enums already with the
zeroBasedEnums
config option, so ideally we wouldn't want to break thatDescribe alternatives you've considered
Clients can set an option to use caseInsensitive serialization - but it would be nice to have this built in
With some guidance on how the config options dictate what is generated in the templates i could take a crack at implementing this - I have contributed to the templates previously - but not connected the build args to whats generated yet - so if there is any guidance or old PRs to look at, I'd be willing to take a crack at it
The text was updated successfully, but these errors were encountered: