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