1

So I have a type:

public enum Types
{
    aaa= 1,
    bbb= 2,
    ccc= 4
}

public class RequestPayload
{
    public int Prop1 { get; set; }
    public string Prop2 { get; set; }
    public Types Prop3 { get; set; }
}

And with Postman I am testing a web api.

public MyType Create([FromBody] RequestPayloadpayload)
{
    return null
}

Here are my postman settings:

enter image description here

So why in the controller my object has property Prop3 to 6666 when my enum does not have this value?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
  • 1
    Possible duplicate of [Validating Enum Values within C# MVC. Partial validation occurs - How to change validation behaviour?](http://stackoverflow.com/questions/26425242/validating-enum-values-within-c-sharp-mvc-partial-validation-occurs-how-to-ch) – Peter B Nov 17 '16 at 16:08

1 Answers1

2

I don't know anything about "postman", but I assume you're surprised that an int value other than 1, 2, or 4 can be assigned to Prop3. The reason is - that's just how enums work in C# since, under the hood, a field of an enum type is converted to an int (or whatever the underlying type of the enum is), any int value can legally be stored in it.

From MSDN:

enum Days : byte {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};  

A variable of type Days can be assigned any value in the range of the underlying type; the values are not limited to the named constants.

This is probably to avoid expensive run-time checking of values against "defined" values, but there may be other architectural reasons as well (the use of "flag" enums is one that comes to mind).

D Stanley
  • 149,601
  • 11
  • 178
  • 240