1

We have a web API that uses snake case serialization for all the properties including enums, to enable it we used this in the startup:

services
    .AddMvcCore()
    .AddJsonOptions(opt =>
    {
        opt.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
        opt.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.None;
        opt.SerializerSettings.ContractResolver = new DefaultContractResolver
        {
            NamingStrategy = new SnakeCaseNamingStrategy { ProcessDictionaryKeys = true }
        };
        opt.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
        opt.SerializerSettings.Converters.Add(new StringEnumConverter());
    })
    .AddApiExplorer()
    .AddJsonFormatters(j => j.ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() { ProcessDictionaryKeys = true } })

That works great on properties, but we are having problems with routing and enums, for example we have this enum (we also tried with JsonProperty but fails in the same way):

[DataContract(Name = "document_type")]
public enum DocumentType
{
    [EnumMember(Value = "passport")]
    Passport,

    [EnumMember(Value = "proof_of_address")]
    ProofOfAddress,
}

and we are trying to search documents by type, so we have this route:

/clients/{clientId:guid/documents/{documentType}

and this in the controller:

[HttpGet]
[Route("/clients/{clientId:guid}/documents/{documentType}")]
public async Task<IActionResult> FindClientDocuments([FromRoute] Guid clientId, [FromRoute] DocumentType documentType)

with this route everything works great:

/clients/60a00cd4-59e2-4f52-871a-4029370f6dd8/documents/ProofOfAddress

but does not work with this:

clients/60a00cd4-59e2-4f52-871a-4029370f6dd8/documents/proof_of_address

In the latter case the enum is always the default value or if we add an action filter the error is "The value 'proof_of_address' is not valid."

Is there a way to make this escenario works besides trying to convert the value myself with a filter?

Thanks

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
Juan Zamudio
  • 373
  • 11
  • 35

1 Answers1

1

I think this question can be broken into 2 parts,

  1. Custom string to Enum conversion using the EnumMemberAttribute
  2. Telling MVC to use this custom conversion to bind model inputs.

I'm going to answer the 2nd part, and hopefully the first part should be relatively easy to implement/research. I included a link to a question directly about the first part.

Telling MVC to use custom enum conversion to bind model inputs.

Getting values from the client is always done via model binding, whereas json serialization mainly deals with formatting response data as json for output. Thus your solution should look into telling the model binder of the enum deserialization you want to use. (The default implementation of enum<=>string conversions does not look into attributes).

I tried the following custom model binder for my enum and it worked well. FooType is my enum:

public enum FooType
{
    [Description("test")]
    TestFoo,
    [Description("another")]
    AnotherFooType
}

I skipped the [EnumMember] attribute and opted for the [Description] attribute only because I wanted to use Humanizer's Enum utility, but you can implement your own way of getting enum values from the EnumMember attribute and just replace my call to DeHumanizeTo<FooType>(), an example can be found on this question.

    public class FooTypeBinder : IModelBinder
    {
        public Task BindModelAsync(ModelBindingContext bindingContext) => Task.Run(() => this.BindModel(bindingContext));

        private void BindModel(ModelBindingContext bindingContext)
        {
            var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            if (valueProviderResult.Length == 0)
            {
                bindingContext.ModelState.AddModelError(bindingContext.ModelName, "No value was provided for enum");
                return;
            }

            var stringValue = valueProviderResult.FirstValue;
            try
            {
                bindingContext.Model = this.FromString(stringValue);
                // Edit by citronas: Based on comments, the following line needs to be necessary
                bindingContext.Result = ModelBindingResult.Success(this.FromString(stringValue));
            }
            catch (NoMatchFoundException ex)
            {
                bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex.Message);
            }
        }

        private FooType FromString(string input)
        {
            //Here you should implement your custom way of checking the [EnumMember] attribute, and convert input into your enum.
            if (Enum.TryParse(typeof(FooType), input, true, out object value))
            {
                return (FooType)value;
            }
            else return input.DehumanizeTo<FooType>();
        }
    }

In my controller, I then tell MVC to use this binder for binding my parameters as follows:

  [HttpGet("{fooType}")]
    public IActionResult GetFooItems([ModelBinder(BinderType = typeof(FooTypeBinder))] FooType fooType)
    {
        // Do your thing, your enum fooType is bound correctly
        return this.Ok(fooType);
    }

I hope this helps.

citronas
  • 19,035
  • 27
  • 96
  • 164
Gerald Chifanzwa
  • 1,277
  • 7
  • 14