I'm seeing an issue where I pass into my controller an int that is outside the bounds of my enum but JsonConvert.DeserializeObject still maps to the int passed.
For example I pass in the int 9 to map to the AvatarEnum below and then when I look at the resulting model I see that DefaultAvatar=9, despite AvatarEnum clearly only going up to 5
I have this basic enum:
public enum AvatarEnum : int
{
Unknown = 0,
Balloon = 1,
Flower = 2,
Sand = 3,
Bubbles = 4,
Seaweed = 5
}
Simple model that contains the enum:
internal class DefaultAvatarModel
{
public AvatarEnum DefaultAvatar { get; set; }
}
The JSON deserialization:
try
{
avatarModel = JsonConvert.DeserializeObject<DefaultAvatarModel>(jsonMessage);
}
catch (JsonSerializationException)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, "Bad JSON request.");
}
So I pass in:
jsonMessage = "{\"DefaultAvatar\":\"9\"}"
And the result is:
avatarModel.DefaultAvatar == 9